Crates.io | prefixopt |
lib.rs | prefixopt |
version | 0.4.0 |
source | src |
created_at | 2017-09-18 10:09:52.146999 |
updated_at | 2017-11-08 11:24:20.851904 |
description | See Read Me. Automatically derive options from structs and enums to use with clap. |
homepage | |
repository | https://github.com/Rupsbant/prefix_opt |
max_upload_size | |
id | 32131 |
size | 29,562 |
Add command line options with a prefix to override fields of structs and enums with a derive macro. This is made to extend clap.
Deriving PrefixOpt for a struct or enum implements the PrefixOpt trait for the struct which allows to create a struct with a prefix. This struct can be transformed into arguments and bound to a clap::App
. The struct allows parsing the ArgMatches
of the App
.
Derivator of PrefixOpt requires that both structs and enums implement Default
.
Add prefixopt
and prefixopt-derive
to your dependencies of your Cargo.toml
:
[dependencies]
prefixopt = "0.1.0"
prefixopt-derive = "0.1.0"
And then, in your rust file:
extern crate prefixopt;
#[macro_use]
extern crate prefixopt_derive;
use prefixopt::core::*;
#[derive(Debug, PrefixOpt)]
pub enum A<T> {
A(Box<T>, Option<Option<u8>>),
B{x:T},
C,
D(),
E(::std::marker::PhantomData<u32>)
}
impl Default for A {
fn default() -> A {
A::A(Box::new(1), None)
}
}
fn main() {
let a_opt = A::<u32>::with_prefix("o");
let app = a_opt.as_arguments().bind_app(clap::App::new("testing"));
let a = a_opt.match_arguments(&app.get_matches());
println!("{:?}", a);
}
I have a program with too much options to encode manually. I liked OpenSSH's approach with -o
which is hopefully automatically generated. I extended the approach to enums and liked the index-like syntax.
In a general order of personal importance.
Dual-licensed.
Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 or the MIT license http://opensource.org/licenses/MIT, at your option. This project may not be copied, modified, or distributed except according to those terms.