Skip to content

Commit e50f1f4

Browse files
committed
Using clap to generate shell completions.
1 parent e358d1a commit e50f1f4

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/main.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ use humantime::{parse_duration, parse_rfc3339_weak};
1515
use isatty::{stdin_isatty};
1616
use regex::Regex;
1717
use subprocess::{Exec, ExitStatus, Redirection};
18-
use structopt::StructOpt;
18+
use structopt::{clap::{Shell, AppSettings}, StructOpt};
1919

2020
fn main() {
2121

22+
// Testing subcommands
23+
let matches = Opt::clap().get_matches();
24+
match matches.subcommand() {
25+
("completions", Some(sub_matches)) => {
26+
let shell = sub_matches.value_of("shell").unwrap();
27+
Opt::clap().gen_completions_to(
28+
"loop",
29+
shell.parse::<Shell>().unwrap(),
30+
&mut io::stdout()
31+
);
32+
std::process::exit(0);
33+
},
34+
_ => (),
35+
}
36+
2237
// Load the CLI arguments
2338
let opt = Opt::from_args();
2439

@@ -140,7 +155,9 @@ fn main() {
140155

141156
#[derive(StructOpt, Debug)]
142157
#[structopt(name = "loop", author = "Rich Jones <[email protected]>",
143-
about = "UNIX's missing `loop` command")]
158+
about = "UNIX's missing `loop` command",
159+
raw(global_settings = "&[AppSettings::SubcommandsNegateReqs]")
160+
)]
144161
struct Opt {
145162
/// The command to be looped
146163
#[structopt()]
@@ -194,6 +211,19 @@ struct Opt {
194211
/// Read from standard input
195212
#[structopt(short = "i", long = "stdin")]
196213
stdin: bool,
214+
215+
#[structopt(subcommand)]
216+
subcommands : Option<Subcommands>,
217+
}
218+
219+
#[derive(StructOpt, Debug)]
220+
enum Subcommands {
221+
#[structopt(name = "completions",
222+
about = "Generates completion scripts for your shell")]
223+
Completions {
224+
/// The shell to generate the script for
225+
shell : Shell,
226+
}
197227
}
198228

199229
#[derive(Debug)]

0 commit comments

Comments
 (0)