arkham/src/opt.rs

92 lines
2.0 KiB
Rust

#[derive(Debug)]
pub enum OptError {
InvalidOpt(String),
}
#[derive(Clone, Debug)]
pub struct Opt {
pub name: String,
pub short: String,
pub long: String,
pub(crate) kind: OptKind,
}
impl Opt {
/// Create a boolean opt that is present or not and does not accept additional arguments
///
/// Example:
/// ```rust
/// use arkham::{Opt, App};
/// App::new().opt(Opt::flag("verbose").short("v").long("verbose"));
///```
pub fn flag(name: &str) -> Self {
Self {
name: name.into(),
short: "".into(),
long: "".into(),
kind: OptKind::Flag,
}
}
/// Create a opt that accepts additioanl arguments
///
/// Example:
/// ```rust
/// use arkham::{Opt, App};
/// App::new().opt(Opt::scalar("user").short("u").long("user"));
///```
pub fn scalar(name: &str) -> Self {
Self {
name: name.into(),
short: "".into(),
long: "".into(),
kind: OptKind::String,
}
}
/// Sets the short flag that can be used with -x
///
/// Example:
/// ```rust
/// use arkham::{Opt, App};
/// App::new().opt(Opt::scalar("user").short("u").long("user"));
///```
pub fn short(mut self, short: &str) -> Self {
self.short = short.into();
self
}
/// Sets the long flag that can be used with --xxxxx
///
/// Example:
/// ```rust
/// use arkham::{Opt, App};
/// App::new().opt(Opt::scalar("user").short("u").long("user"));
///```
pub fn long(mut self, long: &str) -> Self {
self.long = long.into();
self
}
}
#[derive(Clone, Debug)]
pub(crate) enum OptKind {
Flag,
String,
}
#[derive(Clone, Debug)]
pub(crate) struct ActiveOpt {
pub definition: Opt,
pub raw_value: Vec<String>,
}
impl ActiveOpt {
pub fn new(definition: Opt, raw_value: Vec<String>) -> Self {
ActiveOpt {
definition,
raw_value,
}
}
}