static void buildOptionsTable() { const char* flag; const char* arg; lua_newtable(L); arg_reset(); flag = arg_getflag(); while (flag != NULL) { if (strncmp(flag, "--", 2) == 0) flag += 2; lua_pushstring(L, flag); /* If the flag has an argument, push that too */ arg = arg_getflagarg(); if (arg != NULL) lua_pushstring(L, arg); else lua_pushboolean(L, 1); lua_settable(L, -3); flag = arg_getflag(); } lua_setglobal(L, "options"); }
int main(int argc, char** argv) { /* If no args are specified... */ if (argc == 1) { puts(HELP_MSG); return 1; } /* Set defaults */ os_detect(); g_filename = DEFAULT; g_cc = NULL; g_dotnet = NULL; g_verbose = 0; /* Process any options that will effect script processing */ arg_set(argc, argv); if (!preprocess()) return 1; /* chdir() to the directory containing the project script, so that * relative paths may be used in the script */ io_chdir(path_getdir(g_filename)); /* Now run the script */ g_hasScript = script_run(g_filename); if (g_hasScript < 0) { puts("** Script failed to run, ending."); return 1; } /* Process any options that depend on the script output */ arg_reset(); if (!postprocess()) return 1; /* All done */ if (g_hasScript) script_close(); prj_close(); return 0; }
int arg_parse(int argc, char **argv, void **argtable) { struct arg_hdr **table = (struct arg_hdr **)argtable; struct arg_end *endtable; int endindex; char **argvcopy = NULL; /*printf("arg_parse(%d,%p,%p)\n",argc,argv,argtable);*/ /* reset any argtable data from previous invocations */ arg_reset(argtable); /* locate the first end-of-table marker within the array */ endindex = arg_endindex(table); endtable = (struct arg_end*)table[endindex]; /* Special case of argc==0. This can occur on Texas Instruments DSP. */ /* Failure to trap this case results in an unwanted NULL result from */ /* the malloc for argvcopy (next code block). */ if (argc==0) { /* We must still perform post-parse checks despite the absence of command line arguments */ arg_parse_check(table,endtable); /* Now we are finished */ return endtable->count; } argvcopy = malloc(sizeof(char *) * argc); if (argvcopy) { int i; /* Fill in the local copy of argv[]. We need a local copy because getopt rearranges argv[] which adversely affects susbsequent parsing attempts. */ for (i=0; i<argc; i++) argvcopy[i] = argv[i]; /* parse the command line (local copy) for tagged options */ arg_parse_tagged(argc,argvcopy,table,endtable); /* parse the command line (local copy) for untagged options */ arg_parse_untagged(argc,argvcopy,table,endtable); /* if no errors so far then perform post-parse checks otherwise dont bother */ if (endtable->count==0) arg_parse_check(table,endtable); /* release the local copt of argv[] */ free(argvcopy); } else { /* memory alloc failed */ arg_register_error(endtable,endtable,ARG_EMALLOC,NULL); } return endtable->count; }