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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// #![feature(doc_cfg)]
//! # Clapi
//!
//! Clapi (Command-Line API) is a framework for create command line applications.
//!
//! Currently clapi provides several methods for create command line applications:
//! - Parsing the arguments
//! - Function handlers
//! - Macros
//! - Macros attributes
//!
//! See the examples below creating the same app using the 4 methods.
//!
//! ## Parsing the arguments
//! ```no_run
//! use clapi::{Command, CommandOption, Argument, CommandLine};
//! use clapi::validator::validate_type;
//! use std::num::NonZeroUsize;
//!
//! fn main() -> clapi::Result<()> {
//! let command = Command::new("echo")
//! .version("1.0")
//! .description("outputs the given values on the console")
//! .arg(Argument::one_or_more("values"))
//! .option(
//! CommandOption::new("times")
//! .alias("t")
//! .description("number of times to repeat")
//! .arg(
//! Argument::new()
//! .validator(validate_type::<NonZeroUsize>())
//! .validation_error("expected number greater than 0")
//! .default(NonZeroUsize::new(1).unwrap()),
//! ),
//! ).handler(|opts, args| {
//! let times = opts.convert::<usize>("times").unwrap();
//! let values = args.get_raw_args()
//! .map(|s| s.to_string())
//! .collect::<Vec<String>>()
//! .join(" ") as String;
//!
//! for _ in 0..times {
//! println!("{}", values);
//! }
//!
//! Ok(())
//! });
//!
//! CommandLine::new(command)
//! .use_default_help()
//! .use_default_suggestions()
//! .run()
//! .map_err(|e| e.exit())
//! }
//! ```
//!
//! ## Function handlers
//! ```no_run
//! use clapi::validator::validate_type;
//! use clapi::{Argument, Command, CommandLine, CommandOption};
//!
//! fn main() -> clapi::Result<()> {
//! let command = Command::new("MyApp")
//! .subcommand(
//! Command::new("repeat")
//! .arg(Argument::one_or_more("values"))
//! .option(
//! CommandOption::new("times").alias("t").arg(
//! Argument::with_name("times")
//! .validator(validate_type::<u64>())
//! .default(1),
//! ),
//! )
//! .handler(|opts, args| {
//! let times = opts.get_arg("times").unwrap().convert::<u64>()?;
//! let values = args
//! .get("values")
//! .unwrap()
//! .convert_all::<String>()?
//! .join(" ");
//! for _ in 0..times {
//! println!("{}", values);
//! }
//! Ok(())
//! }),
//! );
//!
//! CommandLine::new(command)
//! .use_default_suggestions()
//! .use_default_help()
//! .run()
//! }
//! ```
//! ## Macro
//!```no_run
//! use std::num::NonZeroUsize;
//!
//! fn main() -> clapi::Result<()> {
//! let cli = clapi::app!{ echo =>
//! (version => "1.0")
//! (description => "outputs the given values on the console")
//! (@option times =>
//! (alias => "t")
//! (description => "number of times to repeat")
//! (@arg =>
//! (type => NonZeroUsize)
//! (default => NonZeroUsize::new(1).unwrap())
//! (error => "expected number greater than 0")
//! )
//! )
//! (@arg values => (count => 1..))
//! (handler (times: usize, ...args: Vec<String>) => {
//! let values = args.join(" ");
//! for _ in 0..times {
//! println!("{}", values);
//! }
//! })
//! };
//!
//! cli.use_default_suggestions()
//! .use_default_help()
//! .run()
//! .map_err(|e| e.exit())
//! }
//!```
//!
//! ## Macro attributes
//! Requires `macros` feature enable.
//!
//! ```no_run compile_fail
//! use clapi::macros::*;
//! use std::num::NonZeroUsize;
//!
//! #[command(name="echo", description="outputs the given values on the console", version="1.0")]
//! #[arg(values, min=1)]
//! #[option(times,
//! alias="t",
//! description="number of times to repeat",
//! default=1,
//! error="expected number greater than 0"
//! )]
//! fn main(times: NonZeroUsize, values: Vec<String>) {
//! let values = values.join(" ");
//!
//! for _ in 0..times.get() {
//! println!("{}", values);
//! }
//! }
//! ```
/// Utilities and extensions intended for internal use.
/// Utilities for provide suggestions.
/// Utilities for provide commands help information.
/// Representation of the command-line command, option and args.
/// Converts the command-line arguments into tokens.
/// Provides the `Validator` trait used for validate the values of an `Argument`.
/// Exposes the `struct Type` for arguments type checking.
// Re-exports
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// Clapi macros
/// Macro attributes for create command-line apps. Require `macros` feature enable.
// #[doc(cfg(feature = "macros"))]
pub use *;
/// Utilities intended for internal use.