From 4567565168cae1946b8a9397d31e525802793763 Mon Sep 17 00:00:00 2001 From: kamack38 <64226248+kamack38@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:51:11 +0100 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=F0=9F=8E=B8=20Add=20command=20com?= =?UTF-8?q?pletion=20via=20clap=5Fcompletion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add init command --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/cli.rs | 22 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17b7cac..7463e92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,6 +72,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "299353be8209bd133b049bf1c63582d184a8b39fd9c04f15fe65f50f88bdfe6c" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -125,6 +134,7 @@ name = "ram-machine" version = "1.1.0" dependencies = [ "clap", + "clap_complete", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 5549ddd..c76aa2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ path = "src/main.rs" [dependencies] clap = { version = "4.4.18", features = ["derive"] } +clap_complete = "4.5.0" thiserror = "1.0.52" diff --git a/src/cli.rs b/src/cli.rs index 1bf4049..f10d309 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,17 +11,18 @@ use ram_machine::{ parser::ParserError, }; -use clap::{Parser as ClapParser, Subcommand}; +use clap::{Command, CommandFactory, Parser as ClapParser, Subcommand, ValueHint}; +use clap_complete::{generate, Generator, Shell}; #[derive(ClapParser, Debug)] -#[command(author, version, about, long_about = None)] +#[command(name = "ram", author, version, about, long_about = None)] struct Cli { /// Specifies the path to the input file from which data will be read (input passed from the command line takes precedence) - #[arg(short, long, value_name = "FILE")] + #[arg(short, long, value_name = "FILE", value_hint = ValueHint::FilePath)] input_file: Option, /// Specifies the path to the output file where the results will be written - #[arg(short, long, value_name = "FILE")] + #[arg(short, long, value_name = "FILE", value_hint = ValueHint::FilePath)] output_file: Option, /// Don't pass code output to STDOUT @@ -39,6 +40,9 @@ pub enum Commands { /// Validates ram code syntax of a given file Check { file: PathBuf }, + + /// Generate a shell completion file + Init { shell: Shell }, // Repl, // Debug, } @@ -67,8 +71,13 @@ pub enum RuntimeError { CheckFileError(ParserErrorChain), } +fn print_completions(gen: G, cmd: &mut Command) { + generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout()); +} + pub fn app() -> Result<(), RuntimeError> { let cli = Cli::parse(); + match cli.command { Commands::Run { file, input } => { let mut input = input; @@ -132,6 +141,11 @@ pub fn app() -> Result<(), RuntimeError> { return Err(RuntimeError::CheckFileError(errors)); } } + Commands::Init { shell } => { + let mut cmd = Cli::command(); + eprintln!("Generating completion file for {shell:?}..."); + print_completions(shell, &mut cmd) + } }; Ok(()) }