Git-Raw

 view release on metacpan or  search on metacpan

deps/libgit2/src/cli/opt.c  view on Meta::CPAN

	         (strncmp(parser->args[parser->idx], "-", 1) == 0 &&
		  !parser->in_literal))
		return parse_short(opt, parser);

	/*
	 * We've reached the first "bare" argument.  In POSIX mode, all
	 * remaining items on the command line are arguments.  In GNU
	 * mode, there may be long or short options after this.  Sort any
	 * options up to this position then re-parse the current position.
	 */
	if (parser->needs_sort && sort_gnu_style(parser))
		return cli_opt_parser_next(opt, parser);

	return parse_arg(opt, parser);
}

GIT_INLINE(int) spec_included(const cli_opt_spec **specs, const cli_opt_spec *spec)
{
	const cli_opt_spec **i;

	for (i = specs; *i; ++i) {
		if (spec == *i)
			return 1;
	}

	return 0;
}

static cli_opt_status_t validate_required(
	cli_opt *opt,
	const cli_opt_spec specs[],
	const cli_opt_spec **given_specs)
{
	const cli_opt_spec *spec, *required;
	int given;

	/*
	 * Iterate over the possible specs to identify requirements and
	 * ensure that those have been given on the command-line.
	 * Note that we can have required *choices*, where one in a
	 * list of choices must be specified.
	 */
	for (spec = specs, required = NULL, given = 0; spec->type; ++spec) {
		if (!required && (spec->usage & CLI_OPT_USAGE_REQUIRED)) {
			required = spec;
			given = 0;
		} else if (!required) {
			continue;
		}

		if (!given)
			given = spec_included(given_specs, spec);

		/*
		 * Validate the requirement unless we're in a required
		 * choice.  In that case, keep the required state and
		 * validate at the end of the choice list.
		 */
		if (!spec_is_choice(spec)) {
			if (!given) {
				opt->spec = required;
				opt->status = CLI_OPT_STATUS_MISSING_ARGUMENT;
				break;
			}

			required = NULL;
			given = 0;
		}
	}

	return opt->status;
}

cli_opt_status_t cli_opt_parse(
	cli_opt *opt,
	const cli_opt_spec specs[],
	char **args,
	size_t args_len,
	unsigned int flags)
{
	cli_opt_parser parser;
	const cli_opt_spec **given_specs;
	size_t given_idx = 0;

	cli_opt_parser_init(&parser, specs, args, args_len, flags);

	given_specs = alloca(sizeof(const cli_opt_spec *) * (args_len + 1));

	while (cli_opt_parser_next(opt, &parser)) {
		if (opt->status != CLI_OPT_STATUS_OK &&
		    opt->status != CLI_OPT_STATUS_DONE)
			return opt->status;

		if ((opt->spec->usage & CLI_OPT_USAGE_STOP_PARSING))
			return (opt->status = CLI_OPT_STATUS_DONE);

		given_specs[given_idx++] = opt->spec;
	}

	given_specs[given_idx] = NULL;

	return validate_required(opt, specs, given_specs);
}

static int spec_name_fprint(FILE *file, const cli_opt_spec *spec)
{
	int error;

	if (spec->type == CLI_OPT_TYPE_ARG)
		error = fprintf(file, "%s", spec->value_name);
	else if (spec->type == CLI_OPT_TYPE_ARGS)
		error = fprintf(file, "%s", spec->value_name);
	else if (spec->alias && !(spec->usage & CLI_OPT_USAGE_SHOW_LONG))
		error = fprintf(file, "-%c", spec->alias);
	else
		error = fprintf(file, "--%s", spec->name);

	return error;
}

int cli_opt_status_fprint(



( run in 2.724 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )