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

Better handling of required option without a default #66

Open
kevmoo opened this issue Mar 24, 2021 · 2 comments
Open

Better handling of required option without a default #66

kevmoo opened this issue Mar 24, 2021 · 2 comments

Comments

@kevmoo
Copy link
Owner

kevmoo commented Mar 24, 2021

Warn during generation?
Improve error when parsing?

@t33x
Copy link

t33x commented Dec 5, 2021

I ran into this problem. Let's look at an example with 2 options:

Usage: mycmdapp mycmd
-h, --help                 Print this usage information.
-c, --important-param      my important option

I currently have 2 options.

My first option is to make the parameter required, but not specify a default value. In which case an exception is thrown in the generated code if the parameter is missing:

at ... _$parseMyArgsClassResult(ArgResults results) => ....(importantParam: result['important-param'] as String, ...)
Exception: type 'Null' is not a subtype of type 'String' in type cast

My second option is to make the parameter required and specify an empty default value. In this case I have to validate the argument manually - it's okay and works - , but the bigger problem is that the help does not tell me that the parameter is mandatory:

Usage: mycmdapp mycmd
-h, --help                 Print this usage information.
-c, --important-param      my important option
                           (defaults to "")

The 'args' package already supports the "mandatory" flag ('build_cli_annotations' does not), but this is not a complete solution either:

Usage: mycmdapp mycmd
-h, --help                             Print this usage information.
-c, --important-param (mandatory)      my important option

The problem is that in this case the help does not work:

# mycmdapp mycmd -h
Option 'important-param' is mandatory.

Perhaps a solution would be to make 'build_cli_annotations' support the use of the 'mandatory' flag, and to somehow allow the precedence of the help option to be raised above the mandatory flagged options. But the latter would have to be done in the 'args' package not in the 'build_cli' package.

@fzyzcjy
Copy link
Contributor

fzyzcjy commented Dec 17, 2023

It seems that args package supports mandatory flag. Therefore, it seems that by simply changing

ArgParser _$populateBenchConfigParser(ArgParser parser) => parser
  ..addOption(
    'sth',
  );

to

ArgParser _$populateBenchConfigParser(ArgParser parser) => parser
  ..addOption(
    'sth',
    mandatory: true, // <- NOTE
  );

We can have a better error message.

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

No branches or pull requests

3 participants