Cote
A simple option manager manage the AOpt, support auto generate help message.
Setup
cargo add cote or add following to your Cargo.toml file:
[dependencies]
cote = "0.4"
Enable Features from aopt
Enable sync feature
If you want the utils of current crate implement Send and Sync, you can enable sync feature.
[dependencies]
cote = { version = "*", features = [ "sync" ] }
Enable utf8 feature
By default, the command line parsing support OsString, enable utf8 using String instead.
[dependencies]
cote = { version = "*", features = [ "utf8" ] }
Documents
See reference for more information.
Example
Using Cote generate struct from command line options.
use aopt::opt::Pos;
use cote::*;
fn main() -> Result<(), CoteError> {
#[derive(Debug, Cote)]
pub struct Cli {
flag: bool,
#[arg(alias = "-n")]
name: String,
#[arg(help = "`Option` mean the option is not force required")]
nick: Option<String>,
#[arg(index = "1")]
from: Pos<String>,
#[pos(index = 2..)]
to: Vec<String>,
}
let cli = Cli::parse(Args::from_array(["app", "-nLily", "src", "foo", "bar"]))?;
assert_eq!(cli.flag, false);
assert_eq!(cli.name, String::from("Lily"));
assert_eq!(cli.nick, None);
assert_eq!(cli.from, Pos(String::from("src")));
assert_eq!(cli.to, vec![String::from("foo"), String::from("bar")]);
let cli = Cli::parse(Args::from_array(["app", "--name", "Lily", "src", "foo", "bar"]))?;
assert_eq!(cli.flag, false);
assert_eq!(cli.name, String::from("Lily"));
assert_eq!(cli.nick, None);
assert_eq!(cli.from, Pos(String::from("src")));
assert_eq!(cli.to, vec![String::from("foo"), String::from("bar")]);
assert!(Cli::parse(Args::from_array(["app", "--nick", "Lily", "src", "foo", "bar"])).is_err());
Ok(())
}
LICENSE
MPL-2.0