function: Add --help option to func subcomands
authorPawel Szewczyk <p.szewczyk@samsung.com>
Thu, 25 Jun 2015 10:36:05 +0000 (12:36 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Tue, 7 Jul 2015 11:56:43 +0000 (13:56 +0200)
Using 'help' subcommand is not always good option, e.g. if you want to
create a gadget named 'help'. Instead --help option can be used,
providing similar syntax for all commands.

Change-Id: I645e545c518dff09b680d8f63794d855f56d1a11
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
source/base/include/parser.h
source/base/src/parser.c
source/function/include/function.h
source/function/src/function.c

index 77fec5c..3f53a5d 100644 (file)
@@ -82,7 +82,8 @@ enum gt_option_flags {
        GT_VERBOSE = 4,
        GT_OFF = 8,
        GT_STDIN = 16,
-       GT_STDOUT = 32
+       GT_STDOUT = 32,
+       GT_HELP = 64,
 };
 
 /**
index 4ab100b..73194ca 100644 (file)
@@ -187,6 +187,7 @@ int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv)
                {GT_OFF, {"off", no_argument, 0, 'o'}},
                {GT_STDIN, {"stdin", no_argument, 0, 1}},
                {GT_STDIN, {"stdout", no_argument, 0, 2}},
+               {GT_HELP, {"help", no_argument, 0, 'h'}},
                {0, {NULL, 0, 0, 0}}
        };
 
@@ -237,6 +238,9 @@ int gt_get_options(int *optmask, int allowed_opts, int argc, char **argv)
                case 2:
                        *optmask |= GT_STDOUT;
                        break;
+               case 'h':
+                       *optmask |= GT_HELP;
+                       break;
                default:
                        return -1;
                }
index a51fd21..f0a21ed 100644 (file)
@@ -95,6 +95,7 @@ struct gt_func_get_data {
        const char *type;
        const char *name;
        const char **attrs;
+       int opts;
 };
 
 struct gt_func_set_data {
@@ -102,6 +103,7 @@ struct gt_func_set_data {
        const char *type;
        const char *name;
        struct gt_setting *attrs;
+       int opts;
 };
 
 struct gt_func_func_data {
@@ -138,11 +140,13 @@ struct gt_func_template_data {
 struct gt_func_template_get_data {
        const char *name;
        const char **attrs;
+       int opts;
 };
 
 struct gt_func_template_set_data {
        const char *name;
        struct gt_setting *attrs;
+       int opts;
 };
 
 /**
index 7a1d426..6a42310 100644 (file)
@@ -56,14 +56,14 @@ static void gt_parse_func_create(const Command *cmd, int argc,
        struct gt_func_create_data *dt = NULL;
        int ind;
        int tmp;
-       int avaible_opts = GT_FORCE;
+       int avaible_opts = GT_FORCE | GT_HELP;
 
        dt = zalloc(sizeof(*dt));
        if (dt == NULL)
                goto out;
 
        ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
-       if (ind < 0)
+       if (ind < 0 || dt->opts & GT_HELP)
                goto out;
 
        if (argc - ind < 3)
@@ -109,14 +109,14 @@ static void gt_parse_func_rm(const Command *cmd, int argc,
 {
        struct gt_func_rm_data *dt = NULL;
        int ind;
-       int avaible_opts = GT_FORCE | GT_RECURSIVE;
+       int avaible_opts = GT_FORCE | GT_RECURSIVE | GT_HELP;
 
        dt = zalloc(sizeof(*dt));
        if (dt == NULL)
                goto out;
 
        ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
-       if (ind < 0)
+       if (ind < 0 || dt->opts & GT_HELP)
                goto out;
 
        if (argc - ind < 3)
@@ -181,6 +181,7 @@ static void gt_parse_func_get(const Command *cmd, int argc,
        struct gt_func_get_data *dt = NULL;
        int ind = 0;
        int i;
+       int avaible_opts = GT_HELP;
 
        if (argc < 3)
                goto out;
@@ -189,6 +190,10 @@ static void gt_parse_func_get(const Command *cmd, int argc,
        if (dt == NULL)
                goto out;
 
+       ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+       if (ind < 0 || dt->opts & GT_HELP)
+               goto out;
+
        dt->gadget = argv[ind++];
        dt->type = argv[ind++];
        dt->name = argv[ind++];
@@ -234,6 +239,7 @@ static void gt_parse_func_set(const Command *cmd, int argc,
        struct gt_func_set_data *dt = NULL;
        int ind = 0;
        int tmp;
+       int avaible_opts = GT_HELP;
 
        if (argc < 3)
                goto out;
@@ -242,6 +248,10 @@ static void gt_parse_func_set(const Command *cmd, int argc,
        if (dt == NULL)
                goto out;
 
+       ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+       if (ind < 0 || dt->opts & GT_HELP)
+               goto out;
+
        dt->gadget = argv[ind++];
        dt->type = argv[ind++];
        dt->name = argv[ind++];
@@ -296,7 +306,7 @@ static void gt_parse_func_func(const Command *cmd, int argc,
 {
        struct gt_func_func_data *dt = NULL;
        int ind;
-       int avaible_opts = GT_VERBOSE;
+       int avaible_opts = GT_VERBOSE | GT_HELP;
 
        if (argc < 1)
                goto out;
@@ -306,7 +316,7 @@ static void gt_parse_func_func(const Command *cmd, int argc,
                goto out;
 
        ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
-       if (ind < 0)
+       if (ind < 0 || dt->opts & GT_HELP)
                goto out;
 
        dt->gadget = argv[ind++];
@@ -343,6 +353,7 @@ static void gt_parse_func_load(const Command *cmd, int argc,
                        {"file", required_argument, 0, 1},
                        {"stdin", no_argument, 0, 2},
                        {"path", required_argument, 0, 3},
+                       {"help", no_argument, 0, 'h'},
                        {0, 0, 0, 0}
                };
 
@@ -354,7 +365,7 @@ static void gt_parse_func_load(const Command *cmd, int argc,
        argc++;
        while (1) {
                int opt_index = 0;
-               c = getopt_long(argc, argv, "f", opts, &opt_index);
+               c = getopt_long(argc, argv, "frh", opts, &opt_index);
                if (c == -1)
                        break;
 
@@ -377,6 +388,9 @@ static void gt_parse_func_load(const Command *cmd, int argc,
                                goto out;
                        dt->path = optarg;
                        break;
+               case 'h':
+                       goto out;
+                       break;
                default:
                        goto out;
                }
@@ -433,6 +447,7 @@ static void gt_parse_func_save(const Command *cmd, int argc,
                        {"file", required_argument, 0, 1},
                        {"stdout", no_argument, 0, 2},
                        {"path", required_argument, 0, 3},
+                       {"help", no_argument, 0, 'h'},
                        {0, 0, 0, 0}
                };
 
@@ -444,7 +459,7 @@ static void gt_parse_func_save(const Command *cmd, int argc,
        argc++;
        while (1) {
                int opt_index = 0;
-               c = getopt_long(argc, argv, "f", opts, &opt_index);
+               c = getopt_long(argc, argv, "fh", opts, &opt_index);
                if (c == -1)
                        break;
 
@@ -467,6 +482,9 @@ static void gt_parse_func_save(const Command *cmd, int argc,
                                goto out;
                        dt->path = optarg;
                        break;
+               case 'h':
+                       goto out;
+                       break;
                default:
                        goto out;
                }
@@ -512,7 +530,7 @@ static void gt_parse_func_template(const Command *cmd, int argc,
                char **argv, ExecutableCommand *exec, void * data)
 {
        struct gt_func_template_data *dt;
-       int avaible_opts = GT_VERBOSE;
+       int avaible_opts = GT_VERBOSE | GT_HELP;
        int ind;
 
        dt = zalloc(sizeof(*dt));
@@ -520,7 +538,7 @@ static void gt_parse_func_template(const Command *cmd, int argc,
                goto out;
 
        ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
-       if (ind < 0)
+       if (ind < 0 || dt->opts & GT_HELP)
                goto out;
 
        if (argc - ind > 1)
@@ -558,6 +576,8 @@ static void gt_parse_func_template_get(const Command *cmd, int argc,
 {
        struct gt_func_template_get_data *dt = NULL;
        int i;
+       int ind = 0;
+       int avaible_opts = GT_HELP;
 
        if (argc < 1)
                goto out;
@@ -566,7 +586,11 @@ static void gt_parse_func_template_get(const Command *cmd, int argc,
        if (dt == NULL)
                goto out;
 
-       dt->name = argv[0];
+       ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+       if (ind < 0 || dt->opts & GT_HELP)
+               goto out;
+
+       dt->name = argv[ind++];
 
        dt->attrs = calloc(argc, sizeof(char *));
        if (dt->attrs == NULL)
@@ -607,6 +631,8 @@ static void gt_parse_func_template_set(const Command *cmd, int argc,
 {
        struct gt_func_template_set_data *dt = NULL;
        int tmp;
+       int ind = 0;
+       int avaible_opts = GT_HELP;
 
        if (argc < 2)
                goto out;
@@ -615,8 +641,12 @@ static void gt_parse_func_template_set(const Command *cmd, int argc,
        if (dt == NULL)
                goto out;
 
-       dt->name = argv[0];
-       tmp = gt_parse_setting_list(&dt->attrs, argc - 1, argv + 1);
+       ind = gt_get_options(&dt->opts, avaible_opts, argc, argv);
+       if (ind < 0 || dt->opts & GT_HELP)
+               goto out;
+
+       dt->name = argv[ind++];
+       tmp = gt_parse_setting_list(&dt->attrs, argc - ind, argv + ind);
        if (tmp < 0)
                goto out;
 
@@ -639,11 +669,18 @@ static void gt_parse_func_template_rm(const Command *cmd, int argc,
                char **argv, ExecutableCommand *exec, void * data)
 {
        const char *name;
+       int ind = 0;
+       int avaible_opts = GT_HELP;
+       int opts = 0;
 
        if (argc != 1)
                goto out;
 
-       name = argv[0];
+       ind = gt_get_options(&opts, avaible_opts, argc, argv);
+       if (ind < 0 || opts & GT_HELP)
+               goto out;
+
+       name = argv[ind];
 
        executable_command_set(exec, GET_EXECUTABLE(template_rm), (void *)name, NULL);