diff --git a/src/builtin.cpp b/src/builtin.cpp index ae6b042fb..4cb9da31f 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -493,7 +493,7 @@ bool builtin_exists(const wcstring &cmd) { return static_cast(builtin_look /// Is the command a keyword or a builtin we need to special-case the handling of `-h` and `--help`. static const wcstring_list_t help_builtins({L"for", L"while", L"function", L"if", L"end", L"switch", - L"case", L"printf", L"argparse"}); + L"case", L"printf"}); static bool cmd_needs_help(const wchar_t *cmd) { return contains(help_builtins, cmd); } /// Execute a builtin command diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 429f99033..55b41bb2f 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -57,6 +57,7 @@ class option_spec_t { class argparse_cmd_opts_t { public: + bool print_help = false; bool stop_nonopt = false; size_t min_args = 0; size_t max_args = SIZE_MAX; @@ -79,6 +80,7 @@ static const wchar_t *short_options = L"+:hn:sx:N:X:"; static const struct woption long_options[] = {{L"stop-nonopt", no_argument, NULL, 's'}, {L"name", required_argument, NULL, 'n'}, {L"exclusive", required_argument, NULL, 'x'}, + {L"help", no_argument, NULL, 'h'}, {L"min-args", required_argument, NULL, 'N'}, {L"max-args", required_argument, NULL, 'X'}, {NULL, 0, NULL, 0}}; @@ -356,6 +358,10 @@ static int parse_cmd_opts(argparse_cmd_opts_t &opts, int *optind, //!OCLINT(hig opts.raw_exclusive_flags.push_back(w.woptarg); break; } + case 'h': { + opts.print_help = true; + break; + } case 'N': { long x = fish_wcstol(w.woptarg); if (errno || x < 0) { @@ -657,6 +663,11 @@ int builtin_argparse(parser_t &parser, io_streams_t &streams, wchar_t **argv) { int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; + if (opts.print_help) { + builtin_print_help(parser, streams, cmd, streams.out); + return STATUS_CMD_OK; + } + wcstring_list_t args; args.push_back(opts.name); while (optind < argc) args.push_back(argv[optind++]);