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.
int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
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);
// 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();
// Variables used for performing the actual work.
wchar_t *dest = 0;
wchar_t *dest = NULL;
int retcode = STATUS_CMD_OK;
int scope;
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.
w.woptind = 0;
while (1) {
int c = w.wgetopt_long(argc, argv, short_options, long_options, 0);
if (c == -1) {
break;
}
switch (c) {
case 0: {
break;
}
int opt;
wgetopter_t w;
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 'e': {
erase = 1;
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]);
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.
assert(curses_initialized);
wgetopter_t w;
// Variables used for parsing the argument list.
const struct woption long_options[] = {{L"background", required_argument, 0, 'b'},
{L"help", no_argument, 0, 'h'},
{L"bold", no_argument, 0, 'o'},
{L"underline", no_argument, 0, 'u'},
{L"italics", no_argument, 0, 'i'},
{L"dim", no_argument, 0, 'd'},
{L"reverse", no_argument, 0, 'r'},
{L"version", no_argument, 0, 'v'},
{L"print-colors", no_argument, 0, 'c'},
{0, 0, 0, 0}};
static const wchar_t *short_options = L"b:hvoidrcu";
static const struct woption long_options[] = {{L"background", required_argument, NULL, 'b'},
{L"help", no_argument, NULL, 'h'},
{L"bold", no_argument, NULL, 'o'},
{L"underline", no_argument, NULL, 'u'},
{L"italics", no_argument, NULL, 'i'},
{L"dim", no_argument, NULL, 'd'},
{L"reverse", no_argument, NULL, 'r'},
{L"version", no_argument, NULL, 'v'},
{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);
// 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;
// Parse options to obtain the requested operation and the modifiers.
w.woptind = 0;
while (1) {
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
if (opt == -1) {
break;
}
int opt;
wgetopter_t w;
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 0: {
break;
}
case 'b': {
bgcolor = w.woptarg;
break;
@ -128,7 +119,7 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_INVALID_ARGS;
}
default: {
DIE("unexpected opt");
DIE("unexpected retval from wgetopt_long");
break;
}
}