Skip to content

Commit

Permalink
Adds a --quiet/-q option for command -s
Browse files Browse the repository at this point in the history
I think devnull'ing this builtin to check presence is a common
enough chore that a --quiet option which works like it does on
`type` would be handy.
  • Loading branch information
floam committed Nov 27, 2016
1 parent f63c8a7 commit b41b887
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc_src/command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The following options are available:

- `-s` or `--search` returns the name of the disk file that would be executed, or nothing if no file with the specified name could be found in the `$PATH`.

With the `-s` option, `command` treats every argument as a separate command to look up and sets the exit status to 0 if any of the specified commands were found, or 1 if no commands could be found.
With the `-s` option, `command` treats every argument as a separate command to look up and sets the exit status to 0 if any of the specified commands were found, or 1 if no commands could be found. Additionally passing a `-q` or `--quiet` option prevents any paths from being printed, like the `type -q`, for testing only the exit status.

For basic compatibility with POSIX `command`, the `-v` flag is recognized as an alias for `-s`.

Expand Down
17 changes: 11 additions & 6 deletions src/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,17 +821,18 @@ static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv)
static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
int argc = builtin_count_args(argv);
int print_path = 0;
bool find_path = false;
bool quiet = false;

w.woptind = 0;

static const struct woption long_options[] = {
{L"search", no_argument, 0, 's'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
{L"quiet", no_argument, 0, 'q'}, {L"search", no_argument, 0, 's'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};

while (1) {
int opt_index = 0;

int opt = w.wgetopt_long(argc, argv, L"svh", long_options, &opt_index);
int opt = w.wgetopt_long(argc, argv, L"qsvh", long_options, &opt_index);
if (opt == -1) break;

switch (opt) {
Expand All @@ -848,7 +849,11 @@ static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **ar
}
case 's':
case 'v': {
print_path = 1;
find_path = true;
break;
}
case 'q': {
quiet = true;
break;
}
case '?': {
Expand All @@ -862,7 +867,7 @@ static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **ar
}
}

if (!print_path) {
if (!find_path) {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_ERROR;
}
Expand All @@ -873,7 +878,7 @@ static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **ar
const wchar_t *command_name = argv[idx];
wcstring path;
if (path_get_path(command_name, &path)) {
streams.out.append_format(L"%ls\n", path.c_str());
if (!quiet) streams.out.append_format(L"%ls\n", path.c_str());
++found;
}
}
Expand Down

0 comments on commit b41b887

Please sign in to comment.