1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

tools: arg_is_any_set and arg_is_only_set

Helpful functions to more easily detect conflicting
set of options.
This commit is contained in:
Zdenek Kabelac 2014-07-10 22:52:53 +02:00
parent c0c1ada88e
commit 1931d1e58e
2 changed files with 57 additions and 0 deletions

View File

@ -68,6 +68,61 @@ unsigned arg_is_set(const struct cmd_context *cmd, int a)
return arg_count(cmd, a) ? 1 : 0; return arg_count(cmd, a) ? 1 : 0;
} }
int arg_is_any_set(const struct cmd_context *cmd, const char *err, ...)
{
int arg;
va_list ap;
va_start(ap, err);
while ((arg = va_arg(ap, int)) != -1 && !arg_count(cmd, arg))
/* empty */;
va_end(ap);
if (arg != -1) {
log_error("%s %s.", arg_long_option_name(arg), err);
return 0;
}
return 1;
}
int arg_is_only_set(const struct cmd_context *cmd, const char *err, ...)
{
int i, arg;
va_list ap;
for (i = 0; i < ARG_COUNT; ++i) {
switch (i) {
/* skip common options */
case commandprofile_ARG:
case config_ARG:
case debug_ARG:
case driverloaded_ARG:
case help2_ARG:
case help_ARG:
case profile_ARG:
case quiet_ARG:
case verbose_ARG:
case version_ARG:
case yes_ARG:
continue;
}
if (!arg_count(cmd, i))
continue; /* unset */
va_start(ap, err);
while (((arg = va_arg(ap, int)) != -1) && (arg != i))
/* empty */;
va_end(ap);
if (arg != i) {
log_error("Option %s %s.", arg_long_option_name(i), err);
return 0;
}
}
return 1;
}
unsigned grouped_arg_is_set(const struct arg_values *av, int a) unsigned grouped_arg_is_set(const struct arg_values *av, int a)
{ {
return grouped_arg_count(av, a) ? 1 : 0; return grouped_arg_count(av, a) ? 1 : 0;

View File

@ -144,6 +144,8 @@ int major_minor_valid(const struct cmd_context * cmd, const struct format_type *
/* we use the enums to access the switches */ /* we use the enums to access the switches */
unsigned arg_count(const struct cmd_context *cmd, int a); unsigned arg_count(const struct cmd_context *cmd, int a);
unsigned arg_is_set(const struct cmd_context *cmd, int a); unsigned arg_is_set(const struct cmd_context *cmd, int a);
int arg_is_any_set(const struct cmd_context *cmd, const char *err, ...);
int arg_is_only_set(const struct cmd_context *cmd, const char *err, ...);
const char *arg_long_option_name(int a); const char *arg_long_option_name(int a);
const char *arg_value(struct cmd_context *cmd, int a); const char *arg_value(struct cmd_context *cmd, int a);
const char *arg_str_value(struct cmd_context *cmd, int a, const char *def); const char *arg_str_value(struct cmd_context *cmd, int a, const char *def);