pub struct ArgParser { /* private fields */ }Expand description
The argument parser. Holds the schema (flags, options, positionals, subcommands) and provides methods to parse arguments, generate help text, and serialize/deserialize the schema.
Constructed via ArgBuilder::build().
Implementations§
Source§impl ArgParser
impl ArgParser
Sourcepub fn generate_completions(&self, shell: Shell) -> String
pub fn generate_completions(&self, shell: Shell) -> String
Generate a shell completion script for the given shell.
Returns a self-contained script string that, when sourced or installed, provides tab-completion for your CLI’s flags, options, and subcommands.
§Shell-specific behaviour
| Shell | Descriptions | Short/long exclusion | Script format |
|---|---|---|---|
| Bash | No | No | complete -F function |
| Zsh | Yes | Yes | #compdef + _arguments |
| Fish | Yes | No | complete -c commands |
| PowerShell | Yes | No | Register-ArgumentCompleter |
§Example
use nanoargs::{ArgBuilder, Flag, Shell};
let parser = ArgBuilder::new()
.name("myapp")
.flag(Flag::new("verbose").short('v').desc("Enable verbose output"))
.build()
.unwrap();
let script = parser.generate_completions(Shell::Bash);
assert!(script.contains("complete -F"));Source§impl ArgParser
impl ArgParser
Sourcepub fn program_name(&self) -> Option<&str>
pub fn program_name(&self) -> Option<&str>
Returns the program name, if set.
Sourcepub fn program_desc(&self) -> Option<&str>
pub fn program_desc(&self) -> Option<&str>
Returns the program description, if set.
Sourcepub fn positionals(&self) -> &[PositionalDef]
pub fn positionals(&self) -> &[PositionalDef]
Returns the registered positional definitions.
Sourcepub fn subcommands(&self) -> &[SubcommandDef]
pub fn subcommands(&self) -> &[SubcommandDef]
Returns the registered subcommand definitions.
Sourcepub fn version_text(&self) -> Option<String>
pub fn version_text(&self) -> Option<String>
Returns formatted version text, or None if no version is configured.
Sourcepub fn parse(&self, args: Vec<String>) -> Result<ParseResult, ParseError>
pub fn parse(&self, args: Vec<String>) -> Result<ParseResult, ParseError>
Parse a list of argument strings.
Returns Err(ParseError::HelpRequested(_)) or
Err(ParseError::VersionRequested(_)) when -h/--help or
-V/--version are encountered — these are not errors per se,
but signal that the caller should print the contained text and exit.
Sourcepub fn parse_env(&self) -> Result<ParseResult, ParseError>
pub fn parse_env(&self) -> Result<ParseResult, ParseError>
Parse arguments directly from std::env::args(), skipping the
program name (argv[0]).
This is the recommended entry point for real CLI applications.
Returns Err(ParseError::InvalidUtf8(_)) if any argument contains
bytes that are not valid UTF-8.