Skip to content

Commit aa80795

Browse files
committed
Using clap to generate shell completions.
Added a new subcommand 'completions' that takes an argument (the shell to generate the completions for) and outputs the shell completion to the standard output. This is working the same way that rustup does.
1 parent 73be240 commit aa80795

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/cli.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
name: loop
1+
name: loop
22
version: "0.1.0"
33
author: Rich Jones <miserlou@gmail.com>
4-
about: UNIX's missing `loop` command
4+
about: UNIX's missing `loop` command
5+
settings:
6+
- SubcommandsNegateReqs
7+
58
args:
69
- INPUT:
710
help: The command to be looped
@@ -74,4 +77,13 @@ args:
7477
- stdin:
7578
short: i
7679
long: stdin
77-
help: Read from standard input
80+
help: Read from standard input
81+
82+
subcommands:
83+
- completions:
84+
about: Generates completion scripts for your shell
85+
args:
86+
- shell:
87+
required: true
88+
possible_values: [ bash, fish, zsh, powershell, elvish ]
89+
help: The shell to generate the script for

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::io::{self, BufRead};
1111
use std::process::Command;
1212
use std::time::{Instant, SystemTime};
1313

14-
use clap::App;
14+
use clap::{App, Shell};
1515
use humantime::{parse_duration, parse_rfc3339_weak};
1616
use isatty::{stdin_isatty};
1717
use regex::Regex;
@@ -22,6 +22,20 @@ fn main() {
2222
// Load the CLI
2323
let yaml = load_yaml!("cli.yml");
2424
let matches = App::from_yaml(yaml).get_matches();
25+
let mut cli = App::from_yaml(yaml);
26+
27+
match matches.subcommand() {
28+
("completions", Some(sub_matches)) => {
29+
let shell = sub_matches.value_of("shell").unwrap();
30+
cli.gen_completions_to(
31+
"loop",
32+
shell.parse::<Shell>().unwrap(),
33+
&mut io::stdout()
34+
);
35+
std::process::exit(0);
36+
},
37+
_ => (),
38+
}
2539

2640
// Main input command
2741
let input = matches.value_of("INPUT").unwrap();

0 commit comments

Comments
 (0)