static void setCassandraLogAgentOption(StringArray& opts, const char* opt, const char* val) { if (opt && *opt && val) { VStringBuffer optstr("%s=%s", opt, val); opts.append(optstr); } }
int main(int argc, char *argv[]) { int opt; char *cmd, *opts; progname = strbsnm(argv[0]); if (argc < 2) error(EXIT_FAILURE, 0, "missing FILE"); cmd = strbsnm(argv[1]); pusharg("/bin/rc"); pusharg(argv[1]); opts = optstr(argv[1]); opterr = 0; argc--, argv++; while ((opt = getopt(argc, argv, opts)) != -1) { switch (opt) { case '?': progname = cmd; error(EXIT_FAILURE, 0, "unknown option: -%c", optopt); case ':': progname = cmd; error(EXIT_FAILURE, 0, "option requires argument: -%c", optopt); default: pusharg(optctos(opt)); if (strchr(opts, opt)[1] == ':') pusharg(optarg); } } pusharg("--"); while (optind < argc) pusharg(argv[optind++]); pusharg(NULL); setenv("cmd", cmd, 1); setenv("fn_err", "{echo >[1=2] $cmd^: $*; exit 1}", 1); setenv("fn_usage", "{echo usage: $cmd $usage}", 1); execvp(arg.v[0], arg.v); error(EXIT_FAILURE, errno, "exec: %s", arg.v[0]); return 0; }
int main(int argc, char** argv) { int n; int ext; int ostr; int str; int loop; Info_t* ip; char* s; char* command; char* usage; char** extra; char** oargv; #if NEW Optdisc_t disc; #endif error_info.id = "opt"; setlocale(LC_ALL, ""); error(-1, "test"); extra = 0; ext = 0; str = 0; while (command = *(argv + 1)) { if (*command == '=' && (s = strchr(command + 1, '='))) { argv++; *s++ = 0; command++; if (ip = newof(0, Info_t, 1, 0)) { ip->name = command; ip->value = s; ip->next = info; info = ip; } } else if (streq(command, "-")) { argv++; str = NEW; } else if (streq(command, "-+")) { argv++; #if NEW ast.locale.set |= (1<<AST_LC_MESSAGES); error_info.translate = translate; #endif } else if (streq(command, "+") && *(argv + 2)) { ext += 2; argv += 2; if (!extra) extra = argv; } else break; } if (!(command = *++argv) || !(usage = *++argv)) error(ERROR_USAGE|4, "[ - | + usage ... ] command-name usage-string [ arg ... ]"); argv += str; error_info.id = command; #if NEW memset(&disc, 0, sizeof(disc)); disc.version = OPT_VERSION; disc.infof = infof; opt_info.disc = &disc; #else memset(&opt_info, 0, sizeof(opt_info)); #endif loop = strncmp(usage, "[-1c", 4) ? 0 : 3; oargv= argv; ostr = str; for (;;) { for (;;) { if (!str) { if (!(n = optget(argv, usage))) break; } else if (!(n = optstr(*argv, usage))) { if (!*++argv) break; continue; } if (loop) sfprintf(sfstdout, "[%d] ", loop); if (n == '?') { sfprintf(sfstdout, "return=%c option=%s name=%s num=%I*d\n", n, opt_info.option, opt_info.name, sizeof(opt_info.number), opt_info.number); error(ERROR_USAGE|4, "%s", opt_info.arg); } else if (n == ':') { sfprintf(sfstdout, "return=%c option=%s name=%s num=%I*d", n, opt_info.option, opt_info.name, sizeof(opt_info.number), opt_info.number); if (!opt_info.option[0]) sfprintf(sfstdout, " str=%s", argv[opt_info.index - 1]); sfputc(sfstdout, '\n'); error(2, "%s", opt_info.arg); } else if (n > 0) sfprintf(sfstdout, "return=%c option=%s name=%s arg%-.1s=%s num=%I*d\n", n, opt_info.option, opt_info.name, &opt_info.assignment, opt_info.arg, sizeof(opt_info.number), opt_info.number); else sfprintf(sfstdout, "return=%d option=%s name=%s arg%-.1s=%s num=%I*d\n", n, opt_info.option, opt_info.name, &opt_info.assignment, opt_info.arg, sizeof(opt_info.number), opt_info.number); if (extra) { for (n = 0; n < ext; n += 2) optget(NiL, extra[n]); extra = 0; } } if (!str && *(argv += opt_info.index)) while (command = *argv++) { if (loop) sfprintf(sfstdout, "[%d] ", loop); sfprintf(sfstdout, "argument=%d value=\"%s\"\n", ++str, command); } if (--loop <= 0) break; argv = oargv; str = ostr; opt_info.index = 0; } return error_info.errors != 0; }
// prints the required arguments first (if any) then the optional // ones (if any) ostream& CommandOptionParser::displayUsage(ostream& out, bool doPretty) { CommandOptionVec::size_type index; CommandOption *trailing = NULL; char *colch = getenv("COLUMNS"); int columns = 80; unsigned maxlen = 0; if (colch) { string colStr(colch); columns = asInt(colStr); } // find the trailing argument if any, and max option string length for (index = 0; index < optionVec.size(); index++) { if (optionVec[index]->optType == CommandOption::trailingType) trailing = optionVec[index]; else if (optionVec[index]->optType == CommandOption::stdType) maxlen = std::max(maxlen, unsigned(optionVec[index]->getFullOptionString().length())); } out << "Usage: " << progName; if (hasRequiredArguments || hasOptionalArguments) out << " [OPTION] ..."; if (trailing) out << " " << trailing->description; out << endl << (doPretty ? prettyPrint(text,"\n","","",columns) : text); // << endl // << endl // << "Command options:" << endl; for(int required = 1; required >= 0; required--) { if (required==1 && hasRequiredArguments) out << endl << "Required arguments:" << endl; else if (required==0 && hasOptionalArguments) out << endl << "Optional arguments:" << endl; for(index = 0; index < optionVec.size(); index++) { if ((optionVec[index]->required == (required==1)) && (optionVec[index]->optType == CommandOption::stdType)) { string optstr(optionVec[index]->getFullOptionString()); string desc(optionVec[index]->description); string indent(maxlen, ' '); if(doPretty) { leftJustify(optstr, maxlen); prettyPrint(desc, "\n", indent, optstr, columns); } out << desc; if(!doPretty) out << endl; } } } return out; }
static Dssmeth_t* textmeth(const char* name, const char* options, const char* schema, Dssdisc_t* disc, Dssmeth_t* ometh) { register Text_t* text; register Dssmeth_t* meth; register Cxvariable_t* var; register char* s; register char* t; register char* f; register int c; char* d; int p; int index; if (options) { if (dssoptlib(ometh->cx->buf, &dss_lib_text, usage, disc)) goto drop; s = sfstruse(ometh->cx->buf); for (;;) { switch (optstr(options, s)) { case '?': if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg); goto drop; case ':': if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg); goto drop; } break; } } if (!schema || !*schema) return ometh; if (!(meth = newof(0, Dssmeth_t, 1, sizeof(Text_t) + strlen(name) + 2 * strlen(schema) + 2))) { free(meth); if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "out of space"); return 0; } *meth = *ometh; meth->data = text = (Text_t*)(meth + 1); text->format = strcopy(text->name, name) + 1; index = 0; s = (char*)schema; f = text->format; for (;;) { switch (c = *s++) { case 0: break; case '%': *f++ = '%'; var = 0; switch (c = *s++) { case 0: goto invalid; case 'h': case 'l': case 'L': case '+': case '-': case '.': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': continue; case '%': *f++ = '%'; continue; case '(': t = f; d = 0; p = 1; for (;;) { switch (c = *s++) { case 0: goto invalid; case '(': p++; *t++ = c; continue; case ')': if (!--p) break; *t++ = c; continue; case ':': if (d) *t++ = c; else { *t++ = 0; d = t; } continue; default: *t++ = c; continue; } break; } *t = 0; if (dtmatch(meth->cx->variables, f)) { if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%s: duplicate field", f); goto drop; } if (!(var = newof(0, Cxvariable_t, 1, t - f + 1))) { if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "out of space"); goto drop; } var->index = index; t = strcopy((char*)(var->name = (char*)(var + 1)), f); if (d) var->description = strcpy(t + 1, d); break; } for (;;) { switch (c = *s++) { case 0: goto invalid; case 'h': case 'l': case 'L': case '+': case '-': case '.': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': continue; } break; } if (var) { switch (c) { case 'd': case 'f': case 'g': case 'n': case 'o': case 'u': case 'x': var->type = (Cxtype_t*)"number"; break; case 'i': var->type = (Cxtype_t*)"ipaddr_t"; break; case 's': var->type = (Cxtype_t*)"string"; break; case 't': var->type = (Cxtype_t*)"time_t"; break; default: if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%c: invalid field format >>>%s", c, s - 1); goto drop; } if (cxaddvariable(meth->cx, var, disc)) goto drop; } index++; *f++ = c; continue; case ' ': case '\t': case '\n': if (f == text->format || *(f - 1) != ' ') *f++ = ' '; continue; default: *f++ = c; continue; } break; } if (!(text->vars = index)) goto invalid; *f = 0; dtinsert(meth->formats, &text_format); for (c = 0; c < elementsof(local_callouts); c++) if (cxaddcallout(meth->cx, &local_callouts[c], disc)) return 0; return meth; invalid: if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "%s: invalid schema", schema); drop: free(meth); return 0; }