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
/*!
> Simplified CLI creation via procedural macros.
**For the time being you must also have structopt as a dependency to use `clickrs`.**
This crate is largely inspired by the `click` Python package which allows for defining command line
interfaces very simply through the use of decorators (https://github.com/pallets/click#a-simple-example).
In `clickrs`, we export a precedural macro to mimic this functionality of click: `command`.
Thanks to the fantastic work done by the `structopt` package and taking inspiration from the Python `fire`
package (https://github.com/google/python-fire#basic-usage), we have taken this a step further. Building
`clickrs` on `structopt` allows us to leverage the argument types in the function wrapped by the `command`
procedural macro, which allows for very clean and succinct CLI definitions.
To be clear, `argument` is not a macro, it's just used as a way to provide a macro-like interface for
`command` to pick up additional options for each argument, so you won't need to import it.
Re-implementing the example CLI in the structopt documention (https://github.com/TeXitoi/structopt/blob/master/README.md#example),
with `clickrs` we can simplify it even further:
```
use clickrs::command;
use std::path::PathBuf;
#[command(name = "basic")]
#[argument("debug", short, long)]
#[argument("verbose", short, long, parse(from_occurrences))]
#[argument("speed", short, long, default_value = "42")]
#[argument("output", short, long, parse(from_os_str))]
#[argument("nb_cars", short = "c", long)]
#[argument("level", short, long)]
#[argument("files", name = "FILE", parse(from_os_str))]
fn main(
debug: bool,
verbose: u8,
speed: f64,
output: PathBuf,
nb_cars: Option<i32>,
level: Vec<String>,
files: Vec<PathBuf>,
) {
println!("{:?}", speed);
}
```
## Defaults without using `argument`
You only need to use `argument` if you need to specify additional options for the CLI inputs. As shown
below, if the defaults provided by `structopt` are what you need, we can use the defaults provided
by `structopt` by foregoing the calls to `argument`. This makes defining a CLI even easier:
```
use clickrs::command;
#[command(
name = "example",
about = "An example of clickrs with defaults for arguments."
)]
fn main(
input: String,
file_name: Option<String>,
) {
println!("{}, {:?}", input, file_name);
}
```
which looks like:
```bash
example 0.1.0
An example of clickrs with defaults for arguments.
USAGE:
clickrs <input> [file-name]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
ARGS:
<input>
<file-name>
```
!*/
pub use command;
/// Re-exports
pub use structopt;
pub use structopt_derive;