From af0e08e9f1bfd0027764c714678c7cd624353c83 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 26 Apr 2019 18:37:24 +0200 Subject: [PATCH] argparse: Use the current function name by default This makes the `--name` option usually unnecessary. See #5835. --- CHANGELOG.md | 1 + sphinx_doc_src/cmds/argparse.rst | 4 ++-- src/builtin_argparse.cpp | 11 ++++++++++- tests/argparse.err | 8 ++++++++ tests/argparse.in | 8 ++++++++ tests/argparse.out | 3 +++ 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7e4c2193..53f154ff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/sphinx_doc_src/cmds/argparse.rst b/sphinx_doc_src/cmds/argparse.rst index 071757170..d930a07cc 100644 --- a/sphinx_doc_src/cmds/argparse.rst +++ b/sphinx_doc_src/cmds/argparse.rst @@ -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. diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 4c70fd657..a8316a31c 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -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 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); } diff --git a/tests/argparse.err b/tests/argparse.err index 5c8457310..894d07d74 100644 --- a/tests/argparse.err +++ b/tests/argparse.err @@ -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' diff --git a/tests/argparse.in b/tests/argparse.in index 2361605a5..ce33d1a9d 100644 --- a/tests/argparse.in +++ b/tests/argparse.in @@ -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 diff --git a/tests/argparse.out b/tests/argparse.out index 1267efa51..b1565bb6c 100644 --- a/tests/argparse.out +++ b/tests/argparse.out @@ -129,3 +129,6 @@ expected argparse return status 57 #################### # Explicit int flag validation + +#################### +# Errors use function name by default