fix argparse --help

Also stop special-casing `printf` as if it were a syntactical keyword
with respect to handling `printf --help`. It should use the same pattern
as every other builtin command.
This commit is contained in:
Kurtis Rader 2017-07-19 11:44:53 -07:00
parent f78ab085b5
commit 9e08609f85
3 changed files with 25 additions and 13 deletions

View File

@ -107,7 +107,7 @@ void builtin_wperror(const wchar_t *s, io_streams_t &streams) {
}
}
static const wchar_t *short_options = L":h";
static const wchar_t *short_options = L"+:h";
static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
@ -491,9 +491,9 @@ void builtin_destroy() {}
/// Is there a builtin command with the given name?
bool builtin_exists(const wcstring &cmd) { return static_cast<bool>(builtin_lookup(cmd)); }
/// Is the command a keyword or a builtin we need to special-case the handling of `-h` and `--help`.
/// Is the command a keyword 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"case"});
static bool cmd_needs_help(const wchar_t *cmd) { return contains(help_builtins, cmd); }
/// Execute a builtin command

View File

@ -397,6 +397,8 @@ static int parse_cmd_opts(argparse_cmd_opts_t &opts, int *optind, //!OCLINT(hig
}
}
if (opts.print_help) return STATUS_CMD_OK;
if (argc == w.woptind || wcscmp(L"--", argv[w.woptind - 1]) == 0) {
// The user didn't specify any option specs.
streams.err.append_format(_(L"%ls: No option specs were provided\n"), cmd);

View File

@ -719,21 +719,31 @@ int builtin_printf_state_t::print_formatted(const wchar_t *format, int argc, wch
/// The printf builtin.
int builtin_printf(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
UNUSED(parser);
builtin_printf_state_t state(streams);
wchar_t *format;
int args_used;
const wchar_t *cmd = argv[0];
int argc = builtin_count_args(argv);
help_only_cmd_opts_t opts;
if (argc <= 1) {
state.fatal_error(_(L"printf: not enough arguments"));
int optind;
int retval = parse_help_only_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;
}
argc -= optind;
argv += optind;
if (argc < 1) {
streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1, argc);
return STATUS_INVALID_ARGS;
}
format = argv[1];
argc -= 2;
argv += 2;
builtin_printf_state_t state(streams);
int args_used;
wchar_t *format = argv[0];
argc--;
argv++;
do {
args_used = state.print_formatted(format, argc, argv);