From bc85432aa33173eb1cd7c7a530d1565e4ee352c4 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Fri, 23 Dec 2011 02:56:27 -0200 Subject: [PATCH] tools: kmod: Add global options --- tools/kmod.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/tools/kmod.c b/tools/kmod.c index ec0f573..a16863a 100644 --- a/tools/kmod.c +++ b/tools/kmod.c @@ -20,10 +20,18 @@ #include #include #include +#include #include #include #include "kmod.h" +static const char options_s[] = "+hV"; +static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + {} +}; + static const struct kmod_cmd kmod_cmd_help; static const struct kmod_cmd *kmod_cmds[] = { @@ -34,9 +42,13 @@ static int kmod_help(int argc, char *argv[]) { size_t i; - puts("Manage kernel modules: list, load, unload, etc\n" - "Usage: kmod COMMAND [COMMAND_OPTIONS]\n\n" - "Available commands:"); + printf("Manage kernel modules: list, load, unload, etc\n" + "Usage:\n" + "\t%s [options] command [command_options]\n\n" + "Options:\n" + "\t-V, --version show version\n" + "\t-h, --help show this help\n\n" + "Commands:\n", basename(argv[0])); for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { if (kmod_cmds[i]->help != NULL) { @@ -60,12 +72,34 @@ int main(int argc, char *argv[]) int err = 0; size_t i; - if (argc < 2) { + for (;;) { + int c; + + c = getopt_long(argc, argv, options_s, options, NULL); + if (c == -1) + break; + + switch (c) { + case 'h': + kmod_help(argc, argv); + return EXIT_SUCCESS; + case 'V': + puts("kmod version " VERSION); + return EXIT_SUCCESS; + case '?': + return EXIT_FAILURE; + default: + fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n", c); + return EXIT_FAILURE; + } + } + + if (optind >= argc) { err = -ENOENT; goto finish; } - cmd = argv[1]; + cmd = argv[optind]; for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { if (strcmp(kmod_cmds[i]->name, cmd) != 0) -- 2.7.4