-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcompletions.rs
More file actions
189 lines (159 loc) · 3.71 KB
/
completions.rs
File metadata and controls
189 lines (159 loc) · 3.71 KB
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
use crate::common::*;
const SHELL_FLAG: &str = "shell-flag";
const SHELL_POSITIONAL: &str = "<SHELL>";
const SHELL_HELP: &str = "Print completion script for `SHELL`.";
#[derive(StructOpt)]
#[structopt(
help_message(consts::HELP_MESSAGE),
version_message(consts::VERSION_MESSAGE),
about("Print shell completion scripts to standard output.")
)]
pub(crate) struct Completions {
#[structopt(
name = SHELL_FLAG,
long = "shell",
short = "s",
value_name = "SHELL",
possible_values = Shell::VARIANTS,
help = SHELL_HELP,
)]
shell_flag: Option<Shell>,
#[structopt(
name = SHELL_POSITIONAL,
value_name = "SHELL",
possible_values = Shell::VARIANTS,
required_unless = "dir",
required_unless = SHELL_FLAG,
conflicts_with = SHELL_FLAG,
help = SHELL_HELP,
)]
shell_positional: Option<Shell>,
#[structopt(
long = "dir",
short = "d",
value_name = "DIR",
empty_values = false,
parse(from_os_str),
help = "Write completion script to `DIR` with an appropriate filename. If `--shell` is not \
given, write all completion scripts."
)]
dir: Option<PathBuf>,
}
impl Completions {
pub(crate) fn run(self, env: &mut Env) -> Result<()> {
if self.shell_flag.is_some() || self.shell_positional.is_some() {
let shell = xor_args(
"shell_flag",
self.shell_flag.as_ref(),
"shell_positional",
self.shell_positional.as_ref(),
)?;
if let Some(dir) = self.dir {
Self::write(env, &dir, shell)?;
} else {
let script = shell.completion_script()?;
out!(env, "{}", script)?;
}
} else {
let dir = self
.dir
.ok_or_else(|| Error::internal("Expected `--dir` to be set"))?;
for shell in Shell::iter() {
Self::write(env, &dir, shell)?;
}
}
Ok(())
}
fn write(env: &mut Env, dir: &Path, shell: Shell) -> Result<()> {
let script = shell.completion_script()?;
let dst = dir.join(shell.completion_script_filename());
env.write(dst, script)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shell_required() {
let mut env = test_env! {
args: [
"completions",
],
tree: {},
};
assert_matches!(env.run(), Err(Error::Clap { .. }));
}
#[test]
fn output() {
let mut env = test_env! {
args: [
"completions",
"--shell",
"bash",
],
tree: {},
};
env.assert_ok();
assert!(env.out().starts_with("_imdl() {"));
}
#[test]
fn output_positional() {
let mut env = test_env! {
args: [
"completions",
"bash",
],
tree: {},
};
env.assert_ok();
assert!(env.out().starts_with("_imdl() {"));
}
#[test]
fn single_dir() {
let mut env = test_env! {
args: [
"completions",
"--shell",
"bash",
"--dir",
".",
],
tree: {},
};
env.assert_ok();
let script = env.read_to_string("imdl.bash");
assert!(script.starts_with("_imdl() {"));
}
#[test]
fn single_positional() {
let mut env = test_env! {
args: [
"completions",
"bash",
"--dir",
".",
],
tree: {},
};
env.assert_ok();
let script = env.read_to_string("imdl.bash");
assert!(script.starts_with("_imdl() {"));
}
#[test]
fn all_dir() {
let mut env = test_env! {
args: [
"completions",
"--dir",
".",
],
tree: {},
};
env.assert_ok();
let script = env.read_to_string("imdl.bash");
assert!(script.starts_with("_imdl() {"));
let script = env.read_to_string("_imdl.ps1");
assert!(script.starts_with("using namespace"));
}
}