int OOBase::CmdArgs::parse(int argc, const char* argv[], options_t& options, arguments_t& args, int skip) const { bool bEndOfOpts = false; int err = 0; for (int i=skip; i<argc && err==0; ++i) { if (strcmp(argv[i],"--") == 0) { // -- Terminator bEndOfOpts = true; continue; } if (!bEndOfOpts && argv[i][0]=='-' && argv[i][1] != '\0') { // Options if (argv[i][1] == '-') { // Long option err = parse_long_option(options,argv,i,argc); } else { // Short options err = parse_short_options(options,argv,i,argc); } } else { // Argument String strVal; if (!strVal.assign(argv[i])) err = system_error(); else err = args.push_back(strVal) ? 0 : system_error(); } } return err; }
bool option_parser::parse(int argc, char * argv[]) { build_maps(); int argi = PARSE_STARTING_INDEX; while (argi < argc) { switch (type(argv[argi])) { case ARGUMENT: { parse_arguments(argc, argv, argi); return check_required(); } case SHORT_OPTION: { argi = parse_short_options(argc, argv, argi); if (argi == PARSE_ERROR) { return false; } break; } case LONG_OPTION: { argi = parse_long_option(argc, argv, argi); if (argi == PARSE_ERROR) { return false; } break; } case DELIMITER_OPTION: { parse_arguments(argc, argv, argi + 1); return check_required(); } default: { // huh. out of enumeration? assert(false); } } } return check_required(); }
void parse_args(struct options *const opt, const int argc, const char *const argv[]) { struct argument_flags flags; int i; /* Set override flags */ flags.program_name = basename((char *)argv[0]); flags.conf_file = DEFAULT_CONFIG_FILE; flags.quotes_file = NULL; flags.pid_file = NULL; flags.journal_file = NULL; flags.tproto = PROTOCOL_TNONE; flags.iproto = PROTOCOL_INONE; flags.daemonize = BOOLEAN_UNSET; flags.strict = true; /* Set default options, defined in options.h */ opt->port = DEFAULT_PORT; opt->tproto = DEFAULT_TRANSPORT_PROTOCOL; opt->iproto = DEFAULT_INTERNET_PROTOCOL; opt->quotes_file = DEFAULT_QUOTES_FILE; opt->linediv = DEFAULT_LINE_DIVIDER; opt->pid_file = default_pidfile(); opt->require_pidfile = DEFAULT_REQUIRE_PIDFILE; opt->daemonize = DEFAULT_DAEMONIZE; opt->drop_privileges = DEFAULT_DROP_PRIVILEGES; opt->is_daily = DEFAULT_IS_DAILY; opt->pad_quotes = DEFAULT_PAD_QUOTES; opt->allow_big = DEFAULT_ALLOW_BIG; opt->chdir_root = DEFAULT_CHDIR_ROOT; /* Parse arguments */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "--")) { break; } else if (!strncmp(argv[i], "--", 2)) { parse_long_option(argc, argv, &i, &flags); } else if (argv[i][0] == '-') { const char *next_arg = (i + 1 == argc) ? NULL : argv[i + 1]; parse_short_options(argv[i] + 1, next_arg, &i, &flags); } else { printf("Unrecognized option: %s.\n", argv[i]); usage_and_exit(flags.program_name); } } /* Override config file options */ opt->strict = flags.strict; if (flags.conf_file) { if (flags.conf_file[0] != '/') { opt->chdir_root = false; } parse_config(flags.conf_file, opt); } if (flags.pid_file) { opt->pid_file = strcmp(flags.pid_file, "none") ? flags.pid_file : NULL; } if (flags.quotes_file) { opt->quotes_file = flags.quotes_file; } if (flags.journal_file && !strcmp(flags.journal_file, "-")) { opt->journal_file = flags.journal_file; } else { opt->journal_file = NULL; } if (flags.iproto != PROTOCOL_INONE) { opt->iproto = flags.iproto; } if (flags.tproto != PROTOCOL_TNONE) { opt->tproto = flags.tproto; } if (flags.daemonize != BOOLEAN_UNSET) { opt->daemonize = flags.daemonize; } #if DEBUG journal("\nContents of struct 'opt':\n"); journal("opt = {\n"); journal(" QuotesFile: %s\n", BOOLSTR(opt->quotes_file)); journal(" PidFile: %s\n", opt->pid_file); journal(" Port: %u\n", opt->port); journal(" QuoteDivider: %s\n", name_option_quote_divider(opt->linediv)); journal(" Protocol: %s\n", name_option_protocol(opt->tproto, opt->iproto)); journal(" Daemonize: %s\n", BOOLSTR(opt->daemonize)); journal(" RequirePidfile: %s\n", BOOLSTR(opt->require_pidfile)); journal(" DropPrivileges: %s\n", BOOLSTR(opt->drop_privileges)); journal(" DailyQuotes: %s\n", BOOLSTR(opt->is_daily)); journal(" AllowBigQuotes: %s\n", BOOLSTR(opt->allow_big)); journal(" ChdirRoot: %s\n", BOOLSTR(opt->chdir_root)); journal("}\n\n"); #endif /* DEBUG */ }