Skip to content

Commit

Permalink
Add a subcommand to generate shell completions (benfred#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored and manuels committed Aug 16, 2021
1 parent db583a9 commit c8c7cca
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{App, Arg, crate_description, crate_name, crate_version, arg_enum, value_t};
use clap::{App, AppSettings, Arg, crate_description, crate_name, crate_version, arg_enum, value_t};
use remoteprocess::Pid;

/// Options on how to collect samples from a python process
Expand Down Expand Up @@ -229,6 +229,13 @@ impl Config {
.long("json")
.help("Format output as JSON"));

let completions = clap::SubCommand::with_name("completions")
.about("Generate shell completions")
.setting(AppSettings::Hidden)
.arg(Arg::with_name("shell")
.possible_values(&clap::Shell::variants())
.help("Shell type"));

// add native unwinding if appropiate
#[cfg(unwind)]
let record = record.arg(native.clone());
Expand All @@ -253,7 +260,7 @@ impl Config {
#[cfg(not(target_os="freebsd"))]
let dump = dump.arg(nonblocking.clone());

let matches = App::new(crate_name!())
let mut app = App::new(crate_name!())
.version(crate_version!())
.about(crate_description!())
.setting(clap::AppSettings::InferSubcommands)
Expand All @@ -263,7 +270,8 @@ impl Config {
.subcommand(record)
.subcommand(top)
.subcommand(dump)
.get_matches_from_safe(args)?;
.subcommand(completions);
let matches = app.clone().get_matches_from_safe(args)?;
info!("Command line args: {:?}", matches);

let mut config = Config::default();
Expand All @@ -284,6 +292,11 @@ impl Config {
"top" => {
config.sampling_rate = value_t!(matches, "rate", u64)?;
}
"completions" => {
let shell = value_t!(matches.value_of("shell"), clap::Shell).unwrap_or_else(|e| e.exit());
app.gen_completions_to(crate_name!(), shell, &mut std::io::stdout());
std::process::exit(0);
}
_ => {}
}
config.command = subcommand.to_owned();
Expand Down

0 comments on commit c8c7cca

Please sign in to comment.