Reputation:
I have to write a program in C which has several options on command line when it starts. I was wondering how to handle with a big amount of possible options. I mean, I have already found how to parse them with argp or getopt. Now I have to deal with them. For example, there's the classic option "--verbose | -v". How do I make my program "verbose"? Do I have to write every time something like "if(verboseFlag) printf("...");"? I think that this approach could be dirty when having a lot of arguments. So, what are the possible approaches?
Upvotes: 1
Views: 234
Reputation: 15803
There are lots of possibilities. Here is one. You can use a structure that defines your interface
typedef struct {
void* method1(void);
void* method2(void);
void* method3(void);
} interface;
In function of your options that you pass from command line to main() you initialize this interface with different methods
interface *i;
switch (option) {
case VERBOSE: i->method1=verbose_m1; i->method2=verbose_m2; ...a.s.o.
break;
case DEBUG: i->method1=debug_m1; i->method2=debug_m2; ...a.s.o.
break;
default: i->method1=m1; i->method2=m2; i->method3=m3;
}
and you write a single code in which you call i->method1
.
There are more elegant methods for this, to use uml or type classes , etc. These elegant methods will generate low level code and on your side you define in a simple language the interface, etc. To see how such a language looks like you can look over asdl, which is a very simple language in this spirit.
Upvotes: 2