pub enum Argument<'a> {
Long(&'a str),
Short(char),
Value(Cow<'a, str>),
Stdio,
}Expand description
Represents a parsed command-line argument.
Each argument parsed by Parser::forward is represented as one of these variants.
The parser automatically categorizes arguments based on their prefix and structure.
§Examples
use sap::{Parser, Argument};
let mut parser = Parser::from_arbitrary(["prog", "--verbose", "-x", "file.txt"]).unwrap();
assert_eq!(parser.forward().unwrap(), Some(Argument::Long("verbose")));
assert_eq!(parser.forward().unwrap(), Some(Argument::Short('x')));
assert_eq!(parser.forward().unwrap(), Some(Argument::Value("file.txt".into())));Variants§
Long(&'a str)
A long option starting with -- (e.g., --verbose, --file).
Long options can have associated values via --option=value syntax.
Use Parser::value after parsing to retrieve the value if present.
Short(char)
A short option starting with - followed by a single character (e.g., -v, -x).
Short options can be combined (-abc becomes three separate Short arguments).
They cannot have values attached with = syntax, but can consume the next argument as a value.
Value(Cow<'a, str>)
A positional argument or operand (e.g., file.txt, /path/to/file).
This includes any argument that doesn’t start with - or --, as well as
all arguments following the -- terminator.
Stdio
The special - argument, typically representing stdin/stdout.
This is commonly used in Unix tools to indicate reading from standard input or writing to standard output.
Implementations§
Source§impl<'a> Argument<'a>
impl<'a> Argument<'a>
Sourcepub fn into_error<A>(self, value: A) -> ParsingError
pub fn into_error<A>(self, value: A) -> ParsingError
Converts this argument into a ParsingError::UnexpectedArg error.
This is a convenience method for creating contextual error messages when an argument is encountered but not expected by the application. The resulting error message includes appropriate formatting based on the argument type.
§Parameters
value- Optional value associated with the argument (primarily used with options)
§Examples
use sap::Argument;
// Long option with value
let arg = Argument::Long("unknown");
let error = arg.into_error(Some("value"));
assert_eq!(error.to_string(), "unexpected argument: --unknown=value");
// Short option without value
let arg = Argument::Short('x');
let error = arg.into_error(None::<&str>);
assert_eq!(error.to_string(), "unexpected argument: -x");
// Stdio argument
let arg = Argument::Stdio;
let error = arg.into_error(None::<&str>);
assert_eq!(error.to_string(), "unexpected argument: -");