diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2018-01-26 15:58:53 +0000 |
---|---|---|
committer | P. F. Chimento <philip.chimento@gmail.com> | 2018-03-21 12:14:59 -0700 |
commit | 80408632f1322a44b3094f3dfb6ee6afcdef13dc (patch) | |
tree | b40420f6c239a906f8609ddb040d1b564d35981b | |
parent | 86a999c7f9807d2c369e4658d7e14a8a4257b0b5 (diff) |
Programmatically generate command usage and help
This avoids having each command handle it differently.
-rw-r--r-- | tools/eos-profile-tool/eos-profile-cmd-help.c | 23 | ||||
-rw-r--r-- | tools/eos-profile-tool/eos-profile-cmd-show.c | 2 | ||||
-rw-r--r-- | tools/eos-profile-tool/eos-profile-cmds.h | 14 | ||||
-rw-r--r-- | tools/eos-profile-tool/eos-profile-main.c | 47 |
4 files changed, 65 insertions, 21 deletions
diff --git a/tools/eos-profile-tool/eos-profile-cmd-help.c b/tools/eos-profile-tool/eos-profile-cmd-help.c index 754714f..f81102d 100644 --- a/tools/eos-profile-tool/eos-profile-cmd-help.c +++ b/tools/eos-profile-tool/eos-profile-cmd-help.c @@ -9,20 +9,33 @@ eos_profile_cmd_help_parse_args (int argc, return TRUE; } +static gboolean +print_available_commands (const EosProfileCmd *cmd, + gpointer dummy G_GNUC_UNUSED) +{ + g_print (" %s%*s%s\n", + cmd->name, + 14 - strlen (cmd->name), " ", + cmd->description); + + return FALSE; +} + int eos_profile_cmd_help_main (void) { g_print ( "eos-profile\n" "\n" - "Usage: eos-profile <COMMAND> [OPTIONS...]\n" + "Usage: eos-profile <COMMAND> [OPTION…]\n" "\n" - "Examples:\n" - "\n" - " eos-profile help - This help screen\n" - " eos-profile show FILE - Shows a capture file\n" + "COMMANDS\n" "\n" ); + eos_profile_foreach_cmd (print_available_commands, NULL); + + g_print ("\n"); + return 0; } diff --git a/tools/eos-profile-tool/eos-profile-cmd-show.c b/tools/eos-profile-tool/eos-profile-cmd-show.c index b01bd14..4867dc2 100644 --- a/tools/eos-profile-tool/eos-profile-cmd-show.c +++ b/tools/eos-profile-tool/eos-profile-cmd-show.c @@ -57,9 +57,7 @@ eos_profile_cmd_show_parse_args (int argc, if (files->len == 0) { - g_printerr ("Usage: eos-profile show FILE [FILE...]\n"); g_ptr_array_unref (files); - return FALSE; } diff --git a/tools/eos-profile-tool/eos-profile-cmds.h b/tools/eos-profile-tool/eos-profile-cmds.h index c6da7d4..1b06db5 100644 --- a/tools/eos-profile-tool/eos-profile-cmds.h +++ b/tools/eos-profile-tool/eos-profile-cmds.h @@ -5,9 +5,23 @@ typedef gboolean (* EosProfileCmdParseArgs) (int argc, char **argv); typedef int (* EosProfileCmdMain) (void); +typedef struct { + const char *name; + const char *description; + const char *usage; + + EosProfileCmdParseArgs parse_args; + EosProfileCmdMain main; +} EosProfileCmd; + +typedef gboolean (* EosProfileCmdCallback) (const EosProfileCmd *cmd, + gpointer data); + gboolean eos_profile_cmd_help_parse_args (int argc, char **argv); int eos_profile_cmd_help_main (void); gboolean eos_profile_cmd_show_parse_args (int argc, char **argv); int eos_profile_cmd_show_main (void); +void eos_profile_foreach_cmd (EosProfileCmdCallback cb, + gpointer data); diff --git a/tools/eos-profile-tool/eos-profile-main.c b/tools/eos-profile-tool/eos-profile-main.c index 6879cb6..1691bff 100644 --- a/tools/eos-profile-tool/eos-profile-main.c +++ b/tools/eos-profile-tool/eos-profile-main.c @@ -10,29 +10,34 @@ #include "eos-profile-cmds.h" -static const struct { - const char *name; - const char *description; - - EosProfileCmdParseArgs parse_args; - EosProfileCmdMain main; -} profile_commands[] = { +static const EosProfileCmd profile_commands[] = { { .name = "help", - .description = "Prints help", + .description = "Prints this help string", + .usage = NULL, .parse_args = eos_profile_cmd_help_parse_args, .main = eos_profile_cmd_help_main, }, { .name = "show", - .description = "Shows a capture", + .description = "Prints a report from a capture file", + .usage = "show <FILE> [FILE…]", .parse_args = eos_profile_cmd_show_parse_args, .main = eos_profile_cmd_show_main, }, - - { NULL, }, }; +void +eos_profile_foreach_cmd (EosProfileCmdCallback cb, + gpointer data) +{ + for (int i = 0; i < G_N_ELEMENTS (profile_commands); i++) + { + if (cb (&profile_commands[i], data)) + break; + } +} + int main (int argc, char *argv[]) @@ -45,8 +50,13 @@ main (int argc, const char *cmd = NULL; - if (argc < 2) - cmd = "help"; + if (argc < 2 || + g_strcmp0 (argv[1], "-h") == 0 || + g_strcmp0 (argv[1], "-?") == 0 || + g_strcmp0 (argv[1], "--help") == 0) + { + cmd = "help"; + } else cmd = argv[1]; @@ -58,7 +68,16 @@ main (int argc, argv += 1; if (!profile_commands[i].parse_args (argc, argv)) - return EXIT_FAILURE; + { + const char *usage = profile_commands[i].usage; + + if (usage == NULL) + usage = profile_commands[i].name; + + g_printerr ("Usage: eos-profile %s\n", usage); + + return EXIT_FAILURE; + } return profile_commands[i].main (); } |