cli: refactor common options

Move the common option information to a global place, and reuse them.
Common options will be global variables.

Specify them as _hidden_ for all commands (the main command will pass
the SHOW_HIDDEN flag to the usage printer, so that they're visible).
This commit is contained in:
Edward Thomson
2024-10-21 14:57:55 +01:00
parent fe66d93b0e
commit da6b76d89e
9 changed files with 27 additions and 49 deletions

View File

@@ -22,7 +22,6 @@
static char *file;
static int porcelain, line_porcelain;
static int show_help;
static const cli_opt_spec opts[] = {
CLI_COMMON_OPT,
@@ -254,7 +253,7 @@ int cmd_blame(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -19,7 +19,6 @@ typedef enum {
DISPLAY_TYPE
} display_t;
static int show_help;
static int display = DISPLAY_CONTENT;
static char *type_name, *object_spec;
@@ -147,7 +146,7 @@ int cmd_cat_file(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -19,7 +19,7 @@
#define COMMAND_NAME "clone"
static char *branch, *remote_path, *local_path, *depth;
static int show_help, quiet, checkout = 1, bare;
static int quiet, checkout = 1, bare;
static bool local_path_exists;
static cli_progress progress = CLI_PROGRESS_INIT;
@@ -133,7 +133,7 @@ int cmd_clone(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -23,7 +23,6 @@ typedef enum {
static action_t action = ACTION_NONE;
static int show_origin;
static int show_scope;
static int show_help;
static int null_separator;
static int config_level;
static char *config_filename;
@@ -180,7 +179,7 @@ int cmd_config(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -13,7 +13,6 @@
#define COMMAND_NAME "hash-object"
static int show_help;
static char *type_name;
static int write_object, read_stdin, literally;
static char **filenames;
@@ -103,7 +102,7 @@ int cmd_hash_object(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -13,7 +13,6 @@
#define COMMAND_NAME "help"
static char *command;
static int show_help;
static const cli_opt_spec opts[] = {
CLI_COMMON_OPT,
@@ -62,7 +61,7 @@ int cmd_help(int argc, char **argv)
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
/* Show the meta-help */
if (show_help)
if (cli_opt__show_help)
return print_help();
/* We were not asked to show help for a specific command. */

View File

@@ -14,14 +14,12 @@
#define BUFFER_SIZE (1024 * 1024)
static int show_help, verbose, read_stdin;
static int verbose, read_stdin;
static char *filename;
static cli_progress progress = CLI_PROGRESS_INIT;
static const cli_opt_spec opts[] = {
{ CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1,
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, NULL,
"display help about the " COMMAND_NAME " command" },
CLI_COMMON_OPT,
{ CLI_OPT_TYPE_SWITCH, "verbose", 'v', &verbose, 1,
CLI_OPT_USAGE_DEFAULT, NULL, "display progress output" },
@@ -62,7 +60,7 @@ int cmd_index_pack(int argc, char **argv)
if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);
if (show_help) {
if (cli_opt__show_help) {
print_help();
return 0;
}

View File

@@ -20,15 +20,20 @@
* Common command arguments.
*/
extern int cli_opt__show_help;
#define CLI_COMMON_OPT_HELP \
CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, \
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING
CLI_OPT_TYPE_SWITCH, "help", 0, &cli_opt__show_help, 1, \
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, \
NULL, "display help information"
#define CLI_COMMON_OPT_CONFIG \
CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \
CLI_OPT_USAGE_HIDDEN
CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \
CLI_OPT_USAGE_HIDDEN, \
"key=value", "add configuration value"
#define CLI_COMMON_OPT_CONFIG_ENV \
CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \
CLI_OPT_USAGE_HIDDEN
CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \
CLI_OPT_USAGE_HIDDEN, \
"key=value", "set configuration value to environment variable"
#define CLI_COMMON_OPT \
{ CLI_COMMON_OPT_HELP }, \
@@ -49,23 +54,4 @@ extern int cli_resolve_path(
git_repository *repo,
const char *given_path);
/*
* Common command arguments.
*/
#define CLI_COMMON_OPT_HELP \
CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, \
CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING
#define CLI_COMMON_OPT_CONFIG \
CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0, \
CLI_OPT_USAGE_HIDDEN
#define CLI_COMMON_OPT_CONFIG_ENV \
CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0, \
CLI_OPT_USAGE_HIDDEN
#define CLI_COMMON_OPT \
{ CLI_COMMON_OPT_HELP }, \
{ CLI_COMMON_OPT_CONFIG }, \
{ CLI_COMMON_OPT_CONFIG_ENV }
#endif /* CLI_common_h__ */

View File

@@ -10,18 +10,15 @@
#include "common.h"
#include "cmd.h"
static int show_help = 0;
int cli_opt__show_help = 0;
static int show_version = 0;
static char *command = NULL;
static char **args = NULL;
const cli_opt_spec cli_common_opts[] = {
{ CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1,
CLI_OPT_USAGE_DEFAULT, NULL, "display help information" },
{ CLI_OPT_TYPE_VALUE, NULL, 'c', NULL, 0,
CLI_OPT_USAGE_DEFAULT, "key=value", "add configuration value" },
{ CLI_OPT_TYPE_VALUE, "config-env", 0, NULL, 0,
CLI_OPT_USAGE_DEFAULT, "key=value", "set configuration value to environment variable" },
CLI_COMMON_OPT,
{ CLI_OPT_TYPE_SWITCH, "version", 0, &show_version, 1,
CLI_OPT_USAGE_DEFAULT, NULL, "display the version" },
{ CLI_OPT_TYPE_ARG, "command", 0, &command, 0,
@@ -71,6 +68,8 @@ static void reorder_args(char **argv, size_t first)
*/
static void help_args(int *argc, char **argv)
{
cli_opt__show_help = 0;
argv[0] = "help";
*argc = 1;
}