feat: add a generator for shell completions

This commit is contained in:
Cainã Costa 2023-07-25 12:00:35 -03:00 committed by Lucas
parent eac27eff41
commit 2cecb099d7
6 changed files with 95 additions and 47 deletions

10
Cargo.lock generated vendored
View File

@ -59,6 +59,7 @@ dependencies = [
"anyhow", "anyhow",
"built", "built",
"clap", "clap",
"clap_complete",
"hex", "hex",
"ignore", "ignore",
"indoc", "indoc",
@ -493,6 +494,15 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "clap_complete"
version = "4.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fc443334c81a804575546c5a8a79b4913b50e28d69232903604cada1de817ce"
dependencies = [
"clap",
]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.2.0" version = "4.2.0"

View File

@ -31,6 +31,7 @@ aiken-lang = { path = "../aiken-lang", version = "1.0.13-alpha" }
aiken-lsp = { path = "../aiken-lsp", version = "1.0.13-alpha" } aiken-lsp = { path = "../aiken-lsp", version = "1.0.13-alpha" }
aiken-project = { path = '../aiken-project', version = "1.0.13-alpha" } aiken-project = { path = '../aiken-project', version = "1.0.13-alpha" }
uplc = { path = '../uplc', version = "1.0.13-alpha" } uplc = { path = '../uplc', version = "1.0.13-alpha" }
clap_complete = "4.3.2"
[build-dependencies] [build-dependencies]
built = { version = "0.6.0", features = ["git2"] } built = { version = "0.6.0", features = ["git2"] }

View File

@ -0,0 +1,28 @@
use clap::{Subcommand, Command};
use clap_complete::{Shell, generate};
use crate::cmd::Cmd as MainCmd;
/// Generates shell completion scripts
#[derive(Subcommand)]
pub enum Cmd {
Bash,
Zsh,
Fish
}
pub fn exec(sub_cmd: Cmd) -> miette::Result<()> {
let shell = match sub_cmd {
Cmd::Bash => Shell::Bash,
Cmd::Zsh => Shell::Zsh,
Cmd::Fish => Shell::Fish
};
let cli = Command::new("aiken").disable_version_flag(true);
let mut main = MainCmd::augment_subcommands(cli);
generate(shell, &mut main, "aiken".to_string(), &mut std::io::stdout());
Ok(())
}

View File

@ -1,6 +1,9 @@
use clap::Parser;
pub mod blueprint; pub mod blueprint;
pub mod build; pub mod build;
pub mod check; pub mod check;
pub mod completion;
pub mod docs; pub mod docs;
pub mod fmt; pub mod fmt;
pub mod lsp; pub mod lsp;
@ -8,3 +11,51 @@ pub mod new;
pub mod packages; pub mod packages;
pub mod tx; pub mod tx;
pub mod uplc; pub mod uplc;
use crate::built_info;
/// Aiken: a smart-contract language and toolchain for Cardano
#[derive(Parser)]
#[clap(version = version(), about, long_about = None)]
#[clap(propagate_version = true)]
pub enum Cmd {
New(new::Args),
Fmt(fmt::Args),
Build(build::Args),
Address(blueprint::address::Args),
Check(check::Args),
Docs(docs::Args),
Add(packages::add::Args),
#[clap(subcommand)]
Blueprint(blueprint::Cmd),
#[clap(subcommand)]
Packages(packages::Cmd),
#[clap(subcommand)]
Tx(tx::Cmd),
#[clap(subcommand)]
Uplc(uplc::Cmd),
#[clap(subcommand)]
Completion(completion::Cmd),
#[clap(hide = true)]
Lsp(lsp::Args),
}
impl Default for Cmd {
fn default() -> Self {
Self::parse()
}
}
fn version() -> String {
format!(
"v{} {}",
built_info::PKG_VERSION,
built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown")
)
}

View File

@ -22,7 +22,7 @@ pub struct Args {
#[clap(long)] #[clap(long)]
pub version: String, pub version: String,
#[clap(hide = true)] #[clap(hide = true, long)]
pub overwrite: bool, pub overwrite: bool,
} }

View File

@ -1,50 +1,15 @@
use aiken::{ use aiken::{
built_info,
cmd::{ cmd::{
Cmd,
blueprint::{self, address}, blueprint::{self, address},
build, check, docs, fmt, lsp, new, build, check, docs, fmt, lsp, new,
packages::{self, add}, packages::{self, add},
tx, uplc, tx, uplc, completion,
}, },
}; };
use clap::Parser;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
/// Aiken: a smart-contract language and toolchain for Cardano
#[derive(Parser)]
#[clap(version = version(), about, long_about = None)]
#[clap(propagate_version = true)]
pub enum Cmd {
New(new::Args),
Fmt(fmt::Args),
Build(build::Args),
Address(address::Args),
Check(check::Args),
Docs(docs::Args),
Add(add::Args),
#[clap(subcommand)]
Blueprint(blueprint::Cmd),
#[clap(subcommand)]
Packages(packages::Cmd),
#[clap(subcommand)]
Tx(tx::Cmd),
#[clap(subcommand)]
Uplc(uplc::Cmd),
#[clap(hide = true)]
Lsp(lsp::Args),
}
impl Default for Cmd {
fn default() -> Self {
Self::parse()
}
}
fn main() -> miette::Result<()> { fn main() -> miette::Result<()> {
panic_handler(); panic_handler();
@ -61,17 +26,10 @@ fn main() -> miette::Result<()> {
Cmd::Lsp(args) => lsp::exec(args), Cmd::Lsp(args) => lsp::exec(args),
Cmd::Tx(sub_cmd) => tx::exec(sub_cmd), Cmd::Tx(sub_cmd) => tx::exec(sub_cmd),
Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd), Cmd::Uplc(sub_cmd) => uplc::exec(sub_cmd),
Cmd::Completion(sub_cmd) => completion::exec(sub_cmd)
} }
} }
fn version() -> String {
format!(
"v{} {}",
built_info::PKG_VERSION,
built_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown")
)
}
fn panic_handler() { fn panic_handler() {
std::panic::set_hook(Box::new(move |info| { std::panic::set_hook(Box::new(move |info| {
let message = info let message = info