Skip to main content

FromArg

Trait FromArg 

Source
pub trait FromArg: Sized {
    const CHOICES: &'static [&'static str] = _;
    const HINT: Hint = crate::complete::Hint::Nothing;

    // Required method
    fn from_arg(value: &BStr) -> Result<Self, BoxError>;
}
Expand description

A type a flag’s value can be converted into.

Values arrive as bytes borrowed from the command line. Conversion is the only place a value can fail, so a command line that is not valid UTF-8 still parses — flags still match — and only a value that is actually converted to text can be rejected for it.

For a type that is FromStr, Parsed<T> needs no impl. Otherwise:

use winnow_args::error::BoxError;
use winnow_args::{BStr, FromArg};

/// `WIDTHxHEIGHT`.
struct Size(u32, u32);

impl FromArg for Size {
    fn from_arg(value: &BStr) -> Result<Self, BoxError> {
        let (w, h) = std::str::from_utf8(value)?
            .split_once('x')
            .ok_or("expected WIDTHxHEIGHT")?;
        Ok(Size(w.parse()?, h.parse()?))
    }
}

let Size(w, h) = Size::from_arg(BStr::new("80x24")).unwrap();
assert_eq!((w, h), (80, 24));

Provided Associated Constants§

Source

const CHOICES: &'static [&'static str] = _

The values accepted, when there is a fixed set: help lists them as [possible values: …]. Empty by default; #[derive(ValueEnum)] fills it with each visible variant’s name.

Source

const HINT: Hint = crate::complete::Hint::Nothing

What the value is, for the shell that completes it, when a field states no hint. Nothing a shell knows by default: a number, an address, a type of the program. A PathBuf, a String and an OsString are a file: a word is one more often than not, and a shell that offers files stops no other word from being typed.

Required Methods§

Source

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Convert one value.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl FromArg for IpAddr

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for Ipv4Addr

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for Ipv6Addr

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for OsString

Source§

const HINT: Hint = crate::complete::Hint::Files

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for PathBuf

Source§

const HINT: Hint = crate::complete::Hint::Files

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for SocketAddr

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for String

Source§

const HINT: Hint = crate::complete::Hint::Files

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for Vec<u8>

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for bool

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for char

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for f32

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for f64

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for i8

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for i16

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for i32

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for i64

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for i128

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for isize

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for u8

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for u16

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for u32

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for u64

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for u128

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Source§

impl FromArg for usize

Source§

fn from_arg(value: &BStr) -> Result<Self, BoxError>

Implementors§

Source§

impl FromArg for CInt<i8>

Source§

impl FromArg for CInt<i16>

Source§

impl FromArg for CInt<i32>

Source§

impl FromArg for CInt<i64>

Source§

impl FromArg for CInt<isize>

Source§

impl FromArg for CInt<u8>

Source§

impl FromArg for CInt<u16>

Source§

impl FromArg for CInt<u32>

Source§

impl FromArg for CInt<u64>

Source§

impl FromArg for CInt<usize>

Source§

impl FromArg for Shell

So a program’s own --completions <SHELL> flag can be a Shell.

Source§

const CHOICES: &'static [&'static str]

Source§

impl<K: FromArg, V: FromArg> FromArg for KeyValue<K, V>

Source§

impl<T> FromArg for Parsed<T>
where T: FromStr, T::Err: Into<BoxError>,