These are chat archives for kbknapp/clap-rs

14th
Apr 2015
landersson
@landersson
Apr 14 2015 00:59
Just started learning rust and found clap. Looks good. One feature I'm missing is being able to specify a type for arguments, i.e something like: Arg::new("length").type(f64), and have clap automatically verify that what was specified is a valid floating point number, for example. Are you planning on adding something like that?
Kevin K.
@kbknapp
Apr 14 2015 01:01
It's something I keep thinking about, but currently have on the back-burner.
I may add simple types sometime soon (Int, Float, [maybe File, Directory?]), but it's difficult to know where to draw the line
landersson
@landersson
Apr 14 2015 01:05
ok, thanks, I'll keep an eye out for it... I was thinking that trying to add it myself would be a good rust learning excercise, but given very limited amount of spare time, it wont happen soon...
yep, adding support for Int and Float only (in addition to string) would probably cover a large portion of use cases...
Kevin K.
@kbknapp
Apr 14 2015 01:10

In the meantime, it's not too difficult to validate with something like (assuming "length" is required):

matches.value_of("length").unwrap().parse::<f64>().ok().expect(format!("invalid length, must be a valid f64 value\n{}",matches.usage());

Or something similar ;) (man....that's long :P)

Another thing I have planed is ranges (i.e. 0-3) so I think these two features will fit nicely together....I just need to figure out a nice way to implement them
Also, slightly off topic, I just pushed a new feature today of less verbose argument construction using "usage" type strings....if you try it out let me know if you have any issues! ;)
I'm going to go ahead and add typed values (and ranges while I'm at it) to the issue tracker as well. Thanks for hitting me up about it!
landersson
@landersson
Apr 14 2015 01:13
that'd be useful (ranges)... hehe, your line of code above reminds me I better get back to actually learning more of the rust language before diverging too mich :)
ok, cheers
Kevin K.
@kbknapp
Apr 14 2015 18:51
@landersson I just pushed a commit adding some macros for doing what you're talking about...it's not a full implementation, but it allows a wide enough capability for now
Check out the examples/12_TypedValues.rs
The docs are a little messed up right now, I'll fix them tongiht. The example is fine though.
Basic usage is you can now do something like this: (assuming you've got a required argument called "length")
```
let len = value_t_or_exit!(matches.value_of("length"), f64);
let len = value_t_or_exit!(matches.value_of("length"), f64);
Or if you wish to not exit, and do something like provide a default value:
let len = value_t!(matches.value_of("length"), f64).unwrap_or(3.14);
Kevin K.
@kbknapp
Apr 14 2015 18:57
Hope this helps for the time being until something more permanent
comes along ;)