Selaa lähdekoodia

[cmdline] Generate command option help text automatically

Generate the command option help text automatically from the list of
defined options.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 vuotta sitten
vanhempi
commit
43eba2f555

+ 1
- 1
src/arch/i386/hci/commands/pxe_cmd.c Näytä tiedosto

@@ -78,7 +78,7 @@ static struct option_descriptor stoppxe_opts[] = {};
78 78
 
79 79
 /** "stoppxe" command descriptor */
80 80
 static struct command_descriptor stoppxe_cmd =
81
-	COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0, "" );
81
+	COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0, NULL );
82 82
 
83 83
 /**
84 84
  * The "stoppxe" command

+ 1
- 2
src/arch/x86/hci/commands/cpuid_cmd.c Näytä tiedosto

@@ -54,8 +54,7 @@ static struct option_descriptor cpuid_opts[] = {
54 54
 
55 55
 /** "cpuid" command descriptor */
56 56
 static struct command_descriptor cpuid_cmd =
57
-	COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1,
58
-		       "[--ext] [--ecx] <bit>" );
57
+	COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1, "<bit>" );
59 58
 
60 59
 /**
61 60
  * The "cpuid" command

+ 1
- 1
src/core/exec.c Näytä tiedosto

@@ -397,7 +397,7 @@ static struct option_descriptor echo_opts[] = {
397 397
 /** "echo" command descriptor */
398 398
 static struct command_descriptor echo_cmd =
399 399
 	COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
400
-		       "[-n] [...]" );
400
+		       "[...]" );
401 401
 
402 402
 /**
403 403
  * "echo" command

+ 19
- 2
src/core/parseopt.c Näytä tiedosto

@@ -326,8 +326,25 @@ int parse_parameters ( char *text, struct parameters **params ) {
326 326
  * @v argv		Argument list
327 327
  */
328 328
 void print_usage ( struct command_descriptor *cmd, char **argv ) {
329
-	printf ( "Usage:\n\n  %s %s\n\nSee http://ipxe.org/cmd/%s for further "
330
-		 "information\n", argv[0], cmd->usage, argv[0] );
329
+	struct option_descriptor *option;
330
+	unsigned int i;
331
+	int is_optional;
332
+
333
+	printf ( "Usage:\n\n  %s", argv[0] );
334
+	for ( i = 0 ; i < cmd->num_options ; i++ ) {
335
+		option = &cmd->options[i];
336
+		printf ( " [-%c|--%s", option->shortopt, option->longopt );
337
+		if ( option->has_arg ) {
338
+			is_optional = ( option->has_arg == optional_argument );
339
+			printf ( " %s<%s>%s", ( is_optional ? "[" : "" ),
340
+				 option->longopt, ( is_optional ? "]" : "" ) );
341
+		}
342
+		printf ( "]" );
343
+	}
344
+	if ( cmd->usage )
345
+		printf ( " %s", cmd->usage );
346
+	printf ( "\n\nSee http://ipxe.org/cmd/%s for further information\n",
347
+		 argv[0] );
331 348
 }
332 349
 
333 350
 /**

+ 2
- 3
src/hci/commands/fcmgmt_cmd.c Näytä tiedosto

@@ -106,7 +106,7 @@ static struct option_descriptor fcstat_opts[] = {};
106 106
 
107 107
 /** "fcstat" command descriptor */
108 108
 static struct command_descriptor fcstat_cmd =
109
-	COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, "" );
109
+	COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, NULL );
110 110
 
111 111
 /**
112 112
  * The "fcstat" command
@@ -151,8 +151,7 @@ static struct option_descriptor fcels_opts[] = {
151 151
 
152 152
 /** "fcels" command descriptor */
153 153
 static struct command_descriptor fcels_cmd =
154
-	COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1,
155
-		       "[--port <port>] [--id <peer port id>] <request>" );
154
+	COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1, "<request>" );
156 155
 
157 156
 /**
158 157
  * The "fcels" command

+ 1
- 3
src/hci/commands/ifmgmt_cmd.c Näytä tiedosto

@@ -226,9 +226,7 @@ static int ifconf_payload ( struct net_device *netdev,
226 226
 /** "ifconf" command descriptor */
227 227
 static struct ifcommon_command_descriptor ifconf_cmd =
228 228
 	IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
229
-				0, MAX_ARGUMENTS,
230
-				"[--configurator <configurator>] "
231
-				"[<interface>...]",
229
+				0, MAX_ARGUMENTS, "[<interface>...]",
232 230
 				ifconf_payload, 1 );
233 231
 
234 232
 /**

+ 30
- 23
src/hci/commands/image_cmd.c Näytä tiedosto

@@ -47,22 +47,22 @@ struct imgsingle_options {
47 47
 };
48 48
 
49 49
 /** "img{single}" option list */
50
-static struct option_descriptor imgsingle_opts[] = {
51
-	OPTION_DESC ( "name", 'n', required_argument,
52
-		      struct imgsingle_options, name, parse_string ),
53
-	OPTION_DESC ( "replace", 'r', no_argument,
54
-		      struct imgsingle_options, replace, parse_flag ),
55
-	OPTION_DESC ( "autofree", 'a', no_argument,
56
-		      struct imgsingle_options, autofree, parse_flag ),
50
+static union {
51
+	/* "imgexec" takes all three options */
52
+	struct option_descriptor imgexec[3];
53
+	/* Other "img{single}" commands take only --name and --autofree */
54
+	struct option_descriptor imgsingle[2];
55
+} opts = {
56
+	.imgexec = {
57
+		OPTION_DESC ( "name", 'n', required_argument,
58
+			      struct imgsingle_options, name, parse_string ),
59
+		OPTION_DESC ( "autofree", 'a', no_argument,
60
+			      struct imgsingle_options, autofree, parse_flag ),
61
+		OPTION_DESC ( "replace", 'r', no_argument,
62
+			      struct imgsingle_options, replace, parse_flag ),
63
+	},
57 64
 };
58 65
 
59
-/** "img{single}" command descriptor */
60
-static struct command_descriptor imgsingle_cmd =
61
-	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
62
-		       1, MAX_ARGUMENTS,
63
-		       "[--name <name>] [--autofree] "
64
-		       "<uri|image> [<arguments>...]" );
65
-
66 66
 /** An "img{single}" family command descriptor */
67 67
 struct imgsingle_descriptor {
68 68
 	/** Command descriptor */
@@ -174,9 +174,8 @@ static int imgsingle_exec ( int argc, char **argv,
174 174
 
175 175
 /** "imgfetch" command descriptor */
176 176
 static struct command_descriptor imgfetch_cmd =
177
-	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
178
-		       1, MAX_ARGUMENTS,
179
-		       "[--name <name>] [--autofree] <uri> [<arguments>...]" );
177
+	COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
178
+		       1, MAX_ARGUMENTS, "<uri> [<arguments>...]" );
180 179
 
181 180
 /** "imgfetch" family command descriptor */
182 181
 struct imgsingle_descriptor imgfetch_desc = {
@@ -207,9 +206,14 @@ static int imgselect ( struct image *image,
207 206
 	return image_select ( image );
208 207
 }
209 208
 
209
+/** "imgselect" command descriptor */
210
+static struct command_descriptor imgselect_cmd =
211
+	COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
212
+		       1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
213
+
210 214
 /** "imgselect" family command descriptor */
211 215
 struct imgsingle_descriptor imgselect_desc = {
212
-	.cmd = &imgsingle_cmd,
216
+	.cmd = &imgselect_cmd,
213 217
 	.acquire = imgacquire,
214 218
 	.action = imgselect,
215 219
 	.verb = "select",
@@ -228,10 +232,8 @@ static int imgselect_exec ( int argc, char **argv ) {
228 232
 
229 233
 /** "imgexec" command descriptor */
230 234
 static struct command_descriptor imgexec_cmd =
231
-	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
232
-		       0, MAX_ARGUMENTS,
233
-		       "[--autofree] [--replace] "
234
-		       "[<uri|image> [<arguments>...]]" );
235
+	COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
236
+		       0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" );
235 237
 
236 238
 /**
237 239
  * "imgexec" command action
@@ -282,9 +284,14 @@ static int imgexec_exec ( int argc, char **argv) {
282 284
 	return imgsingle_exec ( argc, argv, &imgexec_desc );
283 285
 }
284 286
 
287
+/** "imgargs" command descriptor */
288
+static struct command_descriptor imgargs_cmd =
289
+	COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
290
+		       1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
291
+
285 292
 /** "imgargs" family command descriptor */
286 293
 struct imgsingle_descriptor imgargs_desc = {
287
-	.cmd = &imgsingle_cmd,
294
+	.cmd = &imgargs_cmd,
288 295
 	.acquire = imgacquire,
289 296
 	.preaction = image_clear_cmdline,
290 297
 };

+ 2
- 4
src/hci/commands/image_trust_cmd.c Näytä tiedosto

@@ -52,8 +52,7 @@ static struct option_descriptor imgtrust_opts[] = {
52 52
 
53 53
 /** "imgtrust" command descriptor */
54 54
 static struct command_descriptor imgtrust_cmd =
55
-	COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0,
56
-		       "[--allow] [--permanent]" );
55
+	COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0, NULL );
57 56
 
58 57
 /**
59 58
  * The "imgtrust" command
@@ -100,8 +99,7 @@ static struct option_descriptor imgverify_opts[] = {
100 99
 /** "imgverify" command descriptor */
101 100
 static struct command_descriptor imgverify_cmd =
102 101
 	COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
103
-		       "[--signer <signer>] [--keep] <uri|image> "
104
-		       "<signature uri|image>" );
102
+		       "<uri|image> <signature uri|image>" );
105 103
 
106 104
 /**
107 105
  * The "imgverify" command

+ 1
- 1
src/hci/commands/login_cmd.c Näytä tiedosto

@@ -39,7 +39,7 @@ static struct option_descriptor login_opts[] = {};
39 39
 
40 40
 /** "login" command descriptor */
41 41
 static struct command_descriptor login_cmd =
42
-	COMMAND_DESC ( struct login_options, login_opts, 0, 0, "" );
42
+	COMMAND_DESC ( struct login_options, login_opts, 0, 0, NULL );
43 43
 
44 44
 /**
45 45
  * "login" command

+ 1
- 2
src/hci/commands/lotest_cmd.c Näytä tiedosto

@@ -50,8 +50,7 @@ static struct option_descriptor lotest_opts[] = {
50 50
 /** "lotest" command descriptor */
51 51
 static struct command_descriptor lotest_cmd =
52 52
 	COMMAND_DESC ( struct lotest_options, lotest_opts, 2, 2,
53
-		       "[--mtu <mtu>] <sending interface> "
54
-		       "<receiving interface>" );
53
+		       "<sending interface> <receiving interface>" );
55 54
 
56 55
 /**
57 56
  * "lotest" command

+ 3
- 6
src/hci/commands/menu_cmd.c Näytä tiedosto

@@ -57,7 +57,7 @@ static struct option_descriptor menu_opts[] = {
57 57
 /** "menu" command descriptor */
58 58
 static struct command_descriptor menu_cmd =
59 59
 	COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
60
-		       "[--name <name>] [--delete] [<title>]" );
60
+		       "[<title>]" );
61 61
 
62 62
 /**
63 63
  * The "menu" command
@@ -131,8 +131,7 @@ static struct option_descriptor item_opts[] = {
131 131
 /** "item" command descriptor */
132 132
 static struct command_descriptor item_cmd =
133 133
 	COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
134
-		       "[--menu <menu>] [--key <key>] [--default] "
135
-		       "[<label>|--gap [<text>]]" );
134
+		       "[<label> [<text>]]" );
136 135
 
137 136
 /**
138 137
  * The "item" command
@@ -215,9 +214,7 @@ static struct option_descriptor choose_opts[] = {
215 214
 
216 215
 /** "choose" command descriptor */
217 216
 static struct command_descriptor choose_cmd =
218
-	COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
219
-		       "[--menu <menu>] [--default <label>] "
220
-		       "[--timeout <timeout>] [--keep] <setting>" );
217
+	COMMAND_DESC ( struct choose_options, choose_opts, 1, 1, "<setting>" );
221 218
 
222 219
 /**
223 220
  * The "choose" command

+ 1
- 1
src/hci/commands/neighbour_cmd.c Näytä tiedosto

@@ -38,7 +38,7 @@ static struct option_descriptor nstat_opts[] = {};
38 38
 
39 39
 /** "nstat" command descriptor */
40 40
 static struct command_descriptor nstat_cmd =
41
-	COMMAND_DESC ( struct nstat_options, nstat_opts, 0, 0, "" );
41
+	COMMAND_DESC ( struct nstat_options, nstat_opts, 0, 0, NULL );
42 42
 
43 43
 /**
44 44
  * The "nstat" command

+ 2
- 3
src/hci/commands/param_cmd.c Näytä tiedosto

@@ -50,8 +50,7 @@ static struct option_descriptor params_opts[] = {
50 50
 
51 51
 /** "params" command descriptor */
52 52
 static struct command_descriptor params_cmd =
53
-	COMMAND_DESC ( struct params_options, params_opts, 0, 0,
54
-		       "[--name <name>] [--delete]" );
53
+	COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
55 54
 
56 55
 /**
57 56
  * The "params" command
@@ -96,7 +95,7 @@ static struct option_descriptor param_opts[] = {
96 95
 /** "param" command descriptor */
97 96
 static struct command_descriptor param_cmd =
98 97
 	COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
99
-		       "[--params <params>] <key> [<value>]" );
98
+		       "<key> [<value>]" );
100 99
 
101 100
 /**
102 101
  * The "param" command

+ 1
- 2
src/hci/commands/ping_cmd.c Näytä tiedosto

@@ -60,8 +60,7 @@ static struct option_descriptor ping_opts[] = {
60 60
 
61 61
 /** "ping" command descriptor */
62 62
 static struct command_descriptor ping_cmd =
63
-	COMMAND_DESC ( struct ping_options, ping_opts, 1, 1,
64
-		       "[--size <size>] [--timeout <timeout>] <host>" );
63
+	COMMAND_DESC ( struct ping_options, ping_opts, 1, 1, "<host>" );
65 64
 
66 65
 /**
67 66
  * The "ping" command

+ 1
- 1
src/hci/commands/poweroff_cmd.c Näytä tiedosto

@@ -40,7 +40,7 @@ static struct option_descriptor poweroff_opts[] = {};
40 40
 
41 41
 /** "poweroff" command descriptor */
42 42
 static struct command_descriptor poweroff_cmd =
43
-	COMMAND_DESC ( struct poweroff_options, poweroff_opts, 0, 0, "" );
43
+	COMMAND_DESC ( struct poweroff_options, poweroff_opts, 0, 0, NULL );
44 44
 
45 45
 /**
46 46
  * The "poweroff" command

+ 1
- 1
src/hci/commands/reboot_cmd.c Näytä tiedosto

@@ -44,7 +44,7 @@ static struct option_descriptor reboot_opts[] = {
44 44
 
45 45
 /** "reboot" command descriptor */
46 46
 static struct command_descriptor reboot_cmd =
47
-	COMMAND_DESC ( struct reboot_options, reboot_opts, 0, 0, "[--warm]" );
47
+	COMMAND_DESC ( struct reboot_options, reboot_opts, 0, 0, NULL );
48 48
 
49 49
 /**
50 50
  * The "reboot" command

+ 1
- 1
src/hci/commands/route_cmd.c Näytä tiedosto

@@ -39,7 +39,7 @@ static struct option_descriptor route_opts[] = {};
39 39
 
40 40
 /** "route" command descriptor */
41 41
 static struct command_descriptor route_cmd =
42
-	COMMAND_DESC ( struct route_options, route_opts, 0, 0, "" );
42
+	COMMAND_DESC ( struct route_options, route_opts, 0, 0, NULL );
43 43
 
44 44
 /**
45 45
  * The "route" command

+ 21
- 13
src/hci/commands/sanboot_cmd.c Näytä tiedosto

@@ -46,30 +46,38 @@ struct sanboot_options {
46 46
 };
47 47
 
48 48
 /** "sanboot" option list */
49
-static struct option_descriptor sanboot_opts[] = {
50
-	OPTION_DESC ( "drive", 'd', required_argument,
51
-		      struct sanboot_options, drive, parse_integer ),
52
-	OPTION_DESC ( "no-describe", 'n', no_argument,
53
-		      struct sanboot_options, no_describe, parse_flag ),
54
-	OPTION_DESC ( "keep", 'k', no_argument,
55
-		      struct sanboot_options, keep, parse_flag ),
49
+static union {
50
+	/* "sanboot" takes all three options */
51
+	struct option_descriptor sanboot[3];
52
+	/* "sanhook" takes only --drive and --no-describe */
53
+	struct option_descriptor sanhook[2];
54
+	/* "sanunhook" takes only --drive */
55
+	struct option_descriptor sanunhook[1];
56
+} opts = {
57
+	.sanboot = {
58
+		OPTION_DESC ( "drive", 'd', required_argument,
59
+			      struct sanboot_options, drive, parse_integer ),
60
+		OPTION_DESC ( "no-describe", 'n', no_argument,
61
+			      struct sanboot_options, no_describe, parse_flag ),
62
+		OPTION_DESC ( "keep", 'k', no_argument,
63
+			      struct sanboot_options, keep, parse_flag ),
64
+	},
56 65
 };
57 66
 
67
+
58 68
 /** "sanhook" command descriptor */
59 69
 static struct command_descriptor sanhook_cmd =
60
-	COMMAND_DESC ( struct sanboot_options, sanboot_opts, 1, 1,
61
-		       "[--drive <drive>] [--no-describe] <root-path>" );
70
+	COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, 1,
71
+		       "<root-path>" );
62 72
 
63 73
 /** "sanboot" command descriptor */
64 74
 static struct command_descriptor sanboot_cmd =
65
-	COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 1,
66
-		       "[--drive <drive>] [--no-describe] [--keep] "
75
+	COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, 1,
67 76
 		       "[<root-path>]" );
68 77
 
69 78
 /** "sanunhook" command descriptor */
70 79
 static struct command_descriptor sanunhook_cmd =
71
-	COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 0,
72
-		       "[--drive <drive>]" );
80
+	COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
73 81
 
74 82
 /**
75 83
  * The "sanboot", "sanhook" and "sanunhook" commands

+ 1
- 2
src/hci/commands/sync_cmd.c Näytä tiedosto

@@ -46,8 +46,7 @@ static struct option_descriptor sync_opts[] = {
46 46
 
47 47
 /** "sync" command descriptor */
48 48
 static struct command_descriptor sync_cmd =
49
-	COMMAND_DESC ( struct sync_options, sync_opts, 0, 0,
50
-		       "[--timeout <timeout>]" );
49
+	COMMAND_DESC ( struct sync_options, sync_opts, 0, 0, NULL );
51 50
 
52 51
 /**
53 52
  * "sync" command

+ 0
- 1
src/hci/commands/vlan_cmd.c Näytä tiedosto

@@ -53,7 +53,6 @@ static struct option_descriptor vcreate_opts[] = {
53 53
 /** "vcreate" command descriptor */
54 54
 static struct command_descriptor vcreate_cmd =
55 55
 	COMMAND_DESC ( struct vcreate_options, vcreate_opts, 1, 1,
56
-		       "--tag <tag> [--priority <priority>] "
57 56
 		       "<trunk interface>" );
58 57
 
59 58
 /**

+ 1
- 1
src/hci/shell.c Näytä tiedosto

@@ -107,7 +107,7 @@ static struct option_descriptor shell_opts[] = {};
107 107
 
108 108
 /** "shell" command descriptor */
109 109
 static struct command_descriptor shell_cmd =
110
-	COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, "" );
110
+	COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
111 111
 
112 112
 /**
113 113
  * "shell" command

+ 1
- 1
src/image/script.c Näytä tiedosto

@@ -375,7 +375,7 @@ static struct option_descriptor prompt_opts[] = {
375 375
 /** "prompt" command descriptor */
376 376
 static struct command_descriptor prompt_cmd =
377 377
 	COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
378
-		       "[--key <key>] [--timeout <timeout>] [<text>]" );
378
+		       "[<text>]" );
379 379
 
380 380
 /**
381 381
  * "prompt" command

Loading…
Peruuta
Tallenna