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

option for required argument? #5

Closed
esromneb opened this issue Feb 12, 2021 · 7 comments
Closed

option for required argument? #5

esromneb opened this issue Feb 12, 2021 · 7 comments

Comments

@esromneb
Copy link

I am looking for a way to make an argument required. I see the dropt_attr_optional_val but can you add a required version?

@jamesderlin
Copy link
Owner

If an option is configured to take an argument, the argument is required by default (unless dropt_attr_optional_val is specified).

Do you have a specific example where that doesn't behave the way that you want?

@jamesderlin
Copy link
Owner

Also, to be clear: are you asking to make an option's argument required (e.g. --option=REQUIRED_ARGUMENT), or are you asking to make an option itself required (e.g. --some_required_option)?

@esromneb
Copy link
Author

esromneb commented Feb 12, 2021

I'm asking for --some_required_option. I hope that makes it clear, if not I can give an example.

In my case I want this for an int, so it would be --some_required_option=4 or --some_required_option=10 or whatever the user passes

@jamesderlin
Copy link
Owner

jamesderlin commented Feb 12, 2021

Currently you'd need to make --some_required_option write to some variable and later verify that it was modified:

int required_value = INT_MAX; // Or some other suitable sentinel value.
dropt_option options[] =
{
    ...
    { '\0', "some_required_option", "description", "ARG", dropt_handle_int, &required_value },
    { 0 }
};
...
char** rest = dropt_parse(droptContext, -1, &argv[1]);
...
if (required_value == INT_MAX)
{
    // Print an error and abort.
}

If there is no suitable sentinel value, you could handle it as a string and parse it later:

char* required_value_str = NULL;
dropt_option options[] =
{
    ...
    { '\0', "some_required_option", "description", "ARG", dropt_handle_int, &required_value_str },
    { 0 }
};
...
char** rest = dropt_parse(droptContext, -1, &argv[1]);
...
if (required_value_str == NULL)
{
    // Print an error and abort.
}
int required_value;
dropt_error error = dropt_handle_int(droptContext, NULL, required_value_str, &required_value);
if (error != dropt_error_none)
{
    // Print an error and abort.
}

@jamesderlin
Copy link
Owner

I'll spend some time thinking if there's a good way to allow dropt to support required options directly, but I suspect that it will be unlikely.

@esromneb
Copy link
Author

Ok thanks! These examples are helpful!

@jamesderlin
Copy link
Owner

I'm sorry, but after some deliberation, I've decided not to implement this.

In general, required options aren't an isolated feature and have implications on how things like --help and --version are handled (where even "required" options shouldn't actually be required in some cases). This problem arose when required options were recently bolted onto the commonly-used argument parser for the Dart ecosystem. dropt maybe would start off in a slightly better position since it likely could leverage dropt_attr_halt to identify such cases, but it's still extra complexity that I'd prefer to avoid. (dropt really is deliberately rudimentary.)

I think ultimately it'd be better to document this scenario with examples, perhaps in an FAQ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants