Skip to content

feat(cli): generate autocomplete shell stuff via -c | --completions argument (#37) #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion curlz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include = ["src/**/*", "LICENSE", "*.md"]
env_logger = "0.10"
log = "0.4"
clap = { version = "4.1", features = ["derive", "std", "cargo", "usage", "help"] }
#clap_complete = "4.1"
clap_complete = "4.1"
clap-verbosity-flag = "2.0"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
Expand Down
39 changes: 30 additions & 9 deletions curlz/src/curlz/cli/execute.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use super::sub_commands::*;

use clap::Parser;
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, generator::Generator, Shell};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use env_logger::Target;

#[derive(Clone, Debug, Parser)]
#[command(author, version, about, long_about = None )]
#[clap(subcommand_required = true, arg_required_else_help = true)]
#[command(arg_required_else_help = true)]
#[command(propagate_version = true)]
#[command(name = "curlz")]
pub struct Cli {
/// increased verbosity will show more debug output of curlz itself
#[command(flatten)]
verbose: Verbosity<InfoLevel>,

/// Generate a SHELL completion script and print to stdout
#[clap(long, short, value_name = "SHELL")]
pub completions: Option<Shell>,

#[command(subcommand)]
pub command: SubCommands,
pub cmd: Option<SubCommands>,
}

pub fn execute() -> crate::Result<()> {
Expand All @@ -23,12 +30,26 @@ pub fn execute() -> crate::Result<()> {
.target(Target::Stderr)
.init();

match args.command {
SubCommands::Request(ref r) => r.execute(),
SubCommands::Bookmark(_b) => {
todo!()
if let Some(shell) = args.completions {
let mut cmd = Cli::command();
print_completions(shell, &mut cmd);
std::process::exit(0);
}

if let Some(cmd) = &args.cmd {
match cmd {
SubCommands::Request(ref r) => r.execute(),
SubCommands::Bookmark(_b) => {
todo!()
}
#[cfg(feature = "x-http-lang")]
SubCommands::HttpFile(ref hf) => hf.execute(),
}
#[cfg(feature = "x-http-lang")]
SubCommands::HttpFile(ref hf) => hf.execute(),
} else {
Ok(())
}
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}
18 changes: 16 additions & 2 deletions curlz/tests/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ mod testlib;
#[test]
fn should_show_usage_when_no_args_passed() {
#[cfg(windows)]
let pattern = predicate::str::contains("Usage: curlz.exe [OPTIONS] <COMMAND>");
let pattern = predicate::str::contains("Usage: curlz.exe [OPTIONS] [COMMAND]");
#[cfg(not(windows))]
let pattern = predicate::str::contains("Usage: curlz [OPTIONS] <COMMAND>");
let pattern = predicate::str::contains("Usage: curlz [OPTIONS] [COMMAND]");

binary().assert().failure().stderr(pattern);
}

#[test]
fn should_show_completions() {
#[cfg(windows)]
let pattern = predicate::str::contains("#compdef curlz");
#[cfg(not(windows))]
let pattern = predicate::str::contains("#compdef curlz");

binary()
.args(["--completions", "zsh"])
.assert()
.success()
.stdout(pattern);
}

#[tokio::test]
async fn should_send_as_get() {
CurlzTestSuite::new()
Expand Down