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
#![deny(unsafe_code)]
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
mod clap;
mod config;
mod error;
mod readline;
#[doc(hidden)]
pub mod shlex;
use crate::readline::Readline;
pub use config::{ColorMode, CompletionType, Config};
pub use error::{ClapError, Error, ErrorKind, ExecutionError, MismatchedQuotes, ReadlineError};
#[derive(Debug)]
pub struct Slaps<'a, 'b> {
config: Config<'b>,
matcher: clap::Matcher<'a, 'b>,
}
impl<'a, 'b> Slaps<'a, 'b> {
#[must_use]
pub fn with_name(name: &str) -> Self {
Self::with_name_and_config(name, Config::default())
}
#[must_use]
pub fn with_name_and_config(name: &str, config: Config<'b>) -> Slaps<'a, 'b> {
Slaps {
config,
matcher: clap::Matcher::with_name_and_config(name, config),
}
}
#[must_use]
pub fn subcommand<T: 'b>(mut self, command: clap::App<'a, 'b>, handler: T) -> Self
where
T: Fn(&clap::ArgMatches) -> Result<(), ExecutionError> + Send + Sync,
{
self.matcher
.register_command_with_handler(command, Box::new(handler));
self
}
pub fn prompt_line(&self, prompt: Option<&str>) -> Result<String, ReadlineError> {
readline::Readline::new(self.config).prompt_line(prompt.unwrap_or(self.config.prompt))
}
pub fn prompt_password(&self, prompt: Option<&str>) -> Result<String, ReadlineError> {
readline::Readline::prompt_password(prompt.unwrap_or(self.config.password_prompt))
}
pub fn get_matches<T: AsRef<str>>(&mut self, input: T) -> Result<clap::ArgMatches, Error> {
let words = shlex::split(input.as_ref())?;
Ok(self.matcher.get_matches(&words)?)
}
pub fn run(mut self) -> Result<(), Error> {
loop {
let input = match Readline::with_helper(
self.config,
&self.matcher,
&self.matcher,
&self.matcher,
)
.prompt_line(self.config.prompt)
{
Ok(i) => i,
Err(e) => return Err(Error::ReadlineError(e)),
};
let fields = match shlex::split(&input) {
Ok(f) => f,
Err(e) => {
eprintln!("{}", e);
continue;
}
};
match self.matcher.handle_matches(&fields) {
Err(Error::ClapError(e)) => match e.kind {
clap::ErrorKind::Io | ErrorKind::Format => return Err(Error::ClapError(e)),
_ => eprintln!("{}", e),
},
Ok(r) => eprintln!("t {:?}", r),
Err(e) => eprintln!("{}", e),
};
}
}
}