Struct yaccas::arguments::Value
[−]
[src]
pub struct Value { /* fields omitted */ }
An argument which represents a value of a specific type.
Example
use yaccas::arguments::{Argument, Value}; use yaccas::parser::{Parser, Result}; use yaccas::scanner::Unix; // This option will be modified by the value let mut will_be_46 = 0u32; { let mut parser = Parser::default(); let value = Value::new::<u32>(); parser.register(&["val"], Argument::with_callback(value, | value | { will_be_46 = value.get_value::<u32>().expect("This will only be executed if the parsing was successful!"); })); assert_eq!(parser.parse(Unix::new(&["-val", "46"])), Result::Success(Vec::new())); } assert_eq!(will_be_46, 46u32);Run
Methods
impl Value
[src]
fn new<T: FromStr + Any + 'static>() -> Value
Creates a new value of a specific type T
which implements FromStr
.
Example
use yaccas::arguments::Value; let value = Value::new::<u32>();Run
fn with_default<T: FromStr + Any + 'static, S: AsRef<str>>(value: S) -> Option<Value>
Creates a new value with a default value.
Example
use yaccas::arguments::Value; assert!(Value::with_default::<u32, _>("46").is_some()); assert!(Value::with_default::<u32, _>("Not a number!").is_none());Run
fn set_value<S: AsRef<str>>(&mut self, value: S) -> bool
Set the value if it is valid.
Example
use yaccas::arguments::Value; let mut value = Value::new::<u32>(); assert_eq!(value.set_value("Invalid value"), false); assert_eq!(value.get_value::<u32>(), None); assert_eq!(value.set_value("46"), true); assert_eq!(value.get_value::<u32>(), Some(46));Run
fn get_value<T: FromStr + Any + 'static>(&self) -> Option<T>
Returns the value from type T
if possible.
Hint
The type T
is checked at runtime to be identical which that from new
.
Example
use yaccas::arguments::Value; let value = Value::with_default::<u32, _>("46").expect("Well, 46 is a number..."); assert_eq!(value.get_value::<u32>(), Some(46)); assert_eq!(value.get_value::<u8>(), None); // See hintRun
fn has_value(&self) -> bool
Returns if the argument has currently a value.