1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#[derive(Debug)]
pub enum OptError {
InvalidOpt(String),
}
#[derive(Clone, Debug)]
pub struct Opt {
pub name: String,
pub short: String,
pub long: String,
pub desc: Option<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,
desc: None,
}
}
/// 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,
desc: None,
}
}
/// 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
}
/// Sets the description for the option. This is displayed when listing via help commands
///
/// Example:
/// ```rust
/// use arkham::{Opt, App};
/// App::new()
/// .opt(
/// Opt::scalar("user")
/// .short("u")
/// .long("user")
/// .desc("The user to perform the action against")
/// );
///```
pub fn desc(mut self, desc: &str) -> Self {
self.desc = Some(desc.into());
self
}
pub(crate) fn usage(&self) -> String {
match self.kind {
OptKind::Flag => {
format!("-{}, --{}", self.short, self.long)
}
OptKind::String => {
format!("-{} [value], --{} [value]", self.short, self.long)
}
}
}
}
#[derive(Clone, Debug)]
pub(crate) enum OptKind {
Flag,
String,
}