command line options for layer23 apps

Christian Vogel vogelchr at vogel.cx
Tue Oct 26 15:48:53 UTC 2010


Hi Andreas,

> therefore i like to leave all common options in common/main.c.
> additional options i like to put in the individual app_*.c files. each
> options i like to check at the individual app file. if it doesn't exist
> there, the main.c checks if the option is a common option:
> ...
> c = l23_app_handle_options();
> if (c == -1)
>         c = getopt_long(argc, argv, "hs:S:a:i:v:d:",
>                     long_options, &option_index);
> if (c == -1)
>         break;
> ...

To be honest, I don't like the idea of calling two different "instances"
of getopt/getopt_long. But that's just me, probably.

What about a app-specific function to call early where the application
can register the relevant options:

	/* in app.c, called before getopt is started */
	l32_app_setup_options(){
		l32_add_option('q',"quux",1); /* short, long, hasarg */
		l32_add_option('d',"debug",0);
	}

	/* in common/main.c */
	l32_add_option(char short,char *long,int hasarg){
		/* append short to a global getopts-type-string */
		/* append long opts to global table of long opts */
		/* mark long-opt index in table (for long-only opts)
		   so that we know to call l32_app_option */
	}

And a callback if the option is given:

	l32_app_option(char short,char *long,char *val){
		if(short == 'q' or !strcmp(long,"quux")){
			quux = 42 + atoi(val);
		}
		if(short == 'd' or !strcmp(long,"debug")){
			debugflag++;
		}
	}

I think that makes for even shorter apps with less getopt branching.

Of course it makes it more difficult to handle options where the meaning
is position dependant, you have to keep state in global structures then.
But we don't want to recreate ffmpeg, do we?

	Chris




More information about the baseband-devel mailing list