Skip to content

Commit 59eb86b

Browse files
committed
added default help and shell completions
- Instead of erroring if now command is provided, help is provided instead - added command to generate completions
1 parent 34efb72 commit 59eb86b

6 files changed

Lines changed: 50 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ categories = ["development-tools", "command-line-utilities"]
1515
[dependencies]
1616
anyhow = "1.0.89"
1717
clap = { version = "4.5.20", features = ["derive"] }
18+
clap_complete = "^4.5"
1819
copypasta = "0.8.2"
1920
dirs = "5.0.1"
2021
human-panic = "2.0.2"
@@ -25,7 +26,10 @@ ratatui = "0.28.1"
2526
serde = { version = "1.0.210", features = ["default", "derive"] }
2627
serde_json = "1.0.128"
2728
tui-input = "0.10.1"
28-
ureq = { version = "2.10.1", features = ["gzip", "native-tls"], default-features = false }
29+
ureq = { version = "2.10.1", features = [
30+
"gzip",
31+
"native-tls",
32+
], default-features = false }
2933
url = "2.5.2"
3034
yansi = "1.0.1"
3135

@@ -53,16 +57,16 @@ pkg-url = "{ repo }/releases/download/v{ version }/{ name }-macos-arm64"
5357

5458
[package.metadata.cross.target.x86_64-unknown-linux-gnu]
5559
pre-build = [
56-
"dpkg --add-architecture $CROSS_DEB_ARCH",
57-
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
60+
"dpkg --add-architecture $CROSS_DEB_ARCH",
61+
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
5862
]
5963
[package.metadata.cross.target.i686-unknown-linux-gnu]
6064
pre-build = [
61-
"dpkg --add-architecture $CROSS_DEB_ARCH",
62-
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
65+
"dpkg --add-architecture $CROSS_DEB_ARCH",
66+
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
6367
]
6468
[package.metadata.cross.target.aarch64-unknown-linux-gnu]
6569
pre-build = [
66-
"dpkg --add-architecture $CROSS_DEB_ARCH",
67-
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
70+
"dpkg --add-architecture $CROSS_DEB_ARCH",
71+
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
6872
]

src/cli.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::template::item::Template;
22
use crate::template::list::TemplateList;
33
use anyhow::{bail, Context, Result};
44
use clap::{Args, Parser, Subcommand};
5+
use clap_complete::Shell;
56
use once_cell::sync::Lazy;
67

78
const LONG_ABOUT: &str = r"
@@ -18,7 +19,7 @@ You can also browse the available templates at the GitHub &
1819
TopTal collections using the `search` command.";
1920

2021
#[derive(Parser, Debug)]
21-
#[command(author, version, about, long_about = LONG_ABOUT)]
22+
#[command(author, version, about, long_about = LONG_ABOUT, arg_required_else_help = true)]
2223
pub struct Cli {
2324
/// Refresh the cache (templates are cached for 1h)
2425
#[arg(short = 'r', long = "refresh", global = true)]
@@ -66,6 +67,11 @@ pub enum Commands {
6667
Create(CommandCreate),
6768
/// Choose templates interactively from the GitHub & TopTal collections
6869
Search,
70+
/// Generate completions to stdout
71+
Completions {
72+
/// Specify desired shell
73+
shell: Shell,
74+
},
6975
}
7076

7177
impl Cli {
@@ -76,6 +82,9 @@ impl Cli {
7682
Some(Commands::Search) => {
7783
bail!("Cannot provide template arguments to 'search' command")
7884
}
85+
Some(Commands::Completions { shell: _ }) => {
86+
bail!("Cannot provide template arguments to 'completions' command")
87+
}
7988
None => bail!("Cannot provide template arguments to an unknown command"),
8089
};
8190

src/commands/completions.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::io;
2+
3+
use anyhow::Result;
4+
use clap::CommandFactory;
5+
use clap_complete::{generate, Shell};
6+
7+
use crate::cli::Cli;
8+
9+
pub fn command(shell: &Shell) -> Result<()> {
10+
let cmd = &mut Cli::command();
11+
12+
generate(*shell, cmd, cmd.get_name().to_string(), &mut io::stdout());
13+
14+
Ok(())
15+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod completions;
12
pub mod create;
23
pub mod search;

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod tests;
77
mod util;
88

99
use crate::cli::{get_cli, Commands};
10+
use crate::commands::completions;
1011
use crate::commands::create;
1112
use crate::commands::search;
1213
use anyhow::{anyhow, Result};
@@ -20,6 +21,7 @@ fn main() -> Result<()> {
2021
let result = match &get_cli().command {
2122
Some(Commands::Create(cmd)) => create::command(cmd),
2223
Some(Commands::Search) => search::command(),
24+
Some(Commands::Completions { shell }) => completions::command(shell),
2325
None => Err(anyhow!("No command specified")),
2426
};
2527

0 commit comments

Comments
 (0)