Skip to main content

TypeConverter

Trait TypeConverter 

Source
pub trait TypeConverter:
    Debug
    + Send
    + Sync {
    type Value;

    // Required methods
    fn name(&self) -> &str;
    fn convert(&self, value: &str) -> Result<Self::Value, String>;

    // Provided methods
    fn get_metavar(&self) -> Option<String> { ... }
    fn get_missing_message(&self) -> Option<String> { ... }
    fn split_envvar_value(&self, value: &str) -> Vec<String> { ... }
    fn shell_complete(&self, _incomplete: &str) -> Vec<CompletionItem> { ... }
    fn is_composite(&self) -> bool { ... }
    fn arity(&self) -> usize { ... }
}
Expand description

Trait for parameter types that convert and validate command-line values.

Each type must define how to convert a string value from the command line into the appropriate Rust type.

Required Associated Types§

Source

type Value

The Rust type that this parameter type converts to.

Required Methods§

Source

fn name(&self) -> &str

Returns the descriptive name of this type (used in error messages).

Source

fn convert(&self, value: &str) -> Result<Self::Value, String>

Convert a string value to the target type.

Returns an error message if conversion fails.

Provided Methods§

Source

fn get_metavar(&self) -> Option<String>

Returns the metavar for this type (used in help text).

For example, INT might return "INTEGER".

Source

fn get_missing_message(&self) -> Option<String>

Returns an optional message when a required value is missing.

Source

fn split_envvar_value(&self, value: &str) -> Vec<String>

Split an environment variable value into multiple values.

By default, splits on whitespace. Path-based types override this to split on the platform’s path separator.

Source

fn shell_complete(&self, _incomplete: &str) -> Vec<CompletionItem>

Returns shell completion items for the given incomplete value.

Most types return an empty list; types like Choice and Path can provide completions.

Source

fn is_composite(&self) -> bool

Whether this type is a composite type (like Tuple).

Source

fn arity(&self) -> usize

The arity (number of values consumed) for composite types.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§