[BusyBox] re: long args in bb_getopt_ulflags()

Tito farmatito at tiscali.it
Mon Jan 31 20:36:48 UTC 2005


On Monday 31 January 2005 17:36, Paul Fox wrote:
>  > Paul,
>  > 
>  > > hi tito.  i'm sorry, i was unclear in my statement above.  yes,
>  > > your example works fine, but "--version" is just a flag option --
>  > > there's no optarg associated with it.  i should have said:  if a
>  > > long option needs to have an argument, and there's no short
>  > > option equivalent for that option, then you can't get the
>  > > argument using bb_getop_ulflags().

Hi to all, 
one more example.
I know that the code is rather ugly and bloated but it does
the work and is easy to understand.

int strings_main(int argc, char **argv)
{
	int n, c, i = 0, status = EXIT_SUCCESS;
	unsigned long opt;
	unsigned long count;
	FILE *file = stdin;
	char *string;
	const char *fmt = "%s: ";
	char *n_arg = "4";
	int color=0;
	
	struct option strings_long_options[] = {
		{ "color", 0, &color, -1 },
		{ 0,         0, 0, 0 }
	};
	
	bb_applet_long_options = strings_long_options;
		
	opt = bb_getopt_ulflags (argc, argv, "afon:", &n_arg);
	/* -a is our default behaviour */
	
	if(color == -1) {
		for (i=0;i<argc;i++) {
			printf("%d %s\n",i, argv[i]); /* debug */
			if(strcmp(argv[i],"--color=never")==0) {
				color=0;
			}
			if(strcmp(argv[i],"--color=always")==0) {
				color=1;
			}
			if(strcmp(argv[i],"--color=auto")==0) {
				color=2;
			}
		} 
		printf("color %d\n", color); /* debug */
		if(color == -1) /* no optarg, assume color=always */ 
			color=1;
	}
	i=0; 
	argc -= optind;
	argv += optind;

and so on........ 
Just an idea.

Ciao,
Tito



More information about the busybox mailing list