From 8a6d1dd408a0dd059a8551cc4a6ed25c758db898 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 17 Dec 2001 10:08:27 +0000 Subject: I had another look at the argument processing code: o You must list long args with no short option (eg. --version) at the front of the args.h file. o If an argument has no short option, set the short option in args.h to '\0' o The index into the 'the_args' var is now stored as the option value for getopt, iff there is no short opt. --- tools/lvm.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'tools/lvm.c') diff --git a/tools/lvm.c b/tools/lvm.c index f10101b3..60d5276e 100644 --- a/tools/lvm.c +++ b/tools/lvm.c @@ -376,6 +376,15 @@ static void alloc_command(void) __alloc(2 * _array_size); } +/* + * Sets up the short and long argument. If there + * is no short argument then the index of the + * argument in the the_args array is set as the + * long opt value. Yuck. Of course this means we + * can't have more than 'a' long arguments. Since + * we have only 1 ATM (--version) I think we can + * live with this restriction. + */ static void add_getopt_arg(int arg, char **ptr, struct option **o) { struct arg *a = the_args + arg; @@ -391,7 +400,7 @@ static void add_getopt_arg(int arg, char **ptr, struct option **o) (*o)->name = a->long_arg + 2; (*o)->has_arg = a->fn ? 1 : 0; (*o)->flag = NULL; - (*o)->val = a->short_arg; + (*o)->val = arg; (*o)++; } } @@ -456,12 +465,18 @@ static int process_command_line(struct command *com, int *argc, char ***argv) static struct arg *find_arg(struct command *com, int opt) { struct arg *a; - int i; + int i, arg; for (i = 0; i < com->num_args; i++) { - a = the_args + com->valid_args[i]; - - if (opt == a->short_arg) + arg = com->valid_args[i]; + a = the_args + arg; + + /* + * opt should equal either the + * short arg, or the index into + * 'the_args'. + */ + if ((a->short_arg && (opt == a->short_arg)) || (opt == arg)) return a; } -- cgit