tools/lib/subcmd/parse-options.c

Source file repositories/reference/linux-study-clean/tools/lib/subcmd/parse-options.c

File Facts

System
Linux kernel
Corpus path
tools/lib/subcmd/parse-options.c
Extension
.c
Size
25968 bytes
Lines
1091
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (p->excl_opt && p->excl_opt != opt) {
			char msg[128];

			if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
			    p->excl_opt->long_name == NULL) {
				snprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
					 p->excl_opt->short_name);
			} else {
				snprintf(msg, sizeof(msg), "cannot be used with %s",
					 p->excl_opt->long_name);
			}
			opterror(opt, msg, flags);
			return -3;
		}
		p->excl_opt = opt;
	}
	if (!(flags & OPT_SHORT) && p->opt) {
		switch (opt->type) {
		case OPTION_CALLBACK:
			if (!(opt->flags & PARSE_OPT_NOARG))
				break;
			/* FALLTHROUGH */
		case OPTION_BOOLEAN:
		case OPTION_INCR:
		case OPTION_BIT:
		case OPTION_SET_UINT:
		case OPTION_SET_PTR:
			return opterror(opt, "takes no value", flags);
		case OPTION_END:
		case OPTION_ARGUMENT:
		case OPTION_GROUP:
		case OPTION_STRING:
		case OPTION_INTEGER:
		case OPTION_UINTEGER:
		case OPTION_LONG:
		case OPTION_ULONG:
		case OPTION_U64:
		default:
			break;
		}
	}

	if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
		if (!(p->flags & PARSE_OPT_OPTARG_ALLOW_NEXT)) {
			/*
			 * If the option has an optional argument, and the argument is not
			 * provided in the option itself, do not attempt to get it from
			 * the next argument, unless PARSE_OPT_OPTARG_ALLOW_NEXT is set.
			 *
			 * This prevents a non-option argument from being interpreted as an
			 * optional argument of a preceding option, for example:
			 *
			 * $ cmd --opt val
			 * -> is "val" argument of "--opt" or a separate non-option
			 * argument?
			 *
			 * With PARSE_OPT_OPTARG_ALLOW_NEXT, "val" is interpreted as
			 * the argument of "--opt", i.e. the same as "--opt=val".
			 * Without PARSE_OPT_OPTARG_ALLOW_NEXT, --opt is interpreted
			 * as having the default value, and "val" as a separate non-option
			 * argument.
			 *
			 * PARSE_OPT_OPTARG_ALLOW_NEXT is useful for commands that take no
			 * non-option arguments and want to allow more flexibility in
			 * optional argument passing.
			 */
			force_defval = true;
		}

		if (p->argc <= 1 || p->argv[1][0] == '-') {
			/*
			 * If next argument is an option or does not exist,
			 * use the default value.
			 */
			force_defval = true;
		}
	}

	if (opt->flags & PARSE_OPT_NOBUILD) {
		char reason[128];
		bool noarg = false;

		err = snprintf(reason, sizeof(reason),
				opt->flags & PARSE_OPT_CANSKIP ?
					"is being ignored because %s " :
					"is not available because %s",
				opt->build_opt);
		reason[sizeof(reason) - 1] = '\0';

		if (err < 0)

Annotation

Implementation Notes