Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing values from options #7

Open
platisd opened this issue Dec 5, 2022 · 1 comment
Open

Support parsing values from options #7

platisd opened this issue Dec 5, 2022 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@platisd
Copy link
Owner

platisd commented Dec 5, 2022

Right now options (i.e. command line arguments that start with - or --) have an implicit boolean value, true if they are present and false otherwise.

It would be cool if we could support the following syntax for all the types we already support:

const auto get = UnparsedCommand::create("get", "Get configuration key", "[-xyz] <key> [default]")
                         .withOption<int>("intOptionName")  // --intKeyOptionName <someFloat>
                         .withOption<bool>("boolOptionName") // --boolOptionName <true|false>
                         .withOptions({ "x", "y", "z" }) // Should this still work? Open for discussion
                         .withArgs<std::string, std::optional<std::string>>();

Regarding bool options and implicit values (i.e. a user does not have to specify true or false after them, I am thinking that we probably need to specify in advance whether a value is expected or not, otherwise there will be confusion with the positional arguments that are the library's primary use case.
Resolving this is not a priority, i.e. it's fine if they have to be explicitly set.

When it comes to fetching values, I think that everything should be an std::optional. If the option is not there, then std::nullopt should be returned. In the bool case maybe we can keep the current API with hasOption. OK to remove.
Anyway, to fetch the value of an option, one should ideally do, not sure if possible:

// No need to declare the type somewhere, it will be std::optional<int>
// because we asked for withOption<int> earlier
const auto intOption = parsedCommand.getOption("intOptionName");

Otherwise:

const auto intOption = parsedCommand.getOption<int>("intOptionName");
@platisd platisd added enhancement New feature or request good first issue Good for newcomers labels Dec 5, 2022
@platisd
Copy link
Owner Author

platisd commented Dec 5, 2022

I don't think the "ideal" syntax is feasible, so let's go for something like this instead:

using CommandParser::operator""_i; // Integer option
using CommandParser::operator""_b; // Boolean option
const auto get = UnparsedCommand::create("get", "Get configuration key", "[-xyz] <key> [default]")
                         .withOption("intOptionName"_i)  // --intKeyOptionName <someFloat>
                         .withOption("boolOptionName"_b) // --boolOptionName <true|false>
                         .withOptions({ "x", "y", "z" }) // Should this still work? Open for discussion
                         .withArgs<std::string, std::optional<std::string>>();

And then:

const auto intOption = parsedCommand.getOption("intOptionName"_i); // return is std::optional<int>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant