First commandline argument parsing with libargtable2.

This commit is contained in:
Kai Lauterbach 2012-01-12 20:03:42 +01:00
parent a6c2b59287
commit 25cb6c8c48

View file

@ -23,67 +23,124 @@
// External functions to control the ledcube.
extern void lc_setFrame(unsigned long);
extern void lc_setMode(int);
extern int lc_saveFrame(unsigned long, int, int);
extern int lc_saveFrame(unsigned long, int, int);
extern void lc_init(void);
extern void lc_close(void);
int main(int argc, char **argv)
{
// parameter structures
struct arg_lit *param_stop = arg_lit0("p", "stop", "stop animation");
struct arg_lit *param_single = arg_lit0("i", "single", "single animation");
struct arg_lit *param_loop = arg_lit0("l", "loop", "loop animation");
struct arg_int *param_frame = arg_int0("m", "frame", "<n>", "frame data");
struct arg_int *param_pos = arg_int0("o", "pos", "<n>", "frame pos in eeprom");
struct arg_int *param_delay = arg_int0("y", "delay", "<n>", "frame delay in eeprom");
struct arg_lit *param_save = arg_lit0("s", "save", "save one frame to the EEPROM");
//struct arg_lit *param_tty = arg_lit0("t", "terminal", "terminal mode");
//struct arg_lit *param_daemon = arg_lit0("d", "daemon", "daemon mode");
struct arg_lit *param_help = arg_lit0("h", "help", "show help");
struct arg_file *param_file = arg_filen("f", "ihex-file","<file>", 0, 1, "iHex input file to write to the EEPROM");
struct arg_end *param_end = arg_end(20);
// default parameter
param_frame->ival[0] = 0x00000000;
param_pos->ival[0] = 0;
param_delay->ival[0] = 1;
// argtable array
void *argtable[] = {
param_stop,
param_single,
param_loop,
param_frame,
param_pos,
param_delay,
param_save,
param_file,
//param_tty,
//param_daemon,
param_help,
param_end
};
// parse the commandline arguments
int nerrors = arg_parse(argc,argv,argtable);
if (arg_nullcheck(argtable) != 0)
{
printf("error: insufficient memory\n");
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return 0;
}
if (nerrors == 0 && param_help->count > 0)
{
printf("Usage: ./clcc <OPTIONS>\n");
arg_print_glossary(stdout, argtable, "\t%-25s %s\n");
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return 0;
}
// parsing failed or to less parameter given
if (nerrors > 0
|| argc < 2
|| (param_save->count > 0 && (param_pos->count == 0 || param_delay->count == 0 || param_frame->count == 0))
)
{
arg_print_errors(stdout,param_end,"clcc");
printf("\nUsage: ./clcc <OPTIONS>\n");
arg_print_syntaxv(stdout,argtable,"\n");
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return -1;
}
lc_init();
if (argc == 2 && !strcmp((char*)argv[1], "--stop"))
if (param_stop->count > 0)
{
printf("stop animation loop\n");
lc_setMode(MODE_ANIMATION_STOP);
}
else if (argc == 2 && !strcmp((char*)argv[1], "--loop"))
else if (param_loop->count > 0)
{
printf("starting animation loop\n");
lc_setMode(MODE_ANIMATION_LOOP);
}
else if (argc == 2 && !strcmp((char*)argv[1], "--single"))
else if (param_single->count > 0 )
{
printf("starting animation as single shot\n");
lc_setMode(MODE_ANIMATION_SINGLE);
}
else if (argc == 3 && !strcmp((char*)argv[1], "--view"))
else if (param_save->count > 0
&& param_pos->count > 0
&& param_delay->count > 0
&& param_frame->count > 0)
{
lc_setMode(MODE_ANIMATION_STOP);
unsigned int frame = 0;
if ( sscanf((char*)argv[2], "0x%08x", &frame) )
{
printf("view frame: data=0x%08x\n", frame);
lc_setFrame(frame);
}
}
else if (argc == 5 && !strcmp((char*)argv[1], "--save"))
{
lc_setMode(MODE_ANIMATION_STOP);
// TODO parse commandline parameters
unsigned int pos = 0;
if ( sscanf((char*)argv[2], "%d", &pos) )
{
unsigned int delay = 0;
if ( sscanf((char*)argv[3], "%d", &delay) )
{
unsigned int frame = 0;
if ( sscanf((char*)argv[4], "0x%08x", &frame) )
{
printf("saving frame: index=%d delay=%d frame=0x%08x\n", pos, delay, frame);
//lc_setFrame(frame);
lc_saveFrame(frame, delay, pos);
}
}
}
int frame = param_frame->ival[0];
int delay = param_delay->ival[0];
int pos = param_pos->ival[0];
printf("saving frame: index=%d delay=%d frame=0x%08x\n", pos, delay, frame);
lc_saveFrame(frame, delay, pos);
}
else if (param_frame->count > 0)
{
lc_setMode(MODE_ANIMATION_STOP);
int frame = param_frame->ival[0];
printf("view frame: data=0x%08x\n", frame);
lc_setFrame(frame);
}
//printf("file[%d]=%s\n",i,file->filename[i]);*/
lc_close();
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return 0;
}