From ccc0580ab73949706a27ba790845ac4ae1b2f25d Mon Sep 17 00:00:00 2001 From: Marcin Puc Date: Sun, 21 Nov 2021 18:43:49 +0100 Subject: [PATCH] Add shell completion --- aocf_cli/README.md | 5 +++++ aocf_cli/src/bin/aocf.rs | 7 +++---- aocf_cli/src/cli.rs | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/aocf_cli/README.md b/aocf_cli/README.md index 961b3d9..3882483 100644 --- a/aocf_cli/README.md +++ b/aocf_cli/README.md @@ -68,6 +68,11 @@ Show a summary of all challenges which exist in the cache, and stars achieved, for a given challenge year. The year shown is either the currently checked out year, or the year provided by the `--year` command line option. +### `completion` + +Generate a shell completion script. +Supported shells are Bash, Zsh, Fish, Powershell, Elvish. + ### `help` Get help on command usage, for a given subcommand. diff --git a/aocf_cli/src/bin/aocf.rs b/aocf_cli/src/bin/aocf.rs index cd8b52a..f7b4151 100644 --- a/aocf_cli/src/bin/aocf.rs +++ b/aocf_cli/src/bin/aocf.rs @@ -5,7 +5,7 @@ use aocf::{ Level, }; use aocf_cli::{ - cli::{Aocf, AocfTimeDateOpts}, + cli::{Aocf, AocfTimeDateOpts, generate_completion}, conf::Conf, pretty::make_pretty, }; @@ -46,6 +46,7 @@ fn run(args: &Aocf) -> Result<(), Error> { Aocf::Init => return Ok(init()?), Aocf::SetCookie { token } => return Ok(set_cookie(&token)?), Aocf::GetCookie => return Ok(get_cookie()?), + Aocf::Completion { shell } => return Ok(generate_completion(*shell)), _ => (), }; @@ -123,9 +124,7 @@ fn run(args: &Aocf) -> Result<(), Error> { Aocf::Status { .. } => status(&aoc)?, Aocf::Summary { year } => summary(*year, conf.year)?, Aocf::Checkout ( args ) => checkout(&mut conf, conf_hash, &args)?, - Aocf::Init => (), - Aocf::SetCookie { .. } => (), - Aocf::GetCookie => (), + Aocf::Init | Aocf::SetCookie { .. } | Aocf::GetCookie | Aocf::Completion { .. } => (), }; // Update configuration if changed since start diff --git a/aocf_cli/src/cli.rs b/aocf_cli/src/cli.rs index 20df67a..c043234 100644 --- a/aocf_cli/src/cli.rs +++ b/aocf_cli/src/cli.rs @@ -1,8 +1,12 @@ -use structopt::StructOpt; +use structopt::{StructOpt, clap::Shell}; use chrono::{Utc, Datelike}; include!(concat!(env!("OUT_DIR"), "/version.rs")); +pub fn generate_completion(shell: Shell) { + Aocf::clap().gen_completions_to("aocf", shell, &mut std::io::stdout()); +} + #[derive(StructOpt, Debug)] #[structopt(about = "Advent of Code Swiss army knife", version = PKG_VERSION)] pub enum Aocf { @@ -85,12 +89,19 @@ pub enum Aocf { /// Get authentication token from firefox cookie store GetCookie, + + /// Generate shell completion script + Completion { + /// Shell type + #[structopt(possible_values = &["bash", "zsh", "fish", "powershell", "elvish"])] + shell: Shell, + } } #[derive(StructOpt, Debug)] pub struct AocfTimeDateOpts { /// Check out current day and year - #[structopt(short, long, conflicts_with_all = &["problem_day", "problem_year", "day", "year"])] + #[structopt(short, long, conflicts_with_all = &["problem-day", "problem-year", "day", "year"])] now: bool, /// Problem day