argparse: Use the current function name by default

This makes the `--name` option usually unnecessary.

See #5835.
This commit is contained in:
Fabian Homborg 2019-04-26 18:37:24 +02:00
parent eb0e0a4ab4
commit af0e08e9f1
6 changed files with 32 additions and 3 deletions

View file

@ -26,6 +26,7 @@
- `count` now also counts lines fed on stdin (#5744).
- `printf` prints what it can when input hasn't been fully converted to a number, but still prints an error (#5532).
- `complete -C foo` now works instead of erroring out and requiring `complete -Cfoo`.
- `argparse` now defaults to showing the current function name (instead of `argparse`) in its errors, making `--name` often superfluous (#5835).
### Interactive improvements
- Major improvements in performance and functionality to the 'sorin' sample prompt (#5411).

View file

@ -25,9 +25,9 @@ Options
The following ``argparse`` options are available. They must appear before all OPTION_SPECs:
- ``-n`` or ``--name`` is the command name to insert into any error messages. If you don't provide this value ``argparse`` will be used.
- ``-n`` or ``--name`` is the command name for use in error messages. By default the current function name will be used, or `argparse` if run outside of a function.
- ``-x`` or ``--exclusive`` should be followed by a comma separated list of short of long options that are mutually exclusive. You can use this option more than once to define multiple sets of mutually exclusive options.
- ``-x`` or ``--exclusive`` should be followed by a comma separated list of short or long options that are mutually exclusive. You can use this more than once to define multiple sets of mutually exclusive options.
- ``-N`` or ``--min-args`` is followed by an integer that defines the minimum number of acceptable non-option arguments. The default is zero.

View file

@ -49,7 +49,7 @@ struct argparse_cmd_opts_t {
size_t min_args = 0;
size_t max_args = SIZE_MAX;
wchar_t implicit_int_flag = L'\0';
wcstring name = L"argparse";
wcstring name = L"";
wcstring_list_t raw_exclusive_flags;
wcstring_list_t argv;
std::unordered_map<wchar_t, option_spec_ref_t> options;
@ -405,6 +405,15 @@ static int parse_cmd_opts(argparse_cmd_opts_t &opts, int *optind, //!OCLINT(hig
return STATUS_INVALID_ARGS;
}
if (opts.name.empty()) {
// If no name has been given, we default to the function name.
// If any error happens, the backtrace will show which argparse it was.
const wchar_t *fn = parser.get_function_name(1);
if (!fn) fn = L"argparse";
opts.name = fn;
}
*optind = w.woptind;
return collect_option_specs(opts, optind, argc, argv, streams);
}

View file

@ -104,3 +104,11 @@ argparse: Value 'a1' for flag 'm' is not an integer
# Explicit int flag validation
argparse: Value '2' for flag 'm' greater than max allowed of '1'
argparse: Value '-1' for flag 'max' less than min allowed of '0'
####################
# Errors use function name by default
notargparse: Unknown option '--banana'
Standard input (line 353):
argparse a/alpha -- --banana
^
in function 'notargparse'

View file

@ -171,3 +171,11 @@ argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=0 bargle
or echo unexpected argparse return status $status >&2
argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=1 bargle
or echo unexpected argparse return status $status >&2
logmsg Errors use function name by default
function notargparse
argparse a/alpha -- --banana
end
notargparse
true

View file

@ -129,3 +129,6 @@ expected argparse return status 57
####################
# Explicit int flag validation
####################
# Errors use function name by default