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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/compiler.hlinux/string.hlinux/types.hstdio.hstdlib.hstdint.hstring.hctype.hsubcmd-util.hparse-options.hsubcmd-config.hpager.h
Detected Declarations
function opterrorfunction optwarningfunction get_argfunction get_valuefunction parse_short_optfunction parse_long_optfunction check_typosfunction parse_options_startfunction parse_options_stepfunction parse_options_endfunction parse_options_subcommandfunction parse_optionsfunction print_option_helpfunction option__cmpfunction option__in_argvfunction usage_with_options_internalfunction usage_with_optionsfunction usage_with_options_msgfunction parse_options_usagefunction parse_opt_verbosity_cbfunction find_optionfunction set_option_flagfunction set_option_nobuild
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
- Immediate include surface: `linux/compiler.h`, `linux/string.h`, `linux/types.h`, `stdio.h`, `stdlib.h`, `stdint.h`, `string.h`, `ctype.h`.
- Detected declarations: `function opterror`, `function optwarning`, `function get_arg`, `function get_value`, `function parse_short_opt`, `function parse_long_opt`, `function check_typos`, `function parse_options_start`, `function parse_options_step`, `function parse_options_end`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.