-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
157 lines (124 loc) · 4.8 KB
/
lib.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
/*!
# Argyle
[](https://docs.rs/argyle/)
[](https://github.com/Blobfolio/argyle/blob/master/CHANGELOG.md)<br>
[](https://crates.io/crates/argyle)
[](https://github.com/Blobfolio/argyle/actions)
[](https://deps.rs/repo/github/blobfolio/argyle)<br>
[](https://en.wikipedia.org/wiki/WTFPL)
[](https://github.com/Blobfolio/argyle/issues)
This crate provides a simple streaming CLI argument parser/iterator called [`Argue`], offering a middle ground between the standard library's barebones [`std::env::args_os`] helper and full-service crates like [clap](https://crates.io/crates/clap).
[`Argue`] performs some basic normalization — it handles string conversion in a non-panicking way, recognizes shorthand value assignments like `-kval`, `-k=val`, `--key=val`, and handles end-of-command (`--`) arguments — and will help identify any special keys/values expected by your app.
The subsequent validation and handling, however, are left _entirely up to you_. Loop, match, and proceed however you see fit.
If that sounds terrible, just use [clap](https://crates.io/crates/clap) instead. Haha.
## Crate Features
The non-default **`try_paths`** feature can be enabled to expose an additional `Argument::Path` variant, used for unassociated-and-unrecognized values for which `std::fs::exists() == Ok(true)`.
## Example
A general setup might look something like the following.
Refer to the documentation for [`Argue`], [`KeyWord`], and [`Argument`] for more information, caveats, etc.
```
use argyle::{Argument, KeyWord};
use std::path::PathBuf;
#[derive(Debug, Clone, Default)]
/// # Configuration.
struct Settings {
threads: usize,
verbose: bool,
paths: Vec<PathBuf>,
}
let args = argyle::args()
.with_keywords([
KeyWord::key("-h").unwrap(), // Boolean flag (short).
KeyWord::key("--help").unwrap(), // Boolean flag (long).
KeyWord::key_with_value("-j").unwrap(), // Expects a value.
KeyWord::key_with_value("--threads").unwrap(),
]);
// Loop and handle!
let mut settings = Settings::default();
for arg in args {
match arg {
// Help flag match.
Argument::Key("-h" | "--help") => {
println!("Help Screen Goes Here.");
return;
},
// Thread option match.
Argument::KeyWithValue("-j" | "--threads", value) => {
settings.threads = value.parse()
.expect("Maximum threads must be a number!");
},
// Something else.
Argument::Other(v) => {
settings.paths.push(PathBuf::from(v));
},
// Also something else, but not String-able. PathBuf doesn't care
// about UTF-8, though, so it might be fine!
Argument::InvalidUtf8(v) => {
settings.paths.push(PathBuf::from(v));
},
// Nothing else is relevant here.
_ => {},
}
}
// Now that you're set up, do stuff…
```
*/
#![forbid(unsafe_code)]
#![deny(
clippy::allow_attributes_without_reason,
clippy::correctness,
unreachable_pub,
)]
#![warn(
clippy::complexity,
clippy::nursery,
clippy::pedantic,
clippy::perf,
clippy::style,
clippy::allow_attributes,
clippy::clone_on_ref_ptr,
clippy::create_dir,
clippy::filetype_is_file,
clippy::format_push_string,
clippy::get_unwrap,
clippy::impl_trait_in_params,
clippy::lossy_float_literal,
clippy::missing_assert_message,
clippy::missing_docs_in_private_items,
clippy::needless_raw_strings,
clippy::panic_in_result_fn,
clippy::pub_without_shorthand,
clippy::rest_pat_in_fully_bound_structs,
clippy::semicolon_inside_block,
clippy::str_to_string,
clippy::string_to_string,
clippy::todo,
clippy::undocumented_unsafe_blocks,
clippy::unneeded_field_pattern,
clippy::unseparated_literal_suffix,
clippy::unwrap_in_result,
macro_use_extern_crate,
missing_copy_implementations,
missing_docs,
non_ascii_idents,
trivial_casts,
trivial_numeric_casts,
unused_crate_dependencies,
unused_extern_crates,
unused_import_braces,
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
mod argue;
mod flag;
mod key;
pub use argue::{
args,
Argue,
ArgueEnv,
Argument,
};
pub use flag::FlagsBuilder;
pub use key::{
KeyWord,
KeyWordsBuilder,
};