From 4fdc29a88b9923d874375c72774a01a91a7fc253 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Feb 2016 12:41:51 -0500 Subject: Add brief -help and -version support. --- bfs.h | 3 +++ eval.c | 4 ++++ parse.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/bfs.h b/bfs.h index 36b12e4..1b3457a 100644 --- a/bfs.h +++ b/bfs.h @@ -18,6 +18,9 @@ #include #include +#define BFS_VERSION "0.0" +#define BFS_HOMEPAGE "https://github.com/tavianator/bfs" + /** * A command line expression. */ diff --git a/eval.c b/eval.c index 0f850fc..63c1348 100644 --- a/eval.c +++ b/eval.c @@ -568,6 +568,10 @@ static int infer_fdlimit() { * Evaluate the command line. */ int eval_cmdline(const struct cmdline *cmdline) { + if (!cmdline->expr) { + return 0; + } + int nopenfd = infer_fdlimit(); struct callback_args args = { diff --git a/parse.c b/parse.c index f894d5b..6177e76 100644 --- a/parse.c +++ b/parse.c @@ -144,6 +144,8 @@ struct parser_state { bool warn; /** Whether any non-option arguments have been encountered. */ bool non_option_seen; + /** Whether an information option like -help or -version was passed. */ + bool just_info; /** The current time. */ struct timespec now; @@ -520,6 +522,37 @@ static struct expr *parse_type(struct parser_state *state, const char *option, e return new_test_idata(state, eval, typeflag); } +/** + * "Parse" -help. + */ +static struct expr *parse_help(struct parser_state *state) { + printf("Usage: %s [arguments...]\n\n", state->argv[0]); + + printf("bfs is compatible with find; see find -help or man find for help with find-\n" + "compatible options :)\n\n"); + + printf("Extra features:\n" + " -color, -nocolor: Turn on or off file type colorization.\n\n" + " -hidden, -nohidden: Match hidden files, or filter them out.\n\n"); + + printf("%s\n", BFS_HOMEPAGE); + + state->just_info = true; + return NULL; +} + +/** + * "Parse" -version. + */ +static struct expr *parse_version(struct parser_state *state) { + printf("bfs %s\n\n", BFS_VERSION); + + printf("%s\n", BFS_HOMEPAGE); + + state->just_info = true; + return NULL; +} + /** * LITERAL : OPTION * | TEST @@ -616,7 +649,9 @@ static struct expr *parse_literal(struct parser_state *state) { break; case 'h': - if (strcmp(arg, "-hidden") == 0) { + if (strcmp(arg, "-help") == 0) { + return parse_help(state); + } else if (strcmp(arg, "-hidden") == 0) { return new_test(state, eval_hidden); } break; @@ -693,6 +728,7 @@ static struct expr *parse_literal(struct parser_state *state) { if (strcmp(arg, "-samefile") == 0) { return parse_samefile(state, arg); } + break; case 't': if (strcmp(arg, "-true") == 0) { @@ -708,6 +744,12 @@ static struct expr *parse_literal(struct parser_state *state) { } break; + case 'v': + if (strcmp(arg, "-version") == 0) { + return parse_version(state); + } + break; + case 'w': if (strcmp(arg, "-warn") == 0) { state->warn = true; @@ -723,6 +765,15 @@ static struct expr *parse_literal(struct parser_state *state) { if (strcmp(arg, "-xtype") == 0) { return parse_type(state, arg, eval_xtype); } + break; + + case '-': + if (strcmp(arg, "--help") == 0) { + return parse_help(state); + } else if (strcmp(arg, "--version") == 0) { + return parse_version(state); + } + break; } fprintf(stderr, "Unknown argument '%s'.\n", arg); @@ -955,6 +1006,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { .implicit_print = true, .warn = true, .non_option_seen = false, + .just_info = false, }; if (clock_gettime(CLOCK_REALTIME, &state.now) != 0) { @@ -965,7 +1017,11 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { if (skip_paths(&state)) { cmdline->expr = parse_expr(&state); if (!cmdline->expr) { - goto fail; + if (state.just_info) { + goto done; + } else { + goto fail; + } } } @@ -996,6 +1052,7 @@ struct cmdline *parse_cmdline(int argc, char *argv[]) { cmdline->colors = parse_colors(getenv("LS_COLORS")); } +done: return cmdline; fail: -- cgit v1.2.3