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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Arg is simple command line argument parser, without any dependencies
//!
//!# Features
//!
//!- `std` - Enables utilities that require `std` library.
//!
//! # Syntax
//!
//! ## Struct
//!
//! ### Arg
//!
//! - `infer_name` - Specifies to insert binary name/version as combination of `env!("CARGO_PKG_NAME")` and `env!("CARGO_PKG_VERSION")`. Applicable only to `struct`
//!
//! ## Fields
//!
//! ### Arg
//!
//! - `short` - Specifies that it is flag with short switch. Optionally can be supplied with flag.
//! - `long` - Specifies that it is flag with long switch. Optionally can be supplied with flag.
//! - `default_value` - Specifies default value to use. Can be supplied with initialization expression as string. Otherwise uses Default trait.
//! - `required` - Specifies whether argument is required. By default all arguments are optional. But booleans cannot be marked as `required`
//! - `sub` - Specifies field to be sub-command. There can be only one sub-command and it is mutually exclusive with `Vec<_>` argument to collect rest of arguments. All other options are not applied to `sub` type of field.
//!
//! ### Types
//!
//! - Flag - is `bool` switch, automatically selected when `bool` is type of argument. Each time flag is supplied it results in `!previous_state`
//! - Option - switch that accepts value. Used for any non-Vec type. Automatically overrides.
//! - Multi Option - switch with `Vec<T>` type, which allows to accumulate multiple values of switch.
//! - Argument - Plain argument that takes value.
//! - Multi argument - Collection of arguments that accumulates into `Vec<T>`, there can be only one.
//! - Sub-command - Propagates rest of arguments to another parser, there ca be only one.
//!
//! ### Conversion
//!
//! By default all types, aside from `bool` flags use `FromStr::from_str` to parse value from string.
//!
//! ### Optional
//!
//! If type is `Option<T>` then argument is assumed to be optional, in which case it cannot be
//! marked with `required` or `default_value`
//!
//! As result, not providing argument shall not fail parser.
//!
//! ### Sub-command
//!
//! It relies on enum to represent sub-commands.
//!
//! Note that when sub-command is used, it is no longer possible to collect multiple arguments into array, resulting in compilation error.
//!
//! Sub-command consumes all remaining arguments, so top command flags/options must be passed prior sub-command invocation.
//!
//! ```rust
//! use arg::Args;
//!
//! #[derive(Args, Debug)]
//! ///First
//! struct First {
//! #[arg(short, long)]
//! ///About this flag
//! flag: bool,
//!
//! #[arg(short = "v", long = "velocity", default_value = "42")]
//! ///This is felocity. Default value is 42.
//! speed: u32,
//! }
//!
//! #[derive(Args, Debug)]
//! ///Second
//! struct Second {
//! #[arg(short = "v", long = "velocity", default_value = "42")]
//! ///This is velocity. Default value is 42.
//! speed: u32,
//! ///To store rest of paths
//! paths: Vec<String>,
//! }
//!
//! #[derive(Args, Debug)]
//! ///My subcommand with implicit command 'help` to list commands
//! enum MySubCommand {
//! ///my first command
//! First(First),
//! ///my second command
//! Second(Second),
//! }
//!
//! #[derive(Args, Debug)]
//! struct MyArgs {
//! #[arg(short, long)]
//! ///About this flag
//! verbose: bool,
//! #[arg(sub)]
//! ///My sub command. Use `help` to show list of commands.
//! cmd: MySubCommand
//! }
//! ```
//!
//! # Usage
//!
//! Here is comprehensive example to illustrate all ways to handle flags and options
//!
//! ```rust
//! use arg::Args;
//!
//! #[derive(Args, Debug)]
//! ///my_exe 0.1.0
//! ///About my program
//! ///
//! ///About my program
//! struct MyArgs {
//! #[arg(short, long)]
//! ///About this flag
//! flag: bool,
//!
//! #[arg(long = "verbose")]
//! ///Verbose mode
//! verbose: Option<bool>,
//!
//! #[arg(short = "v", long = "velocity", default_value = "42")]
//! ///This is velocity. Default value is 42.
//! speed: u32,
//!
//! #[arg(short = "g", long = "gps")]
//! ///GPS coordinates.
//! gps: Vec<u32>,
//!
//! #[arg(short, long, default_value = "\"./address.txt\".to_owned()")]
//! ///Extra to show how to set default string value
//! extra: String,
//!
//! ///To store path
//! path: String,
//!
//! ///To store path 2
//! path2: String,
//!
//! ///To store rest of paths as multi argument collector
//! remain_paths: Vec<String>,
//! }
//!
//! fn main() {
//! match MyArgs::from_text("-v path1 path2") {
//! Ok(args) => println!("args={:?}", args),
//! Err(err) => println!("err={:?}", err),
//! }
//! }
//! ```
//!
extern crate std;
pub use *;
pub use Split;
use fmt;
///Parse errors
///Parse errors
///Describers command line argument parser
///Parses CLI arguments from `std::env::args()`
///
///Requires feature `std`
///
///In case of help it prints help to stdout and exits with return code 0.
///In case of error it prints error to stderr and exits with return code 1.