From 03ab4da2ac19a6cb7653da9a250319f4dff963e9 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 6 Sep 2016 21:26:32 -0400 Subject: Give suggestions for operators too. --- parse.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/parse.c b/parse.c index 15e0213..6d55317 100644 --- a/parse.c +++ b/parse.c @@ -1398,7 +1398,9 @@ static const struct table_entry parse_table[] = { {"P", false, parse_follow, 0, false}, {"H", false, parse_follow, BFTW_FOLLOW_ROOT, false}, {"L", false, parse_follow, BFTW_FOLLOW | BFTW_DETECT_CYCLES, false}, + {"a"}, {"amin", false, parse_acmtime, ATIME, MINUTES}, + {"and"}, {"atime", false, parse_acmtime, ATIME, DAYS}, {"anewer", false, parse_acnewer, ATIME}, {"cmin", false, parse_acmtime, CTIME, MINUTES}, @@ -1440,9 +1442,12 @@ static const struct table_entry parse_table[] = { {"nocolor", false, parse_color, false}, {"nohidden", false, parse_nohidden}, {"noleaf", false, parse_noleaf}, + {"not"}, {"nowarn", false, parse_warn, false}, + {"o"}, {"ok", false, parse_exec, EXEC_CONFIRM}, {"okdir", false, parse_exec, EXEC_CONFIRM | EXEC_CHDIR}, + {"or"}, {"path", false, parse_path, false}, {"print", false, parse_print}, {"print0", false, parse_print0}, @@ -1467,23 +1472,8 @@ static const struct table_entry parse_table[] = { {0}, }; -/** - * LITERAL : OPTION - * | TEST - * | ACTION - */ -static struct expr *parse_literal(struct parser_state *state) { - struct cmdline *cmdline = state->cmdline; - - // Paths are already skipped at this point - const char *arg = state->argv[0]; - - if (*arg++ != '-') { - pretty_error(cmdline->stderr_colors, - "error: Expected a predicate; found '%s'.\n", arg); - return NULL; - } - +/** Look up an argument in the parse table. */ +static const struct table_entry *table_lookup(const char *arg) { for (const struct table_entry *entry = parse_table; entry->arg; ++entry) { bool match; if (entry->prefix) { @@ -1492,12 +1482,18 @@ static struct expr *parse_literal(struct parser_state *state) { match = strcmp(arg, entry->arg) == 0; } if (match) { - return entry->parse(state, entry->arg1, entry->arg2); + return entry; } } + return NULL; +} + +/** Search for a fuzzy match in the parse table. */ +static const struct table_entry *table_lookup_fuzzy(const char *arg) { const struct table_entry *best = NULL; int best_dist; + for (const struct table_entry *entry = parse_table; entry->arg; ++entry) { int dist = typo_distance(arg, entry->arg); if (!best || dist < best_dist) { @@ -1506,8 +1502,40 @@ static struct expr *parse_literal(struct parser_state *state) { } } + return best; +} + +/** + * LITERAL : OPTION + * | TEST + * | ACTION + */ +static struct expr *parse_literal(struct parser_state *state) { + struct cmdline *cmdline = state->cmdline; + + // Paths are already skipped at this point + const char *arg = state->argv[0]; + + if (arg[0] != '-') { + goto unexpected; + } + + const struct table_entry *match = table_lookup(arg + 1); + if (match) { + if (!match->parse) { + goto unexpected; + } + return match->parse(state, match->arg1, match->arg2); + } + + match = table_lookup_fuzzy(arg + 1); + pretty_error(cmdline->stderr_colors, + "error: Unknown argument '%s'; did you mean '-%s'?\n", arg, match->arg); + return NULL; + +unexpected: pretty_error(cmdline->stderr_colors, - "error: Unknown argument '-%s'; did you mean '-%s'?\n", arg, best->arg); + "error: Expected a predicate; found '%s'.\n", arg); return NULL; } -- cgit v1.2.3