mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
util_opt: Change util_opt_init() to honor current command, if set
Function util_opt_init() build the option string for getopt_long(). If a command has been set via util_opt_set_command(), then util_opt_init() must honor that command and only add those options that match the command, or are command independent. That way the same option can be used in different commands with different flags and different argument settings. E.g. for command 'a' option '-x' might require an argument, for command 'b' the same option '-x' might not require an argument. The behavior of util_opt_init() is unchanged if no command is set, and also if different commands use the same option, but with the same flags and argument settings. Currently only the zkey tools set a command, but use unique options per command. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com> Acked-by: Jan Höppner <hoeppner@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
c31eba5e00
commit
2f87cba1c7
@@ -42,6 +42,12 @@ static struct util_opt opt_vec[] = {
|
||||
.desc = "Option with a required argument REQ_ARG",
|
||||
.command = COMMAND_PULL,
|
||||
},
|
||||
{
|
||||
.option = { "test", required_argument, NULL, 't'},
|
||||
.argument = "TEST",
|
||||
.desc = "Option 'test' with a required argument TEST for pull",
|
||||
.command = COMMAND_PULL,
|
||||
},
|
||||
{
|
||||
.desc = "OPTIONS",
|
||||
.flags = UTIL_OPT_FLAG_SECTION,
|
||||
@@ -57,7 +63,12 @@ static struct util_opt opt_vec[] = {
|
||||
.option = { NULL, no_argument, NULL, 'l'},
|
||||
.desc = "Option with only a short name",
|
||||
.flags = UTIL_OPT_FLAG_NOLONG,
|
||||
.command = "push",
|
||||
.command = COMMAND_PUSH,
|
||||
},
|
||||
{
|
||||
.option = { "test", no_argument, NULL, 't'},
|
||||
.desc = "Option 'test' without an argument for push",
|
||||
.command = COMMAND_PUSH,
|
||||
},
|
||||
UTIL_OPT_SECTION("COMMON OPTIONS"),
|
||||
/* Standard option: -h,--help */
|
||||
@@ -122,9 +133,6 @@ int main(int argc, char *argv[])
|
||||
char **my_argv = argv;
|
||||
char *command = NULL;
|
||||
|
||||
/* Install option vector */
|
||||
util_opt_init(opt_vec, NULL);
|
||||
|
||||
/* The command name is the very first argument */
|
||||
if (argc >= 2 && strncmp(argv[1], "-", 1) != 0) {
|
||||
command = argv[1];
|
||||
@@ -144,6 +152,9 @@ int main(int argc, char *argv[])
|
||||
util_opt_set_command(command);
|
||||
util_prg_set_command(command);
|
||||
|
||||
/* Install option vector */
|
||||
util_opt_init(opt_vec, NULL);
|
||||
|
||||
/* Parse all options specified in my_argv[] */
|
||||
while (1) {
|
||||
/* Get the next option 'c' from my_argv[] */
|
||||
@@ -173,6 +184,9 @@ int main(int argc, char *argv[])
|
||||
case 'l':
|
||||
printf("Specified: -l\n");
|
||||
break;
|
||||
case 't':
|
||||
printf("Specified: --test %s\n", optarg ? optarg : "");
|
||||
break;
|
||||
default:
|
||||
util_opt_print_parse_error(c, my_argv);
|
||||
return EXIT_FAILURE;
|
||||
|
||||
Reference in New Issue
Block a user