pki: Pre-process common arguments

This way the position of --debug doesn't matter for it to apply to the
parsing of all command-specific arguments.
This commit is contained in:
Tobias Brunner
2023-11-13 12:50:47 +01:00
parent 95c7d49954
commit 5764e1e506
+30 -4
View File
@@ -116,20 +116,41 @@ int command_getopt(char **arg)
{ {
op = getopt_long(argc, argv, command_optstring, command_opts, NULL); op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
switch (op) switch (op)
{
case '+':
case 'v':
continue;
default:
*arg = optarg;
return op;
}
}
}
/**
* Process options common for all commands
*/
static bool process_common_opts()
{
while (TRUE)
{
switch (getopt_long(argc, argv, command_optstring, command_opts, NULL))
{ {
case '+': case '+':
if (!options->from(options, optarg, &argc, &argv, optind)) if (!options->from(options, optarg, &argc, &argv, optind))
{ {
/* a error value */ return FALSE;
return 255;
} }
continue; continue;
case 'v': case 'v':
dbg_default_set_level(atoi(optarg)); dbg_default_set_level(atoi(optarg));
continue; continue;
default: default:
*arg = optarg; continue;
return op; case '?':
return FALSE;
case EOF:
return TRUE;
} }
} }
} }
@@ -279,6 +300,11 @@ int command_dispatch(int c, char *v[])
{ {
active = i; active = i;
build_opts(); build_opts();
if (!process_common_opts())
{
return command_usage("invalid options");
}
optind = 2;
return cmds[i].call(); return cmds[i].call();
} }
} }