]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[cmdline] Generate command option help text automatically
authorMichael Brown <mcb30@ipxe.org>
Thu, 7 Nov 2013 17:00:51 +0000 (17:00 +0000)
committerMichael Brown <mcb30@ipxe.org>
Thu, 7 Nov 2013 17:00:51 +0000 (17:00 +0000)
Generate the command option help text automatically from the list of
defined options.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
22 files changed:
src/arch/i386/hci/commands/pxe_cmd.c
src/arch/x86/hci/commands/cpuid_cmd.c
src/core/exec.c
src/core/parseopt.c
src/hci/commands/fcmgmt_cmd.c
src/hci/commands/ifmgmt_cmd.c
src/hci/commands/image_cmd.c
src/hci/commands/image_trust_cmd.c
src/hci/commands/login_cmd.c
src/hci/commands/lotest_cmd.c
src/hci/commands/menu_cmd.c
src/hci/commands/neighbour_cmd.c
src/hci/commands/param_cmd.c
src/hci/commands/ping_cmd.c
src/hci/commands/poweroff_cmd.c
src/hci/commands/reboot_cmd.c
src/hci/commands/route_cmd.c
src/hci/commands/sanboot_cmd.c
src/hci/commands/sync_cmd.c
src/hci/commands/vlan_cmd.c
src/hci/shell.c
src/image/script.c

index c7cdb07f87536d1681b2ca0bec4642b8da4085e9..523724ab05b2daeb75dcc8724a29626a432e8ed7 100644 (file)
@@ -78,7 +78,7 @@ static struct option_descriptor stoppxe_opts[] = {};
 
 /** "stoppxe" command descriptor */
 static struct command_descriptor stoppxe_cmd =
-       COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0, "" );
+       COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0, NULL );
 
 /**
  * The "stoppxe" command
index 981143308a925957a5bb5755b60f2348bdcf68a6..c4e35d17954992adb1adb2ce9aed341cc9b6d6c7 100644 (file)
@@ -54,8 +54,7 @@ static struct option_descriptor cpuid_opts[] = {
 
 /** "cpuid" command descriptor */
 static struct command_descriptor cpuid_cmd =
-       COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1,
-                      "[--ext] [--ecx] <bit>" );
+       COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1, "<bit>" );
 
 /**
  * The "cpuid" command
index 843a51db7a014909944df9342cf8beeb9c35b3dd..1c85705aec61dc11802a77c4b353af0f75457a5a 100644 (file)
@@ -397,7 +397,7 @@ static struct option_descriptor echo_opts[] = {
 /** "echo" command descriptor */
 static struct command_descriptor echo_cmd =
        COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
-                      "[-n] [...]" );
+                      "[...]" );
 
 /**
  * "echo" command
index 334ffb2cf50333acd1ac712f5320c72fa338ef34..d268c05947ad6f20e0eecbf086c92f4143bfb8cc 100644 (file)
@@ -326,8 +326,25 @@ int parse_parameters ( char *text, struct parameters **params ) {
  * @v argv             Argument list
  */
 void print_usage ( struct command_descriptor *cmd, char **argv ) {
-       printf ( "Usage:\n\n  %s %s\n\nSee http://ipxe.org/cmd/%s for further "
-                "information\n", argv[0], cmd->usage, argv[0] );
+       struct option_descriptor *option;
+       unsigned int i;
+       int is_optional;
+
+       printf ( "Usage:\n\n  %s", argv[0] );
+       for ( i = 0 ; i < cmd->num_options ; i++ ) {
+               option = &cmd->options[i];
+               printf ( " [-%c|--%s", option->shortopt, option->longopt );
+               if ( option->has_arg ) {
+                       is_optional = ( option->has_arg == optional_argument );
+                       printf ( " %s<%s>%s", ( is_optional ? "[" : "" ),
+                                option->longopt, ( is_optional ? "]" : "" ) );
+               }
+               printf ( "]" );
+       }
+       if ( cmd->usage )
+               printf ( " %s", cmd->usage );
+       printf ( "\n\nSee http://ipxe.org/cmd/%s for further information\n",
+                argv[0] );
 }
 
 /**
index 99f76113e18ab71300cb83c379db3f3888d187ff..1c199b5dc3eeca54edefd989b0eeced7252aac6a 100644 (file)
@@ -106,7 +106,7 @@ static struct option_descriptor fcstat_opts[] = {};
 
 /** "fcstat" command descriptor */
 static struct command_descriptor fcstat_cmd =
-       COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, "" );
+       COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, NULL );
 
 /**
  * The "fcstat" command
@@ -151,8 +151,7 @@ static struct option_descriptor fcels_opts[] = {
 
 /** "fcels" command descriptor */
 static struct command_descriptor fcels_cmd =
-       COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1,
-                      "[--port <port>] [--id <peer port id>] <request>" );
+       COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1, "<request>" );
 
 /**
  * The "fcels" command
index f3d00ca387e465e77ac6631688dd3c0be3e839ad..5307c9423d998f91aa6d894c6db3f8842e417c8c 100644 (file)
@@ -226,9 +226,7 @@ static int ifconf_payload ( struct net_device *netdev,
 /** "ifconf" command descriptor */
 static struct ifcommon_command_descriptor ifconf_cmd =
        IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
-                               0, MAX_ARGUMENTS,
-                               "[--configurator <configurator>] "
-                               "[<interface>...]",
+                               0, MAX_ARGUMENTS, "[<interface>...]",
                                ifconf_payload, 1 );
 
 /**
index 17e22dcba48f325d2fcf4600b91eb90bfea3ca04..dc30b3f2478b06a2b36f02afe814a6b7a5e8870c 100644 (file)
@@ -47,22 +47,22 @@ struct imgsingle_options {
 };
 
 /** "img{single}" option list */
-static struct option_descriptor imgsingle_opts[] = {
-       OPTION_DESC ( "name", 'n', required_argument,
-                     struct imgsingle_options, name, parse_string ),
-       OPTION_DESC ( "replace", 'r', no_argument,
-                     struct imgsingle_options, replace, parse_flag ),
-       OPTION_DESC ( "autofree", 'a', no_argument,
-                     struct imgsingle_options, autofree, parse_flag ),
+static union {
+       /* "imgexec" takes all three options */
+       struct option_descriptor imgexec[3];
+       /* Other "img{single}" commands take only --name and --autofree */
+       struct option_descriptor imgsingle[2];
+} opts = {
+       .imgexec = {
+               OPTION_DESC ( "name", 'n', required_argument,
+                             struct imgsingle_options, name, parse_string ),
+               OPTION_DESC ( "autofree", 'a', no_argument,
+                             struct imgsingle_options, autofree, parse_flag ),
+               OPTION_DESC ( "replace", 'r', no_argument,
+                             struct imgsingle_options, replace, parse_flag ),
+       },
 };
 
-/** "img{single}" command descriptor */
-static struct command_descriptor imgsingle_cmd =
-       COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
-                      1, MAX_ARGUMENTS,
-                      "[--name <name>] [--autofree] "
-                      "<uri|image> [<arguments>...]" );
-
 /** An "img{single}" family command descriptor */
 struct imgsingle_descriptor {
        /** Command descriptor */
@@ -174,9 +174,8 @@ static int imgsingle_exec ( int argc, char **argv,
 
 /** "imgfetch" command descriptor */
 static struct command_descriptor imgfetch_cmd =
-       COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
-                      1, MAX_ARGUMENTS,
-                      "[--name <name>] [--autofree] <uri> [<arguments>...]" );
+       COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
+                      1, MAX_ARGUMENTS, "<uri> [<arguments>...]" );
 
 /** "imgfetch" family command descriptor */
 struct imgsingle_descriptor imgfetch_desc = {
@@ -207,9 +206,14 @@ static int imgselect ( struct image *image,
        return image_select ( image );
 }
 
+/** "imgselect" command descriptor */
+static struct command_descriptor imgselect_cmd =
+       COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
+                      1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
+
 /** "imgselect" family command descriptor */
 struct imgsingle_descriptor imgselect_desc = {
-       .cmd = &imgsingle_cmd,
+       .cmd = &imgselect_cmd,
        .acquire = imgacquire,
        .action = imgselect,
        .verb = "select",
@@ -228,10 +232,8 @@ static int imgselect_exec ( int argc, char **argv ) {
 
 /** "imgexec" command descriptor */
 static struct command_descriptor imgexec_cmd =
-       COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
-                      0, MAX_ARGUMENTS,
-                      "[--autofree] [--replace] "
-                      "[<uri|image> [<arguments>...]]" );
+       COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
+                      0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" );
 
 /**
  * "imgexec" command action
@@ -282,9 +284,14 @@ static int imgexec_exec ( int argc, char **argv) {
        return imgsingle_exec ( argc, argv, &imgexec_desc );
 }
 
+/** "imgargs" command descriptor */
+static struct command_descriptor imgargs_cmd =
+       COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
+                      1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
+
 /** "imgargs" family command descriptor */
 struct imgsingle_descriptor imgargs_desc = {
-       .cmd = &imgsingle_cmd,
+       .cmd = &imgargs_cmd,
        .acquire = imgacquire,
        .preaction = image_clear_cmdline,
 };
index ad238bff9a96c4d189c0139e32183211274425ac..ef4bbfa898c961c2f16bc53d2501c5333f652b68 100644 (file)
@@ -52,8 +52,7 @@ static struct option_descriptor imgtrust_opts[] = {
 
 /** "imgtrust" command descriptor */
 static struct command_descriptor imgtrust_cmd =
-       COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0,
-                      "[--allow] [--permanent]" );
+       COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
 
 /**
  * The "imgtrust" command
@@ -100,8 +99,7 @@ static struct option_descriptor imgverify_opts[] = {
 /** "imgverify" command descriptor */
 static struct command_descriptor imgverify_cmd =
        COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
-                      "[--signer <signer>] [--keep] <uri|image> "
-                      "<signature uri|image>" );
+                      "<uri|image> <signature uri|image>" );
 
 /**
  * The "imgverify" command
index bc23e6ff52c370f456f55f5f97151afd0be7ce00..f5db427d536ca2845764951cbdc12397f67bcb91 100644 (file)
@@ -39,7 +39,7 @@ static struct option_descriptor login_opts[] = {};
 
 /** "login" command descriptor */
 static struct command_descriptor login_cmd =
-       COMMAND_DESC ( struct login_options, login_opts, 0, 0, "" );
+       COMMAND_DESC ( struct login_options, login_opts, 0, 0, NULL );
 
 /**
  * "login" command
index 92f08887aedb06500c23b4fd0c8d577ac798c22b..0fa031bcbdeeebbd9a471bba9a4a6d4450f3b688 100644 (file)
@@ -50,8 +50,7 @@ static struct option_descriptor lotest_opts[] = {
 /** "lotest" command descriptor */
 static struct command_descriptor lotest_cmd =
        COMMAND_DESC ( struct lotest_options, lotest_opts, 2, 2,
-                      "[--mtu <mtu>] <sending interface> "
-                      "<receiving interface>" );
+                      "<sending interface> <receiving interface>" );
 
 /**
  * "lotest" command
index 6ff30083bc42c09e449acce6e1ea8460d39c83c9..66a6262e67808e44961779d882bee6e606a73cbf 100644 (file)
@@ -57,7 +57,7 @@ static struct option_descriptor menu_opts[] = {
 /** "menu" command descriptor */
 static struct command_descriptor menu_cmd =
        COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
-                      "[--name <name>] [--delete] [<title>]" );
+                      "[<title>]" );
 
 /**
  * The "menu" command
@@ -131,8 +131,7 @@ static struct option_descriptor item_opts[] = {
 /** "item" command descriptor */
 static struct command_descriptor item_cmd =
        COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
-                      "[--menu <menu>] [--key <key>] [--default] "
-                      "[<label>|--gap [<text>]]" );
+                      "[<label> [<text>]]" );
 
 /**
  * The "item" command
@@ -215,9 +214,7 @@ static struct option_descriptor choose_opts[] = {
 
 /** "choose" command descriptor */
 static struct command_descriptor choose_cmd =
-       COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
-                      "[--menu <menu>] [--default <label>] "
-                      "[--timeout <timeout>] [--keep] <setting>" );
+       COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
 
 /**
  * The "choose" command
index d65c355c6b1f05495dd2fdc90d3643ae80fdf49c..a1e052439a2def1403595c2dc7248c93f54d6582 100644 (file)
@@ -38,7 +38,7 @@ static struct option_descriptor nstat_opts[] = {};
 
 /** "nstat" command descriptor */
 static struct command_descriptor nstat_cmd =
-       COMMAND_DESC ( struct nstat_options, nstat_opts, 0, 0, "" );
+       COMMAND_DESC ( struct nstat_options, nstat_opts, 0, 0, NULL );
 
 /**
  * The "nstat" command
index 1b466cdaea50f74822a30e986e94d4fa4076d74b..a30c62af9bf7b232675dfbbc3fb5754ce567b583 100644 (file)
@@ -50,8 +50,7 @@ static struct option_descriptor params_opts[] = {
 
 /** "params" command descriptor */
 static struct command_descriptor params_cmd =
-       COMMAND_DESC ( struct params_options, params_opts, 0, 0,
-                      "[--name <name>] [--delete]" );
+       COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
 
 /**
  * The "params" command
@@ -96,7 +95,7 @@ static struct option_descriptor param_opts[] = {
 /** "param" command descriptor */
 static struct command_descriptor param_cmd =
        COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
-                      "[--params <params>] <key> [<value>]" );
+                      "<key> [<value>]" );
 
 /**
  * The "param" command
index 4959f6a002c67a518daa6f40cbceba44ad3d7e8a..d514a2a27e29c79472bcf9be6988a1817116e00d 100644 (file)
@@ -60,8 +60,7 @@ static struct option_descriptor ping_opts[] = {
 
 /** "ping" command descriptor */
 static struct command_descriptor ping_cmd =
-       COMMAND_DESC ( struct ping_options, ping_opts, 1, 1,
-                      "[--size <size>] [--timeout <timeout>] <host>" );
+       COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
 
 /**
  * The "ping" command
index 159fe61868fbff5003ad0737cd6c642226901225..9d487d330a301a60c5d003f2dbfbf17a9887dc76 100644 (file)
@@ -40,7 +40,7 @@ static struct option_descriptor poweroff_opts[] = {};
 
 /** "poweroff" command descriptor */
 static struct command_descriptor poweroff_cmd =
-       COMMAND_DESC ( struct poweroff_options, poweroff_opts, 0, 0, "" );
+       COMMAND_DESC ( struct poweroff_options, poweroff_opts, 0, 0, NULL );
 
 /**
  * The "poweroff" command
index 44dcfc71c7dd6cedfdfc0abade83336969ee54a0..485939e4285f3b56c2677ebe3b7dd4d175cabe15 100644 (file)
@@ -44,7 +44,7 @@ static struct option_descriptor reboot_opts[] = {
 
 /** "reboot" command descriptor */
 static struct command_descriptor reboot_cmd =
-       COMMAND_DESC ( struct reboot_options, reboot_opts, 0, 0, "[--warm]" );
+       COMMAND_DESC ( struct reboot_options, reboot_opts, 0, 0, NULL );
 
 /**
  * The "reboot" command
index 3b51f06b0e3b0e43220bb784e06477f0b9dfcb83..cc5ffc2f2d4030345d81229aee1c4e3b2c0ef62e 100644 (file)
@@ -39,7 +39,7 @@ static struct option_descriptor route_opts[] = {};
 
 /** "route" command descriptor */
 static struct command_descriptor route_cmd =
-       COMMAND_DESC ( struct route_options, route_opts, 0, 0, "" );
+       COMMAND_DESC ( struct route_options, route_opts, 0, 0, NULL );
 
 /**
  * The "route" command
index 14055a8c41d00f675935def8b3cb92feb24b8f87..5954b63268024545c1c4ee268be4400671dfa977 100644 (file)
@@ -46,30 +46,38 @@ struct sanboot_options {
 };
 
 /** "sanboot" option list */
-static struct option_descriptor sanboot_opts[] = {
-       OPTION_DESC ( "drive", 'd', required_argument,
-                     struct sanboot_options, drive, parse_integer ),
-       OPTION_DESC ( "no-describe", 'n', no_argument,
-                     struct sanboot_options, no_describe, parse_flag ),
-       OPTION_DESC ( "keep", 'k', no_argument,
-                     struct sanboot_options, keep, parse_flag ),
+static union {
+       /* "sanboot" takes all three options */
+       struct option_descriptor sanboot[3];
+       /* "sanhook" takes only --drive and --no-describe */
+       struct option_descriptor sanhook[2];
+       /* "sanunhook" takes only --drive */
+       struct option_descriptor sanunhook[1];
+} opts = {
+       .sanboot = {
+               OPTION_DESC ( "drive", 'd', required_argument,
+                             struct sanboot_options, drive, parse_integer ),
+               OPTION_DESC ( "no-describe", 'n', no_argument,
+                             struct sanboot_options, no_describe, parse_flag ),
+               OPTION_DESC ( "keep", 'k', no_argument,
+                             struct sanboot_options, keep, parse_flag ),
+       },
 };
 
+
 /** "sanhook" command descriptor */
 static struct command_descriptor sanhook_cmd =
-       COMMAND_DESC ( struct sanboot_options, sanboot_opts, 1, 1,
-                      "[--drive <drive>] [--no-describe] <root-path>" );
+       COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, 1,
+                      "<root-path>" );
 
 /** "sanboot" command descriptor */
 static struct command_descriptor sanboot_cmd =
-       COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 1,
-                      "[--drive <drive>] [--no-describe] [--keep] "
+       COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, 1,
                       "[<root-path>]" );
 
 /** "sanunhook" command descriptor */
 static struct command_descriptor sanunhook_cmd =
-       COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 0,
-                      "[--drive <drive>]" );
+       COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
 
 /**
  * The "sanboot", "sanhook" and "sanunhook" commands
index 3c9ef2a4d7e91f156bcb6cbca2dbd9c2a96f979b..adf7e3cc67f4444b34eacddc23d2e743bf0fab8e 100644 (file)
@@ -46,8 +46,7 @@ static struct option_descriptor sync_opts[] = {
 
 /** "sync" command descriptor */
 static struct command_descriptor sync_cmd =
-       COMMAND_DESC ( struct sync_options, sync_opts, 0, 0,
-                      "[--timeout <timeout>]" );
+       COMMAND_DESC ( struct sync_options, sync_opts, 0, 0, NULL );
 
 /**
  * "sync" command
index 822e5dcf3cb527dc1968dc350e03ee92b0c6b4cd..5d7298220ff34fd95a6f5b26ac2bf45abe269462 100644 (file)
@@ -53,7 +53,6 @@ static struct option_descriptor vcreate_opts[] = {
 /** "vcreate" command descriptor */
 static struct command_descriptor vcreate_cmd =
        COMMAND_DESC ( struct vcreate_options, vcreate_opts, 1, 1,
-                      "--tag <tag> [--priority <priority>] "
                       "<trunk interface>" );
 
 /**
index b62086769aa6745e4ff4d07db8da0ce6237a01ed..c1a54384995e818a9e29a58e4ddf3523b0d24438 100644 (file)
@@ -107,7 +107,7 @@ static struct option_descriptor shell_opts[] = {};
 
 /** "shell" command descriptor */
 static struct command_descriptor shell_cmd =
-       COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, "" );
+       COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
 
 /**
  * "shell" command
index 8d80241dccb480027d89166a17ac33b713dec158..5328da8b4fcebcb19c8cff48eba768ec1a4f524a 100644 (file)
@@ -375,7 +375,7 @@ static struct option_descriptor prompt_opts[] = {
 /** "prompt" command descriptor */
 static struct command_descriptor prompt_cmd =
        COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
-                      "[--key <key>] [--timeout <timeout>] [<text>]" );
+                      "[<text>]" );
 
 /**
  * "prompt" command