From 3ea5d62e980975d0a888293d875f26fc267cd368 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 7 Jun 2026 03:13:34 +0800 Subject: Refactor CLI entry points and move main logic to library --- mling/src/bin/cargo-mling.rs | 3 ++ mling/src/bin/mling.rs | 84 +-------------------------------------- mling/src/cargo_style.rs | 8 ++-- mling/src/cli.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++ mling/src/display.rs | 2 +- mling/src/lib.rs | 4 +- mling/src/proj_mgr/mod.rs | 4 +- 7 files changed, 109 insertions(+), 91 deletions(-) create mode 100644 mling/src/bin/cargo-mling.rs create mode 100644 mling/src/cli.rs (limited to 'mling/src') diff --git a/mling/src/bin/cargo-mling.rs b/mling/src/bin/cargo-mling.rs new file mode 100644 index 0000000..4c285c6 --- /dev/null +++ b/mling/src/bin/cargo-mling.rs @@ -0,0 +1,3 @@ +fn main() { + mingling_cli::cli::run(); +} diff --git a/mling/src/bin/mling.rs b/mling/src/bin/mling.rs index d48f439..4c285c6 100644 --- a/mling/src/bin/mling.rs +++ b/mling/src/bin/mling.rs @@ -1,85 +1,3 @@ -use std::{env::current_dir, process::exit}; - -use mingling::{ - Program, - hook::ProgramHook, - macros::program_setup, - setup::{ExitCodeSetup, GeneralRendererSetup, HelpFlagSetup, QuietFlagSetup}, -}; -use mingling_cli::{ThisProgram, display::markdown, res::ResCurrentDir}; - fn main() { - #[cfg(windows)] - colored::control::set_virtual_terminal(true).unwrap(); - - // Preprocess args to handle cargo-mling invocations - let mut args: Vec = std::env::args().collect(); - if args.first().map_or(false, |a| a.contains("cargo-mling")) { - args[0] = "cargo-mling".to_string(); - } - if args.get(1).map_or(false, |a| a == "mling") { - args.remove(1); - } - - // Build program with preprocessed args - let mut program = Program::::new_with_args(args); - - // Intercept Version - program.global_flag(["-V", "--version"], |_| { - eprintln!(include_str!("../helps/version.txt")); - exit(0) - }); - - // Intercept Help - program.with_hook(ProgramHook::empty().on_post_dispatch(|c| match c { - // When dispatcher is not found - ThisProgram::ErrorDispatcherNotFound => { - // And user requests Help - if ThisProgram::this().user_context.help { - // Print help - eprintln!("{}", markdown(include_str!("../helps/mling_help.txt"))); - exit(0) - } - } - _ => {} - })); - - // Setups - program.with_setup(HelpFlagSetup::new(["-h", "--help"])); - program.with_setup(GeneralRendererSetup); - program.with_setup(StandardOutputSetup); - program.with_setup(ExitCodeSetup::default()); - - // Resources - program.with_resource(ResCurrentDir { - path: current_dir().unwrap(), - }); - - // Execute - let quiet = program.stdout_setting.quiet; - let error_output = program.stdout_setting.error_output && !quiet; - let render_output = program.stdout_setting.render_output && !quiet; - let result = program.exec_without_render().unwrap(); - if !result.is_empty() { - if result.exit_code == 0 && render_output { - println!("{}", result.trim()); - } else if error_output { - eprintln!("{}", result.trim()); - } - } - exit(result.exit_code); -} - -#[program_setup] -fn standard_output_setup(program: &mut Program) { - program.with_setup(QuietFlagSetup::new("--silence")); - program.global_flag(["--no-error"], |program| { - program.stdout_setting.error_output = false; - }); - program.global_flag(["--no-result"], |program| { - program.stdout_setting.render_output = false; - }); - program.global_flag(["--silence", "--quiet"], |program| { - program.stdout_setting.quiet = true; - }); + mingling_cli::cli::run(); } diff --git a/mling/src/cargo_style.rs b/mling/src/cargo_style.rs index 048e75f..f127cc9 100644 --- a/mling/src/cargo_style.rs +++ b/mling/src/cargo_style.rs @@ -17,7 +17,7 @@ use colored::Colorize; /// /// # Examples /// -/// ``` +/// ```ignore /// format_cargo!("Compiling: hello.rs"); /// // Output: " Compiling hello.rs" (green bold "Compiling" padded to 12) /// ``` @@ -40,7 +40,7 @@ macro_rules! format_cargo { /// /// # Examples /// -/// ``` +/// ```ignore /// eformat_cargo!("failed to parse input"); /// // Output: "error: failed to parse input" (red bold "error") /// ``` @@ -63,7 +63,7 @@ macro_rules! eformat_cargo { /// /// # Examples /// -/// ``` +/// ```ignore /// println_cargo!("Compiling: hello.rs"); /// ``` #[macro_export] @@ -85,7 +85,7 @@ macro_rules! println_cargo { /// /// # Examples /// -/// ``` +/// ```ignore /// eprintln_cargo!("failed to parse input"); /// ``` #[macro_export] diff --git a/mling/src/cli.rs b/mling/src/cli.rs new file mode 100644 index 0000000..57f562f --- /dev/null +++ b/mling/src/cli.rs @@ -0,0 +1,95 @@ +use std::{env::current_dir, process::exit}; + +use crate::{ + CMDCompletion, ThisProgram, + display::markdown, + proj_mgr::{CMDInstall, CMDListNamespace, CMDRemoveNamespace}, + res::ResCurrentDir, +}; +use mingling::{ + Program, + hook::ProgramHook, + macros::program_setup, + setup::{ExitCodeSetup, GeneralRendererSetup, HelpFlagSetup, QuietFlagSetup}, +}; + +pub fn run() { + #[cfg(windows)] + colored::control::set_virtual_terminal(true).unwrap(); + + // Preprocess args to handle cargo-mling invocations + let mut args: Vec = std::env::args().collect(); + if args.first().is_some_and(|a| a.contains("cargo-mling")) { + args[0] = "cargo-mling".to_string(); + } + if args.get(1).is_some_and(|a| a == "mling") { + args.remove(1); + } + + // Build program with preprocessed args + let mut program = Program::::new_with_args(args); + + // Intercept Version + program.global_flag(["-V", "--version"], |_| { + eprintln!(include_str!("helps/version.txt")); + exit(0) + }); + + // Intercept Help + program.with_hook(ProgramHook::empty().on_post_dispatch(|c| match c { + // When dispatcher is not found + ThisProgram::ErrorDispatcherNotFound + // And user requests Help + if ThisProgram::this().user_context.help => { + // Print help + eprintln!("{}", markdown(include_str!("helps/mling_help.txt"))); + exit(0) + } + _ => {} + })); + + // Setups + program.with_setup(HelpFlagSetup::new(["-h", "--help"])); + program.with_setup(GeneralRendererSetup); + program.with_setup(StandardOutputSetup); + program.with_setup(ExitCodeSetup::default()); + + // Resources + program.with_resource(ResCurrentDir { + path: current_dir().unwrap(), + }); + + // Commands + program.with_dispatcher(CMDCompletion); + program.with_dispatcher(CMDInstall); + program.with_dispatcher(CMDListNamespace); + program.with_dispatcher(CMDRemoveNamespace); + + // Execute + let quiet = program.stdout_setting.quiet; + let error_output = program.stdout_setting.error_output && !quiet; + let render_output = program.stdout_setting.render_output && !quiet; + let result = program.exec_without_render().unwrap(); + if !result.is_empty() { + if result.exit_code == 0 && render_output { + println!("{}", result.trim()); + } else if error_output { + eprintln!("{}", result.trim()); + } + } + exit(result.exit_code); +} + +#[program_setup] +fn standard_output_setup(program: &mut Program) { + program.with_setup(QuietFlagSetup::new("--silence")); + program.global_flag(["--no-error"], |program| { + program.stdout_setting.error_output = false; + }); + program.global_flag(["--no-result"], |program| { + program.stdout_setting.render_output = false; + }); + program.global_flag(["--silence", "--quiet"], |program| { + program.stdout_setting.quiet = true; + }); +} diff --git a/mling/src/display.rs b/mling/src/display.rs index 5635cae..f664203 100644 --- a/mling/src/display.rs +++ b/mling/src/display.rs @@ -67,7 +67,7 @@ impl Markdown for String { /// /// # Examples /// ``` -/// # use cli_utils::display::markdown::markdown; +/// # use mingling_cli::display::markdown; /// let formatted = markdown("Hello **world**!"); /// println!("{}", formatted); /// diff --git a/mling/src/lib.rs b/mling/src/lib.rs index 0f58ccc..d4ce697 100644 --- a/mling/src/lib.rs +++ b/mling/src/lib.rs @@ -5,6 +5,8 @@ use mingling::{ res::ResExitCode, }; +pub mod cli; + mod cargo_style; pub use cargo_style::*; pub mod display; @@ -17,7 +19,7 @@ use crate::display::markdown; #[renderer] pub fn render_error_dispatch_not_found(err: ErrorDispatcherNotFound, ec: &mut ResExitCode) { - if err.len() < 1 { + if err.is_empty() { r_println!("{}", markdown(include_str!("helps/mling_help.txt"))); ec.exit_code = 0; } else { diff --git a/mling/src/proj_mgr/mod.rs b/mling/src/proj_mgr/mod.rs index ebc47a6..381784c 100644 --- a/mling/src/proj_mgr/mod.rs +++ b/mling/src/proj_mgr/mod.rs @@ -2,5 +2,5 @@ use mingling::macros::dispatcher; dispatcher!("install"); -dispatcher!("ls.namespace"); -dispatcher!("rm.namespace"); +dispatcher!("ls.namespace", CMDListNamespace => EntryListNamespace); +dispatcher!("rm.namespace", CMDRemoveNamespace => EntryRemoveNamespace); -- cgit