Support bundling of single letter options.
authorKristian H?gsberg <krh@redhat.com>
Wed Jun 04 20:16:50 2008 -0400 (2008-06-04)
changeset 2191c2997b34929
parent 218 9aed412ed2b8
child 220 1fcb5c23034a
Support bundling of single letter options.
rpm-razor.c
     1.1 --- a/rpm-razor.c	Wed Jun 04 20:04:57 2008 -0400
     1.2 +++ b/rpm-razor.c	Wed Jun 04 20:16:50 2008 -0400
     1.3 @@ -491,29 +491,34 @@
     1.4  }
     1.5  
     1.6  static int
     1.7 -for_each_option(const struct option *options, const char *s,
     1.8 +for_each_option(const struct option *options,
     1.9 +		const char *name, char short_name,
    1.10  		void (*fn)(const struct option *o,
    1.11 -			   const char *arg, void *data), void *data)
    1.12 +			   const char *name, char short_name,
    1.13 +			   void *data), void *data)
    1.14  {
    1.15  	int i, count = 0;
    1.16  
    1.17  	for (i = 0; options[i].type != OPTION_LAST; i++) {
    1.18  		switch (options[i].type) {
    1.19  		case OPTION_GROUP:
    1.20 -			count += for_each_option(options[i].data, s, fn, data);
    1.21 +			count += for_each_option(options[i].data,
    1.22 +						 name, short_name, fn, data);
    1.23  			break;
    1.24  
    1.25  		case OPTION_BOOL:
    1.26  		case OPTION_STRING:
    1.27 -			if (s[0] == '-' &&
    1.28 -			    s[1] == options[i].short_name && s[2] == '\0') {
    1.29 -				fn(&options[i], s, data);
    1.30 +			if (name && strcmp(options[i].name, name) == 0) {
    1.31 +				fn(&options[i], name, 0, data);
    1.32  				count++;
    1.33 +				break;
    1.34  			}
    1.35 -			if (s[0] == '-' && s[1] == '-' &&
    1.36 -			    strcmp(options[i].name, s + 2) == 0) {
    1.37 -				fn(&options[i], s, data);
    1.38 +
    1.39 +			if (short_name &&
    1.40 +			    short_name == options[i].short_name) {
    1.41 +				fn(&options[i], NULL, short_name, data);
    1.42  				count++;
    1.43 +				break;
    1.44  			}
    1.45  			break;
    1.46  
    1.47 @@ -526,10 +531,14 @@
    1.48  }
    1.49  
    1.50  static void
    1.51 -handle_option(const struct option *o, const char *arg, void *data)
    1.52 +handle_option(const struct option *o,
    1.53 +	      const char *name, char short_name, void *data)
    1.54  {
    1.55  	if (o->data == NULL) {
    1.56 -		printf("option \"%s\" not supported\n", arg);
    1.57 +		if (name)
    1.58 +			printf("option --%s not supported\n", name);
    1.59 +		else
    1.60 +			printf("option -%c not supported\n", short_name);
    1.61  		return;
    1.62  	}
    1.63  
    1.64 @@ -539,7 +548,7 @@
    1.65  		break;
    1.66  
    1.67  	case OPTION_STRING:
    1.68 -		*(const char **) o->data = arg + strlen(o->name) + 3;
    1.69 +		*(const char **) o->data = name + strlen(o->name) + 1;
    1.70  		break;
    1.71  
    1.72  	case OPTION_LAST:
    1.73 @@ -552,9 +561,7 @@
    1.74  static int
    1.75  parse_options(const struct option *options, int argc, const char **argv)
    1.76  {
    1.77 -	int i, j;
    1.78 -
    1.79 -	/* FIXME: Bundling... rpm -Uvh must work :) */
    1.80 +	int i, j, k;
    1.81  
    1.82  	for (i = 1, j = 0; i < argc; i++) {
    1.83  		if (argv[i][0] != '-') {
    1.84 @@ -562,10 +569,21 @@
    1.85  			continue;
    1.86  		}
    1.87  
    1.88 -		if (for_each_option(options, argv[i],
    1.89 -				    handle_option, NULL) == 0) {
    1.90 -			printf("unknown option: \"%s\"\n", argv[i]);
    1.91 -			exit(1);
    1.92 +		if (argv[i][1] == '-') {
    1.93 +			if (for_each_option(options, &argv[i][2], 0,
    1.94 +					    handle_option, NULL) == 0) {
    1.95 +				printf("unknown option: %s\n", argv[i]);
    1.96 +				exit(1);
    1.97 +			}
    1.98 +			continue;
    1.99 +		}
   1.100 +
   1.101 +		for (k = 1; argv[i][k]; k++) {
   1.102 +			if (for_each_option(options, NULL, argv[i][k],
   1.103 +					    handle_option, NULL) == 0) {
   1.104 +				printf("unknown option: -%c\n", argv[i][k]);
   1.105 +				exit(1);
   1.106 +			}
   1.107  		}
   1.108  	}
   1.109