diff --git a/Cargo.lock b/Cargo.lock index f04f3b2d..c02ba631 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,7 @@ dependencies = [ "anyhow", "built", "clap", + "clap_complete", "hex", "ignore", "indoc", @@ -493,6 +494,15 @@ dependencies = [ "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]] name = "clap_derive" version = "4.2.0" diff --git a/crates/aiken/Cargo.toml b/crates/aiken/Cargo.toml index 66923241..6aec4645 100644 --- a/crates/aiken/Cargo.toml +++ b/crates/aiken/Cargo.toml @@ -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-project = { path = '../aiken-project', version = "1.0.13-alpha" } uplc = { path = '../uplc', version = "1.0.13-alpha" } +clap_complete = "4.3.2" [build-dependencies] built = { version = "0.6.0", features = ["git2"] } diff --git a/crates/aiken/src/cmd/completion.rs b/crates/aiken/src/cmd/completion.rs new file mode 100644 index 00000000..92018953 --- /dev/null +++ b/crates/aiken/src/cmd/completion.rs @@ -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(()) +} diff --git a/crates/aiken/src/cmd/mod.rs b/crates/aiken/src/cmd/mod.rs index bfef865e..5dae8375 100644 --- a/crates/aiken/src/cmd/mod.rs +++ b/crates/aiken/src/cmd/mod.rs @@ -1,6 +1,9 @@ +use clap::Parser; + pub mod blueprint; pub mod build; pub mod check; +pub mod completion; pub mod docs; pub mod fmt; pub mod lsp; @@ -8,3 +11,51 @@ pub mod new; pub mod packages; pub mod tx; 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") + ) +} diff --git a/crates/aiken/src/cmd/packages/add.rs b/crates/aiken/src/cmd/packages/add.rs index 3ab5aad6..c15e8f63 100644 --- a/crates/aiken/src/cmd/packages/add.rs +++ b/crates/aiken/src/cmd/packages/add.rs @@ -22,7 +22,7 @@ pub struct Args { #[clap(long)] pub version: String, - #[clap(hide = true)] + #[clap(hide = true, long)] pub overwrite: bool, } diff --git a/crates/aiken/src/main.rs b/crates/aiken/src/main.rs index e2a74c16..2abc9568 100644 --- a/crates/aiken/src/main.rs +++ b/crates/aiken/src/main.rs @@ -1,50 +1,15 @@ use aiken::{ - built_info, cmd::{ + Cmd, blueprint::{self, address}, build, check, docs, fmt, lsp, new, packages::{self, add}, - tx, uplc, + tx, uplc, completion, }, }; -use clap::Parser; + 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<()> { panic_handler(); @@ -61,17 +26,10 @@ fn main() -> miette::Result<()> { Cmd::Lsp(args) => lsp::exec(args), Cmd::Tx(sub_cmd) => tx::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() { std::panic::set_hook(Box::new(move |info| { let message = info