mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-06 18:18:51 +00:00
3005adebd5
Unfortunately print_hints was true *by default* - so for all builtins
that didn't pass it it would now be false instead.
This resulted in the trailer missing, which includes the line number
and context. So if you ran a script that includes `bind -M` the error
message would now just be "bind: -M: option requires an argument",
with no indication as to where.
This reverts commit 8a50d47a46
.
108 lines
3.2 KiB
C++
108 lines
3.2 KiB
C++
// Implementation of the builtin builtin.
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
#include "builtin.h"
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
|
|
#include "../builtin.h"
|
|
#include "../common.h"
|
|
#include "../fallback.h" // IWYU pragma: keep
|
|
#include "../io.h"
|
|
#include "../maybe.h"
|
|
#include "../wgetopt.h"
|
|
#include "../wutil.h" // IWYU pragma: keep
|
|
|
|
struct builtin_cmd_opts_t {
|
|
bool print_help = false;
|
|
bool list_names = false;
|
|
bool query = false;
|
|
};
|
|
static const wchar_t *const short_options = L":hnq";
|
|
static const struct woption long_options[] = {
|
|
{L"help", no_argument, 'h'}, {L"names", no_argument, 'n'}, {L"query", no_argument, 'q'}, {}};
|
|
|
|
static int parse_cmd_opts(builtin_cmd_opts_t &opts, int *optind, int argc, const wchar_t **argv,
|
|
parser_t &parser, io_streams_t &streams) {
|
|
const wchar_t *cmd = argv[0];
|
|
int opt;
|
|
wgetopter_t w;
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
|
|
switch (opt) {
|
|
case 'h': {
|
|
opts.print_help = true;
|
|
break;
|
|
}
|
|
case 'n': {
|
|
opts.list_names = true;
|
|
break;
|
|
}
|
|
case 'q': {
|
|
opts.query = true;
|
|
break;
|
|
}
|
|
case ':': {
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
|
return STATUS_INVALID_ARGS;
|
|
}
|
|
case '?': {
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
return STATUS_INVALID_ARGS;
|
|
}
|
|
default: {
|
|
DIE("unexpected retval from wgetopt_long");
|
|
}
|
|
}
|
|
}
|
|
|
|
*optind = w.woptind;
|
|
return STATUS_CMD_OK;
|
|
}
|
|
|
|
/// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
|
|
/// parser. All this code does is some additional operational modes, such as printing a list of all
|
|
/// builtins, printing help, etc.
|
|
maybe_t<int> builtin_builtin(parser_t &parser, io_streams_t &streams, const wchar_t **argv) {
|
|
const wchar_t *cmd = argv[0];
|
|
int argc = builtin_count_args(argv);
|
|
builtin_cmd_opts_t opts;
|
|
|
|
int optind;
|
|
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);
|
|
return STATUS_CMD_OK;
|
|
}
|
|
|
|
if (opts.query && opts.list_names) {
|
|
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd,
|
|
_(L"--query and --names are mutually exclusive"));
|
|
return STATUS_INVALID_ARGS;
|
|
}
|
|
|
|
if (opts.query) {
|
|
wcstring_list_t names = builtin_get_names();
|
|
retval = STATUS_CMD_ERROR;
|
|
for (int i = optind; i < argc; i++) {
|
|
if (contains(names, argv[i])) {
|
|
retval = STATUS_CMD_OK;
|
|
break;
|
|
}
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
if (opts.list_names) {
|
|
wcstring_list_t names = builtin_get_names();
|
|
std::sort(names.begin(), names.end());
|
|
|
|
for (auto &name : names) {
|
|
streams.out.append(name + L"\n");
|
|
}
|
|
}
|
|
|
|
return STATUS_CMD_OK;
|
|
}
|