#include <command.h>command *command_init(string name);void command_register(command *c, string cmd, string argtypes, string help);string *command_get(command *c);void command_close(command *c);
After obtaining this data structure with command_init (the name is merely used to allow multiple commands to live peacefully together when doing disk I/O operations), commands (or actions if you wish) need to be registered with command_register. In addition to the command, the argument types also need to be supplied. Currently only integer, real, string and optional arguments are allowed, designated by the characters "irs." resp. If no arguments are allowed, the argtypes string should be left blank (but not NULL).
The command parser returns a valid argv like array of strings from command_get, but it is currentlyu left to the user to parse these correctly, despite the earlier registration requirements (e.g. they lack a function pointer).
command_close is called to free up all memory associated with the command parser.
#include <command.h> ... void do_a(int), do_b(int, double); ... string *argv; .. command cmd = command_init(name); /* initialize */ command_register(cmd,"a","i", "foo bar"); /* register commands */ command_register(cmd,"b","ir", "fum bar"); command_register(cmd,"quit","","alternate form of quit"); while((argv=command_get(cmd))) { /* loop getting arguments */ na = xstrlen(argv,sizeof(string))-1; if (streq(argv[0],"quit")) { break; } else if (streq(argv[0],"a")) { do_a(natoi(argv[1])); } else if (streq(argv[0],"b")) { do_b(natoi(argv[1]), natof(argv[2])); } freestrings(argv); } command_close(cmd);
24-dec-2003 V1.0 - written for SIRTF’s map2 project PJT 18-feb-2004 V1.1 - implemented command_read PJT