diff --git a/src/builtin_function.cpp b/src/builtin_function.cpp index 7ba5c9c06..3012e8832 100644 --- a/src/builtin_function.cpp +++ b/src/builtin_function.cpp @@ -35,9 +35,9 @@ struct function_cmd_opts_t { wcstring_list_t wrap_targets; }; -// This command is atypical in using the "+" (REQUIRE_ORDER) option for flag parsing. +// This command is atypical in using the "-" (RETURN_IN_ORDER) option for flag parsing. // This is needed due to the semantics of the -a/--argument-names flag. -static const wchar_t *const short_options = L"+:a:d:e:hj:p:s:v:w:SV:"; +static const wchar_t *const short_options = L"-:a:d:e:hj:p:s:v:w:SV:"; static const struct woption long_options[] = {{L"description", required_argument, NULL, 'd'}, {L"on-signal", required_argument, NULL, 's'}, {L"on-job-exit", required_argument, NULL, 'j'}, @@ -56,8 +56,20 @@ static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(hig const wchar_t *cmd = L"function"; int opt; wgetopter_t w; + bool handling_named_arguments = false; while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + if (opt != 'a' && opt != 1) handling_named_arguments = false; switch (opt) { + case 1: { + if (handling_named_arguments) { + opts.named_arguments.push_back(w.woptarg); + break; + } else { + streams.err.append_format(_(L"%ls: Unexpected positional argument '%ls'"), cmd, + w.woptarg); + return STATUS_INVALID_ARGS; + } + } case 'd': { opts.description = w.woptarg; break; @@ -122,6 +134,7 @@ static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(hig break; } case 'a': { + handling_named_arguments = true; opts.named_arguments.push_back(w.woptarg); break; } diff --git a/tests/checks/function.fish b/tests/checks/function.fish index 58a7d99a5..5d6a22be1 100644 --- a/tests/checks/function.fish +++ b/tests/checks/function.fish @@ -2,13 +2,3 @@ function t --argument-names a b c echo t end - -function t2 --argument-names a b c --no-scope-shadowing - echo t2 -end -#CHECKERR: {{.*/?}}function.fish (line {{\d+}}): function: Variable name '--no-scope-shadowing' is not valid. See `help identifiers`. -#CHECKERR: function t2 --argument-names a b c --no-scope-shadowing -#CHECKERR: ^ - -functions -q t2 && echo exists || echo does not exist -#CHECK: does not exist