pub enum ParseError {
UnknownCommand {
command: String,
suggestions: Vec<String>,
},
MissingArgument {
argument: String,
command: String,
suggestion: Option<String>,
},
MissingOption {
option: String,
command: String,
suggestion: Option<String>,
},
TooManyArguments {
command: String,
expected: usize,
got: usize,
suggestion: Option<String>,
},
UnknownOption {
flag: String,
command: String,
suggestions: Vec<String>,
},
TypeParseError {
arg_name: String,
expected_type: String,
value: String,
details: Option<String>,
},
InvalidChoice {
arg_name: String,
value: String,
choices: Vec<String>,
},
InvalidSyntax {
details: String,
hint: Option<String>,
},
}Expand description
Errors when parsing user commands
These errors occur when analyzing arguments provided by the user in CLI or REPL mode.
Variants§
UnknownCommand
Unknown command
The user typed a command that doesn’t exist. Includes suggestions based on Levenshtein distance.
Fields
MissingArgument
Missing required positional argument
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::MissingArgument {
argument: "filename".to_string(),
command: "process".to_string(),
suggestion: Some("Run --help process to see required arguments.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("filename"));Fields
MissingOption
Missing required option
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::MissingOption {
option: "output".to_string(),
command: "export".to_string(),
suggestion: Some("Run --help export to see required options.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("output"));Fields
TooManyArguments
Too many positional arguments
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::TooManyArguments {
command: "run".to_string(),
expected: 1,
got: 3,
suggestion: Some("Run --help run for the expected usage.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("run"));Fields
UnknownOption
Unknown option
Includes similar option suggestions.
Fields
TypeParseError
Type parsing error
The user provided a value that can’t be converted to the expected type (e.g., “abc” for an integer).
Fields
InvalidChoice
Value not in allowed choices
InvalidSyntax
Invalid command syntax
Implementations§
Source§impl ParseError
impl ParseError
Sourcepub fn unknown_command_with_suggestions(
command: &str,
available: &[String],
) -> Self
pub fn unknown_command_with_suggestions( command: &str, available: &[String], ) -> Self
Create an unknown command error with Levenshtein suggestions
Automatically computes similar command names from the available list.
§Arguments
command- The command typed by the useravailable- List of available commands
§Example
use dynamic_cli::error::ParseError;
let available = vec!["simulate".to_string(), "validate".to_string()];
let error = ParseError::unknown_command_with_suggestions("simulat", &available);
match error {
ParseError::UnknownCommand { suggestions, .. } => {
assert!(suggestions.contains(&"simulate".to_string()));
}
_ => panic!("wrong variant"),
}Sourcepub fn unknown_option_with_suggestions(
flag: &str,
command: &str,
available: &[String],
) -> Self
pub fn unknown_option_with_suggestions( flag: &str, command: &str, available: &[String], ) -> Self
Create an unknown option error with Levenshtein suggestions
§Example
use dynamic_cli::error::ParseError;
let available = vec!["--verbose".to_string(), "--output".to_string()];
let error = ParseError::unknown_option_with_suggestions("--verbos", "run", &available);
match error {
ParseError::UnknownOption { suggestions, .. } => {
assert!(suggestions.contains(&"--verbose".to_string()));
}
_ => panic!("wrong variant"),
}Sourcepub fn missing_argument(argument: &str, command: &str) -> Self
pub fn missing_argument(argument: &str, command: &str) -> Self
Create a missing argument error with a help hint
The suggestion automatically refers the user to --help <command>.
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::missing_argument("filename", "process");
match error {
ParseError::MissingArgument { suggestion, .. } => {
assert!(suggestion.is_some());
}
_ => panic!("wrong variant"),
}Sourcepub fn missing_option(option: &str, command: &str) -> Self
pub fn missing_option(option: &str, command: &str) -> Self
Create a missing option error with a help hint
The suggestion automatically refers the user to --help <command>.
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::missing_option("output", "export");
match error {
ParseError::MissingOption { suggestion, .. } => {
assert!(suggestion.is_some());
}
_ => panic!("wrong variant"),
}Sourcepub fn too_many_arguments(command: &str, expected: usize, got: usize) -> Self
pub fn too_many_arguments(command: &str, expected: usize, got: usize) -> Self
Create a too-many-arguments error with a help hint
§Example
use dynamic_cli::error::ParseError;
let error = ParseError::too_many_arguments("run", 1, 3);
match error {
ParseError::TooManyArguments { suggestion, .. } => {
assert!(suggestion.is_some());
}
_ => panic!("wrong variant"),
}Trait Implementations§
Source§impl Debug for ParseError
impl Debug for ParseError
Source§impl Display for ParseError
impl Display for ParseError
Source§impl Error for ParseError
impl Error for ParseError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()