normalize flag parsing

This commit is contained in:
Kurtis Rader 2017-06-09 13:29:16 -07:00
parent 2b4f61f294
commit 75e9c863f6
2 changed files with 40 additions and 54 deletions

View file

@ -316,22 +316,6 @@ static void print_variables(int include_values, int esc, bool shorten_ok, int sc
/// The set builtin creates, updates, and erases (removes, deletes) variables. /// The set builtin creates, updates, and erases (removes, deletes) variables.
int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) { int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wchar_t *cmd = argv[0]; wchar_t *cmd = argv[0];
wgetopter_t w;
// Variables used for parsing the argument list.
const struct woption long_options[] = {{L"export", no_argument, 0, 'x'},
{L"global", no_argument, 0, 'g'},
{L"local", no_argument, 0, 'l'},
{L"erase", no_argument, 0, 'e'},
{L"names", no_argument, 0, 'n'},
{L"unexport", no_argument, 0, 'u'},
{L"universal", no_argument, 0, 'U'},
{L"long", no_argument, 0, 'L'},
{L"query", no_argument, 0, 'q'},
{L"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
const wchar_t *short_options = L"+xglenuULqh";
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
// Flags to set the work mode. // Flags to set the work mode.
@ -343,24 +327,32 @@ int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
const int incoming_exit_status = proc_get_last_status(); const int incoming_exit_status = proc_get_last_status();
// Variables used for performing the actual work. // Variables used for performing the actual work.
wchar_t *dest = 0; wchar_t *dest = NULL;
int retcode = STATUS_CMD_OK; int retcode = STATUS_CMD_OK;
int scope; int scope;
int slice = 0; int slice = 0;
// Variables used for parsing the argument list. This command is atypical in using the "+"
// (REQUIRE_ORDER) option for flag parsing. This is not typical of most fish commands. It means
// we stop scanning for flags when the first non-flag argument is seen.
static const wchar_t *short_options = L"+LUeghlnqux";
static const struct woption long_options[] = {{L"export", no_argument, NULL, 'x'},
{L"global", no_argument, NULL, 'g'},
{L"local", no_argument, NULL, 'l'},
{L"erase", no_argument, NULL, 'e'},
{L"names", no_argument, NULL, 'n'},
{L"unexport", no_argument, NULL, 'u'},
{L"universal", no_argument, NULL, 'U'},
{L"long", no_argument, NULL, 'L'},
{L"query", no_argument, NULL, 'q'},
{L"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
// Parse options to obtain the requested operation and the modifiers. // Parse options to obtain the requested operation and the modifiers.
w.woptind = 0; int opt;
while (1) { wgetopter_t w;
int c = w.wgetopt_long(argc, argv, short_options, long_options, 0); while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
if (c == -1) {
break;
}
switch (c) {
case 0: {
break;
}
case 'e': { case 'e': {
erase = 1; erase = 1;
preserve_failure_exit_status = false; preserve_failure_exit_status = false;
@ -408,7 +400,10 @@ int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]); builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS; return STATUS_INVALID_ARGS;
} }
default: { break; } default: {
DIE("unexpected retval from wgetopt_long");
break;
}
} }
} }

View file

@ -54,20 +54,19 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// By the time this is called we should have initialized the curses subsystem. // By the time this is called we should have initialized the curses subsystem.
assert(curses_initialized); assert(curses_initialized);
wgetopter_t w;
// Variables used for parsing the argument list. // Variables used for parsing the argument list.
const struct woption long_options[] = {{L"background", required_argument, 0, 'b'}, static const wchar_t *short_options = L"b:hvoidrcu";
{L"help", no_argument, 0, 'h'}, static const struct woption long_options[] = {{L"background", required_argument, NULL, 'b'},
{L"bold", no_argument, 0, 'o'}, {L"help", no_argument, NULL, 'h'},
{L"underline", no_argument, 0, 'u'}, {L"bold", no_argument, NULL, 'o'},
{L"italics", no_argument, 0, 'i'}, {L"underline", no_argument, NULL, 'u'},
{L"dim", no_argument, 0, 'd'}, {L"italics", no_argument, NULL, 'i'},
{L"reverse", no_argument, 0, 'r'}, {L"dim", no_argument, NULL, 'd'},
{L"version", no_argument, 0, 'v'}, {L"reverse", no_argument, NULL, 'r'},
{L"print-colors", no_argument, 0, 'c'}, {L"version", no_argument, NULL, 'v'},
{0, 0, 0, 0}}; {L"print-colors", no_argument, NULL, 'c'},
{NULL, 0, NULL, 0}};
const wchar_t *short_options = L"b:hvoidrcu";
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
// Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a // Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a
@ -80,18 +79,10 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
bool bold = false, underline = false, italics = false, dim = false, reverse = false; bool bold = false, underline = false, italics = false, dim = false, reverse = false;
// Parse options to obtain the requested operation and the modifiers. // Parse options to obtain the requested operation and the modifiers.
w.woptind = 0; int opt;
while (1) { wgetopter_t w;
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0); while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
if (opt == -1) {
break;
}
switch (opt) { switch (opt) {
case 0: {
break;
}
case 'b': { case 'b': {
bgcolor = w.woptarg; bgcolor = w.woptarg;
break; break;
@ -128,7 +119,7 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_INVALID_ARGS; return STATUS_INVALID_ARGS;
} }
default: { default: {
DIE("unexpected opt"); DIE("unexpected retval from wgetopt_long");
break; break;
} }
} }