Skip to content

Commit

Permalink
Add completion command (#33)
Browse files Browse the repository at this point in the history
This PR adds `completion` command to generate shell completion for Bash, zsh, and other shell types.
  • Loading branch information
smbl64 authored Dec 25, 2023
1 parent 1c33b45 commit c13c557
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "humble-cli"
authors = ["Mohammad Banisaeid <[email protected]>"]
version = "0.13.1"
version = "0.14.0"
license = "MIT"
description = "The missing CLI for downloading your Humble Bundle purchases"
documentation = "https://github.com/smbl64/humble-cli"
Expand All @@ -26,6 +26,7 @@ anyhow = "1.0"
byte-unit = { version = "4.0", default-features = false }
chrono = { version = "0.4", features = ["serde"] }
clap = { version = "3.1", features = ["cargo", "derive"] }
clap_complete = "3.2"
dirs = "4.0.0"
futures-util = "0.3"
indicatif = "0.16"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Use `humble-cli auth "<YOUR SESSION KEY>"` to store the authentication key local
After that you will have access to the following sub-commands:

```
$ humble-cli 0.13.1
$ humble-cli --help
humble-cli 0.14.0
The missing Humble Bundle CLI
USAGE:
Expand All @@ -48,6 +49,7 @@ OPTIONS:
SUBCOMMANDS:
auth Set the authentication session key
completion Generate shell completions
details Print details of a certain bundle [aliases: info]
download Selectively download items from a bundle [aliases: d]
help Print this message or the help of the given subcommand(s)
Expand Down
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::io;

use anyhow::Context;
use clap::{builder::ValueParser, value_parser, Arg, Command};
use clap_complete::Shell;
use humble_cli::prelude::*;

fn main() {
Expand Down Expand Up @@ -44,6 +47,17 @@ fn run() -> Result<(), anyhow::Error> {
)
);

let completion_subcommand = Command::new("completion")
.about("Generate shell completions")
.arg(
Arg::new("SHELL")
.help("Shell type to generate completions for")
.possible_values(&["bash", "elvish", "fish", "powershell", "zsh"])
.takes_value(true)
.required(true)
.value_parser(value_parser!(Shell)),
);

let list_choices_subcommand = Command::new("list-choices")
.about("List your current Humble Choices")
.arg(
Expand Down Expand Up @@ -164,20 +178,28 @@ fn run() -> Result<(), anyhow::Error> {
details_subcommand,
download_subcommand,
search_subcommand,
completion_subcommand,
];

let crate_name = clap::crate_name!();

let matches = clap::Command::new(crate_name)
let mut root = clap::Command::new(crate_name)
.about("The missing Humble Bundle CLI")
.version(clap::crate_version!())
.after_help("Note: `humble-cli -h` prints a short and concise overview while `humble-cli --help` gives all details.")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommands(sub_commands)
.get_matches();
.subcommands(sub_commands);

let matches = root.clone().get_matches();
return match matches.subcommand() {
Some(("completion", sub_matches)) => {
if let Some(g) = sub_matches.get_one::<Shell>("SHELL").copied() {
let crate_name = clap::crate_name!();
clap_complete::generate(g, &mut root, crate_name, &mut io::stdout());
}
Ok(())
}
Some(("auth", sub_matches)) => {
let session_key = sub_matches.value_of("SESSION-KEY").unwrap();
auth(session_key)
Expand Down

0 comments on commit c13c557

Please sign in to comment.