-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.rs
437 lines (410 loc) · 13.6 KB
/
macros.rs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#![doc(hidden)]
pub mod const_exprs;
#[macro_export]
#[doc(hidden)]
macro_rules! __parse_arg {
( err => $args:expr, $name:ident, ) => {
let $name = $name.get(&$args);
};
( ok => $args:expr, $name:ident, ) => {
let $name = $name.get(&$args).map(|a| a.ok()).flatten();
};
( => $args:expr, $name:ident, ) => {
let $name = $name
.get(&$args)
.expect("Tried to unwrap argument that wasn't passed")
.expect("Tried to unwrap argument that failed to parse");
};
( err => $args:expr, $name:ident, $default:expr ) => {
let $name = $name
.get(&$args)
.unwrap_or_else(|| ::std::result::Result::Ok($default));
};
( ok => $args:expr, $name:ident, $default:expr ) => {
compile_error!(concat!(
"cannot have default value for argument `#ok ",
stringify!($name),
" = ",
stringify!($default),
"`, only on `#err` or unwrapped arguments",
))
};
( => $args:expr, $name:ident, $default:expr ) => {
let $name = $name
.get(&$args)
.transpose()
.ok()
.flatten()
.unwrap_or_else(|| $default);
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __arg_typ {
( $name:ident, err, $typ:ty, ) => {
$crate::ArgResult<$typ>
};
( $name:ident, err, $typ:ty, $default:expr ) => {
$crate::DefaultedArgResult<$typ>
};
( $name:ident, ok, $typ:ty, ) => {
::std::option::Option<$typ>
};
( $name:ident, ok, $typ:ty, $default:expr ) => {
compile_error!(concat!(
"cannot have default value for argument `#ok ",
stringify!($name),
" = ",
stringify!($default),
"`, only on `#err` or unwrapped arguments",
))
};
( $name:ident, $typ:ty, $( $default:expr )? ) => {
$typ
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __var_tag {
( $long:ident $( $doc:literal )* ) => {{
let mut s = ::std::string::String::new();
$(
s.push_str($doc);
s.push('\n');
)*
s.pop();
$crate::tag::long($crate::__replace!(::std::stringify!($long), '_', '-'))
.doc(s)
}};
( $short:literal $long:ident $( $doc:literal )* ) => {{
let mut s = ::std::string::String::new();
$(
s.push_str($doc);
s.push('\n');
)*
s.pop();
$crate::tag::both($short, $crate::__replace!(::std::stringify!($long), '_', '-'))
.doc(s)
}};
( $long:ident $env:ident $( $doc:literal )* ) => {{
let mut s = ::std::string::String::new();
$(
s.push_str($doc);
s.push('\n');
)*
s.pop();
$crate::tag::long(
$crate::__replace!(::std::stringify!($long), '_', '-')
)
.env(::std::stringify!($env))
.doc(s)
}};
( $short:literal $long:ident $env:ident $( $doc:literal )* ) => {{
let mut s = ::std::string::String::new();
$(
s.push_str($doc);
s.push('\n');
)*
s.pop();
$crate::tag::both($short, $crate::__replace!(::std::stringify!($long), '_', '-'))
.env(::std::stringify!($env))
.doc(s)
}};
}
/// A macro to quickly define your CLI interface with struct-like syntax.
///
/// The syntax looks like this:
///
/// ```plain
/// sarge! {
/// StructName,
/// [fields...]
/// }
/// ```
///
/// Each field has the following form:
/// ```plain
/// [> DOCS...]
/// [#MARKER] [SHORT_FORM] [@ENV_FORM] long_form: type [= DEFAULT],
/// ```
///
/// # Documentation
///
/// You may specify documentation to apply to arguments. On feature `help`,
/// this will also be specified in `print_help`. Example:
///
/// ```plain
/// > "Documentation for argument"
/// (rest of argument)
/// ```
///
/// Whether or not you add documentation, the basic form of the argument will
/// still be provided in the help message.
///
/// # Wrapper markers
///
/// You can specify the type of each argument by prepending a "wrapper" marker,
/// like so:
///
/// ```plain
/// #ok name: type,
/// ```
///
/// There are three kinds of wrappers: `#err`, `#ok`, and none at all. `#err`
/// wraps the type in `Option<Result<T, _>>`: if the argument wasn't passed at
/// all, it's `None`; if it failed to parse, it's `Some(Err(_))`.
///
/// `#ok` wraps it in `Option<T>`: if the argument wasn't passed, or failed to
/// parse, it's `None`.
///
/// No wrapper means that if the argument wasn't passed, or failed to parse,
/// trying to parse your arguments will panic. It gives basic error messages,
/// but this should still be avoided if possible. It is, however, safe to use
/// this marker on `bool` arguments, since they will default to `false`.
///
/// # Short forms
///
/// To specify a short form for your argument, place a character literal after
/// your wrapper marker (if any), like so:
///
/// ```plain
/// #ok 'a' name: type,
/// ```
///
/// Note that you cannot specify an argument that has a short form but no long
/// form. This is for simplicity's sake, and is rarely needed anyways. The
/// long form of your argument is derived from the fields name, with any
/// underscores replaced by dashes at compile-time.
///
/// # Environment variables
///
/// To specify an environment variable form, place the name preceded by an `@`
/// symbol after your short form (if any), like so:
///
/// ```plain
/// #ok 'a' @ENV_FORM name: type,
/// ```
///
/// The name will not be altered in any way, so make sure it's unique and won't
/// clash with any other common environment variables.
///
/// # Defaults
///
/// Sometimes, when an argument is left unspecified, you wish to automatically
/// fill it with a default. This may be done with the following syntax:
///
/// ```plain
/// page: u32 = 1,
/// ```
///
/// This example shows an infallible default, i.e. even if the argument is
/// passed but fails to parse, it will be defaulted. You may instead desire to
/// place the default on an `#err` argument, in which case it will become
/// `Result<T, _>`.
///
/// You may not place defaults on `#ok` arguments.
///
/// # Example
///
/// ```
/// # use sarge::prelude::*;
/// sarge! {
/// // This is the name of our struct.
/// > "This is documentation for our command."
/// Args,
///
/// // These are our arguments. Each will have a long variant matching the
/// // field name one-to-one, with one exception: all underscores are
/// // replaced by dashes at compile-time.
/// //
/// // The hashtags denote the arg 'wrapper'. No wrapper means it will be
/// // unwrapped; if the argument wasn't passed, or it failed to parse, this
/// // will panic. Thankfully, `bool` arguments are immune to both, and
/// // `String` arguments are immune to the latter.
///
/// > "Hello, World!"
/// first: bool, // true if `--first` is passed, false otherwise
///
/// // If you want a short variant (e.g. '-s'), you can specify one with a char
/// // literal before the name (but after the wrapper, if any):
/// 's' second: String,
///
/// // You can also specify an environment variable counterpart. If an argument
/// // has values for both an environment variable and a CLI argument, the CLI
/// // argument takes precedence.
/// @ENV_VAR env_var: i32,
///
/// // `#err` makes the argument an `Option<Result<T, _>>`.
/// #err foo: f32,
///
/// // `#ok` makes the argument an `Option<T>`, discarding any parsing errors.
/// #ok bar: f64,
///
/// // Here's almost every feature in one argument:
/// // an `Option<Result<T, _>>` that can be set via `-b`, `--baz`, or `BAZ=`.
/// #err 'b' @BAZ baz: Vec<u64>,
///
/// // An argument with a default value:
/// qux: String = "foobar".into(),
/// }
///
/// fn main() {
/// let args = [
/// "test", // Usually the name of the executable.
/// "--first",
/// "-s", "Hello, World!",
/// "--bar=badnum", // The syntax `--arg=val` is valid for long tags.
/// "foobar", // This value isn't part of an argument.
/// "--baz", "1,2,3", // Remember this value...
/// ];
///
/// let env = [
/// ("ENV_VAR", "42"),
/// ("BAZ", "4,5,6"), // ...and this one.
/// ];
///
/// // Normally, you would use `::parse()` here. However, since this gets run
/// // as a test, we'll manually pass the arguments along.
/// let (args, remainder) = Args::parse_provided(args, env)
/// .expect("Failed to parse arguments");
///
/// assert_eq!(remainder, vec!["test", "foobar"]);
///
/// assert!(args.first);
/// assert_eq!(args.second, "Hello, World!");
/// assert_eq!(args.env_var, 42);
/// assert_eq!(args.foo, None);
/// assert_eq!(args.bar, None);
/// assert_eq!(args.baz, Some(Ok(vec![1, 2, 3])));
/// assert_eq!(args.qux, "foobar");
/// }
/// ```
#[macro_export]
macro_rules! sarge {
(
$( > $doc:literal )*
$v:vis $name:ident, $(
$( > $adoc:literal )*
$( # $spec:ident )?
$( $short:literal )?
$( @ $env:ident )?
$av:vis
$long:ident : $typ:ty
$( = $default:expr )?
),* $(,)?
) => {
$v struct $name {
$(
$(#[doc = $adoc])*
$av $long: $crate::__arg_typ!($long, $( $spec, )? $typ, $( $default )?),
)*
}
impl $name {
/// Prints help for all the arguments.
///
/// Only available on feature `help`.
#[allow(unused)]
pub fn print_help() {
let mut parser = $crate::ArgumentReader::new();
parser.doc = Some(
String::new()
$( + "\n" + $doc )*
);
$(
parser.add::<$typ>(
$crate::__var_tag!($( $short )? $long $( $env )? $( $adoc )*)
);
)*
parser.print_help();
}
/// Parse arguments from `std::env::{args,vars}`.
///
/// # Errors
///
/// If any arguments fail to parse their values, this
/// will forward that error. Otherwise, see
/// [`ArgParseError`] for a list of all possible errors.
#[allow(unused)]
pub fn parse() -> std::result::Result<(Self, std::vec::Vec<std::string::String>), $crate::ArgParseError> {
Self::parse_provided(
std::env::args(),
std::env::vars(),
)
}
/// Parse the provided arguments as if they were environment variables.
///
/// If `reset == true`, clears the values of all arguments beforehand.
/// You probably want to leave this at `false`, unless you're re-using
/// your parser.
///
/// # Errors
///
/// See [`parse`] for details.
#[allow(unused)]
pub fn parse_env<
K: std::convert::AsRef<str>,
V: std::convert::AsRef<str>,
I: std::iter::IntoIterator<Item = (K, V)>,
>(env: I) -> std::result::Result<Self, $crate::ArgParseError> {
Ok(Self::parse_provided(
std::option::Option::<&'static str>::None,
env,
)?.0)
}
/// Parses the provided arguments as if they were from the CLI.
///
/// If `reset == true`, clears the values of all arguments beforehand.
/// You probably want to leave this at `false`, unless you're re-using
/// your parser.
///
/// # Errors
///
/// See [`parse`] for details.
#[allow(clippy::missing_panics_doc)]
#[allow(unused)]
pub fn parse_cli<
A: std::convert::AsRef<str>,
I: std::iter::IntoIterator<Item = A>,
>(args: I) -> std::result::Result<(Self, std::vec::Vec<std::string::String>), $crate::ArgParseError> {
Self::parse_provided(
args,
std::option::Option::<(&'static str, &'static str)>::None,
)
}
/// Parse from the provided environment variables and CLI arguments.
///
/// # Errors
///
/// See [`parse`] for details.
#[allow(unused)]
pub fn parse_provided<
A: std::convert::AsRef<str>,
IA: std::iter::IntoIterator<Item = A>,
K: std::convert::AsRef<str>,
V: std::convert::AsRef<str>,
IE: std::iter::IntoIterator<Item = (K, V)>,
>(
cli: IA,
env: IE,
) -> std::result::Result<
(Self, std::vec::Vec<std::string::String>), $crate::ArgParseError
>
{
let mut parser = $crate::ArgumentReader::new();
$(
let $long = parser.add::<$typ>(
$crate::__var_tag!($( $short )? $long $( $env )? )
);
)*
let args = parser.parse_provided(cli, env)?;
$(
$crate::__parse_arg!($( $spec )? => args, $long, $( $default )?);
)*
let me = Self {$(
$long,
)*};
Ok((me, args.into()))
}
}
};
}