build_cli
build_cli copied to clipboard
Better handling of required option without a default
Warn during generation? Improve error when parsing?
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.
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.