From 09c267139db983c5fceceac34a7d2559f5391179 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 12 Dec 2023 21:24:12 -0500 Subject: [PATCH] CLI help command --- src/cli/empty.rs | 0 src/cli/help.rs | 9 +++++++-- src/cli/mod.rs | 20 +++++++++++++------- src/cli/types.rs | 8 ++++++-- 4 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 src/cli/empty.rs diff --git a/src/cli/empty.rs b/src/cli/empty.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/help.rs b/src/cli/help.rs index f393cf4..24b0591 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -1,5 +1,10 @@ -use crate::cli::HELP_TEXT; +use crate::cli::get_help_text; +use colored::*; pub fn help_command(options: &Vec) { - println!("{}", HELP_TEXT); + println!("{}", get_help_text()); + + if options.len() > 0 { + println!("{}: {}", "warning".yellow(), "The help command doesn't take any options."); + } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 87f3355..4b6fa8c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,17 +1,20 @@ mod help; mod types; +mod empty; use types::{Command, CommandType}; use colored::*; -pub const HELP_TEXT: &str = r#" +pub fn get_help_text() -> String { + format!( + r#" Usage: `thp [command] [options]` Commands - c _file_ Compiles `file` in-place - f _file_ Formats `file` + c {0} Compiles {0} in-place + f {0} Formats {0} r Starts the REPL init Initializes a new project in the current directory @@ -24,9 +27,12 @@ Commands General options -h, --help Print command-specific usage -"#; +"#, + "_file_".green() + ) +} -fn get_copyright() -> String { +fn get_version() -> String { let crate_version = env!("CARGO_PKG_VERSION"); format!("The THP compiler, linter & formatter, v{}", crate_version) } @@ -35,8 +41,8 @@ pub fn run_cli() { let command = match parse_args() { Ok(c) => c, Err(reason) => { - println!("{}", HELP_TEXT); - println!("{}: {}", "error".red(), reason); + println!("{}", get_help_text()); + println!("{}: {}", "error".on_red(), reason); return; } }; diff --git a/src/cli/types.rs b/src/cli/types.rs index 2dacdde..fcc742b 100644 --- a/src/cli/types.rs +++ b/src/cli/types.rs @@ -19,13 +19,17 @@ pub enum CommandType { impl Command { pub fn run(&self) { - println!("Running command! {:?}", self); self.command.run(&self.options); } } impl CommandType { pub fn run(&self, options: &Vec) { - println!("Running command! {:?}", self) + match self { + CommandType::Help => super::help::help_command(options), + _ => { + println!("Not implemented yet! {:?} {:?}", self, options); + } + } } }