コード例 #1
0
ファイル: carg_parser.cpp プロジェクト: mrtuborg/libs
char ap_init( Arg_parser * ap, const int argc, const char * const argv[],
              const ap_Option options[], const char in_order )
  {
  const char ** non_options = 0;	// skipped non-options
  int non_options_size = 0;		// number of skipped non-options
  int argind = 1;			// index in argv
  int i;

  ap->data = 0;
  ap->error = 0;
  ap->data_size = 0;
  ap->error_size = 0;
  if( argc < 2 || !argv || !options ) {

      return 0;
  }

  while( argind < argc )
    {
    const unsigned char ch1 = argv[argind][0];
    const unsigned char ch2 = ( ch1 ? argv[argind][1] : 0 );

    if( ch1 == '-' && ch2 )		// we found an option
      {
      const char * const opt = argv[argind];
      const char * const arg = (argind + 1 < argc) ? argv[argind+1] : 0;
      if( ch2 == '-' )
        {
        if( !argv[argind][2] ) { ++argind; break; }	// we found "--"
        else if( !parse_long_option( ap, opt, arg, options, &argind ) ) return 0;
        }
      else if( !parse_short_option( ap, opt, arg, options, &argind ) ) return 0;
      if( ap->error ) break;
      }
    else
      {
      if( !in_order )
        {
        if( !ap_resize_buffer( (void *)&non_options,
            ( non_options_size + 1 ) * sizeof( *non_options ) ) )
          return 0;
        non_options[non_options_size++] = argv[argind++];
        }
      else if( !push_back_record( ap, 0, argv[argind++] ) ) return 0;
      }
    }
  if( ap->error ) free_data( ap );
  else
    {
    for( i = 0; i < non_options_size; ++i )
      if( !push_back_record( ap, 0, non_options[i] ) ) return 0;
    while( argind < argc )
      if( !push_back_record( ap, 0, argv[argind++] ) ) return 0;
    }
  if( non_options ) free( non_options );
  return 1;
  }
コード例 #2
0
ファイル: isl_arg.c プロジェクト: VanirLLVM/toolchain_isl
static int parse_option(struct isl_arg *decl, char **arg,
	struct isl_prefixes *prefixes, void *opt)
{
	int i;

	for (i = 0; decl[i].type != isl_arg_end; ++i) {
		int parsed = 0;
		switch (decl[i].type) {
		case isl_arg_choice:
			parsed = parse_choice_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_flags:
			parsed = parse_flags_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_int:
			parsed = parse_int_option(&decl[i], arg, prefixes, opt);
			break;
		case isl_arg_long:
			parsed = parse_long_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_ulong:
			parsed = parse_ulong_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_bool:
			parsed = parse_bool_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_str:
			parsed = parse_str_option(&decl[i], arg, prefixes, opt);
			break;
		case isl_arg_str_list:
			parsed = parse_str_list_option(&decl[i], arg, prefixes,
							opt);
			break;
		case isl_arg_child:
			parsed = parse_child_option(&decl[i], arg,
							prefixes, opt);
			break;
		case isl_arg_alias:
		case isl_arg_arg:
		case isl_arg_footer:
		case isl_arg_user:
		case isl_arg_version:
		case isl_arg_end:
			break;
		}
		if (parsed)
			return parsed;
	}

	return 0;
}
コード例 #3
0
ファイル: arg_parser1.2.hpp プロジェクト: sijianwudi/varlib
    Arg_parser( const int argc, const char * const argv[],
                const Option options[], const bool in_order = false )
    {
        if( argc < 2 || !argv || !options ) return;

        std::vector< std::string > non_options;	// skipped non-options
        int argind = 1;				// index in argv

        while( argind < argc )
        {
            const unsigned char ch1 = argv[argind][0];
            const unsigned char ch2 = ( ch1 ? argv[argind][1] : 0 );

            if( ch1 == '-' && ch2 )		// we found an option
            {
                const char * const opt = argv[argind];
                const char * const arg = (argind + 1 < argc) ? argv[argind+1] : 0;
                if( ch2 == '-' )
                {
                    if( !argv[argind][2] )
                    {
                        ++argind;    // we found "--"
                        break;
                    }
                    else if( !parse_long_option( opt, arg, options, argind ) ) break;
                }
                else if( !parse_short_option( opt, arg, options, argind ) ) break;
            }
            else
            {
                if( !in_order ) non_options.push_back( argv[argind++] );
                else
                {
                    data.push_back( Record() );
                    data.back().argument = argv[argind++];
                }
            }
        }
        if( error_.size() ) data.clear();
        else
        {
            for( unsigned i = 0; i < non_options.size(); ++i )
            {
                data.push_back( Record() );
                data.back().argument.swap( non_options[i] );
            }
            while( argind < argc )
            {
                data.push_back( Record() );
                data.back().argument = argv[argind++];
            }
        }
    }
コード例 #4
0
ファイル: ticker.c プロジェクト: ar-nelson/ticker
// Parses command-line arguments into the global 'options'
// struct. Returns false if the arguments are invalid.
bool parse_options(int argc, char** argv)
{
    options.always_scroll = false;
    options.blank   = DEFAULT_BLANK;
    options.length  = DEFAULT_LEN;
    options.reverse = false;
    options.tick    = DEFAULT_TICK;
    int i = 1;
    while (i < argc)
    {
        char* arg = argv[i];
        if (strlen(arg) < 2)
            return false;
        if (arg[0] == '-')
        {
            if (arg[1] == '-')
            {
                if (!parse_long_option(arg))
                {
                    printf("Invalid argument: '%s'\n", arg);
                    return false;
                }
                i += 1;
            }
            else
            {
                size_t len = strlen(arg);
                char* next = (i < argc-1) ? argv[i+1] : NULL;
                uint8_t res;
                for (size_t c=1; c<len; c++)
                {
                    res = parse_short_option(arg[c], next);
                    if (res == 0 || (c < len-1 && res > 1))
                    {
                        printf("Invalid argument or value for '-%c'\n", arg[c]);
                        return false;
                    }
                }
                i += res;
            }
        }
        else
        {
            printf("Invalid argument: '%s'\n", arg);
            return false;
        }
    }
    return true;
}
コード例 #5
0
ファイル: arg_parser.cpp プロジェクト: GulBroz/bfs2pack
Arg_parser::Arg_parser( const char * const opt, const char * const arg,
                        const Option options[] ) throw()
  {
  if( !opt || !opt[0] || !options ) return;

  if( opt[0] == '-' && opt[1] )		// we found an option
    {
    int argind = 1;			// dummy
    if( opt[1] == '-' )
      { if( opt[2] ) parse_long_option( opt, arg, options, argind ); }
    else
      parse_short_option( opt, arg, options, argind );
    if( _error.size() ) data.clear();
    }
  else { data.push_back( Record() ); data.back().argument = opt; }
  }
コード例 #6
0
ファイル: CmdArgs.cpp プロジェクト: jamal-fuma/oobase
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;
}
コード例 #7
0
ファイル: option_parser.cpp プロジェクト: czestmyr/dpp
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();
}
コード例 #8
0
ファイル: parse_cmdline.c プロジェクト: macboy012/RHash
/**
 * Parse command line arguments.
 *
 * @param cmd_line structure to store parsed options data
 */
static void parse_cmdline_options(struct parsed_cmd_line_t* cmd_line)
{
	int argc;
	int n_files = 0, b_opt_end = 0;
	rsh_tchar** files;
	rsh_tchar **parg, **end_arg;
	parsed_option_t *next_opt;

#ifdef _WIN32
	parg = cmd_line->warg = CommandLineToArgvW(GetCommandLineW(), &argc);
	if( NULL == parg || argc < 1) {
		die(_("CommandLineToArgvW failed\n"));
	}
#else
	argc = cmd_line->argc;
	parg = cmd_line->argv;
#endif

	/* allocate array for files */
	files = (rsh_tchar**)rsh_malloc(argc * sizeof(rsh_tchar*));
	end_arg = parg + argc;

	/* loop by program arguments */
	for(parg++; parg < end_arg; parg++)
	{
		/* if argument is not an option */
		if((*parg)[0] != RSH_T('-') || (*parg)[1] == 0 || b_opt_end) {
			/* it's a file, note that '-' is interpreted as stdin */
			files[n_files++] = *parg;
			continue;
		}

		assert((*parg)[0] == RSH_T('-') && (*parg)[1] != 0);
		
		if((*parg)[1] == L'-' && (*parg)[2] == 0) {
			b_opt_end = 1; /* string "--" means end of options */
			continue;
		}

		/* check for "--" */
		if((*parg)[1] == RSH_T('-')) {
			cmdline_opt_t *t;

			/* allocate parsed_option */
			rsh_blocks_vector_add_empty(&cmd_line->options, 16, sizeof(parsed_option_t));
			next_opt = rsh_blocks_vector_get_item(&cmd_line->options, cmd_line->options.size - 1, 16, parsed_option_t);

			/* find the long option */
			parse_long_option(next_opt, &parg);
			t = next_opt->o;

			/* process encoding and -o/-l options early */
			if(is_output_modifier(t->type)) {
				apply_option(&opt, next_opt);
			}
		} else if((*parg)[1] != 0) {
			/* found '-'<some string> */
			rsh_tchar* ptr;
			
			/* parse short options. A string of several characters is interpreted
			 * as separate short options */
			for(ptr = *parg + 1; *ptr; ptr++) {
				cmdline_opt_t *t;
				char ch = (char)*ptr;

#ifdef _WIN32
				if(((unsigned)*ptr) >= 128) {
					ptr[1] = 0;
					fail_on_unknow_option(w2c(ptr));
				}
#endif
				/* allocate parsed_option */
				rsh_blocks_vector_add_empty(&cmd_line->options, 16, sizeof(parsed_option_t));
				next_opt = rsh_blocks_vector_get_item(&cmd_line->options, cmd_line->options.size - 1, 16, parsed_option_t);

				next_opt->buf[0] = '-', next_opt->buf[1] = ch, next_opt->buf[2] = '\0';
				next_opt->name = next_opt->buf;
				next_opt->parameter = NULL;
				
				/* search for the short option */
				for(t = cmdline_opt; t->type && ch != t->short1 && ch != t->short2; t++);
				if(!t->type) fail_on_unknow_option(next_opt->buf);
				next_opt->o = t;
				if(is_param_required(t->type)) {
					next_opt->parameter = (ptr[1] ? ptr + 1 : *(++parg));
					if(!next_opt->parameter) {
						/* note: need to check for parameter here, for early -o/-l options processing */
						log_error(_("argument is required for option %s\n"), next_opt->name);
						rsh_exit(2);
					}
				}

				/* process encoding and -o/-l options early */
				if(is_output_modifier(t->type)) {
					apply_option(&opt, next_opt);
				}
				if(next_opt->parameter) break;  /* a parameter ends the short options string */
			}
		}

	} /* for */

	cmd_line->n_files = n_files;
	cmd_line->files = files;
}
コード例 #9
0
ファイル: arguments.c プロジェクト: ammongit/qotd
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 */
}