diff options
Diffstat (limited to 'src/Cli/Cli.c')
| -rw-r--r-- | src/Cli/Cli.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/Cli/Cli.c b/src/Cli/Cli.c index 3648493..c7569da 100644 --- a/src/Cli/Cli.c +++ b/src/Cli/Cli.c @@ -16,6 +16,8 @@ void cli_print_usage(const char *program_name) { " -d, --directory DIRECTORY Process a directory recursively\n" " -o, --output OUTPUT Set the output path\n" " -f, --overwrite Overwrite an existing output directory\n" + " --include PATTERN Keep entries matching PATTERN\n" + " --exclude PATTERN Drop entries matching PATTERN\n" " -l, --log-level LEVEL error, warning, info, debug, or silent\n" " -v, --version Show the program version\n" " -h, --help Show this help\n", @@ -28,12 +30,29 @@ static const char *strip_optional_equals(const char *value) { return value != NULL && value[0] == '=' ? value + 1 : value; } +static CliParseResult append_pattern(CliOptions *options, PatternKind kind, + const char *value, const char *name) { + const char *pattern = strip_optional_equals(value); + + if (pattern_list_append(&options->patterns, kind, pattern) != 0) { + logger_error("invalid %s pattern '%s'", name, pattern); + return CLI_PARSE_ERROR; + } + return CLI_PARSE_OK; +} + +void cli_free_options(CliOptions *options) { + pattern_list_clear(&options->patterns); +} + CliParseResult cli_parse(int argc, char **argv, CliOptions *options) { static const struct option long_options[] = { {"directory", required_argument, NULL, 'd'}, {"output", required_argument, NULL, 'o'}, {"overwrite", no_argument, NULL, 'f'}, {"force", no_argument, NULL, 'f'}, + {"include", required_argument, NULL, 1}, + {"exclude", required_argument, NULL, 2}, {"log-level", required_argument, NULL, 'l'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, @@ -46,6 +65,9 @@ CliParseResult cli_parse(int argc, char **argv, CliOptions *options) { options->output_path = NULL; options->overwrite_output = 0; options->log_level = LOG_LEVEL_INFO; + options->patterns.items = NULL; + options->patterns.length = 0; + options->patterns.capacity = 0; while ((option = getopt_long(argc, argv, "d:o:fl:vh", long_options, NULL)) != -1) { @@ -59,6 +81,18 @@ CliParseResult cli_parse(int argc, char **argv, CliOptions *options) { case 'f': options->overwrite_output = 1; break; + case 1: + if (append_pattern(options, PATTERN_INCLUDE, optarg, "include") != + CLI_PARSE_OK) { + return CLI_PARSE_ERROR; + } + break; + case 2: + if (append_pattern(options, PATTERN_EXCLUDE, optarg, "exclude") != + CLI_PARSE_OK) { + return CLI_PARSE_ERROR; + } + break; case 'l': if (logger_parse_level(optarg, &options->log_level) != 0) { logger_error("unknown log level '%s'", optarg); |
