From c39ba62a0dbe4c1ba55b6279b1d7d9316c244eae Mon Sep 17 00:00:00 2001 From: Qihang Cai Date: Wed, 26 Jun 2024 00:26:30 +0800 Subject: [PATCH 1/2] add `lib.rs` & `args` in `cli::parse` for external calls Signed-off-by: Qihang Cai --- mega/src/cli.rs | 7 +++++-- mega/src/lib.rs | 13 +++++++++++++ mega/src/main.rs | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 mega/src/lib.rs diff --git a/mega/src/cli.rs b/mega/src/cli.rs index 73e4d335e..3eee38f15 100644 --- a/mega/src/cli.rs +++ b/mega/src/cli.rs @@ -20,8 +20,11 @@ use crate::commands::{builtin, builtin_exec}; /// /// This function returns a `MegaResult`. If the parsing is successful, it will return the result. /// If there is an error during the parsing, it will return an error. -pub fn parse() -> MegaResult { - let matches = cli().try_get_matches().unwrap_or_else(|e| e.exit()); +pub fn parse(args: Option>) -> MegaResult { + let matches = match args { + Some(args) => cli().try_get_matches_from(args).unwrap_or_else(|e| e.exit()), + None => cli().try_get_matches().unwrap_or_else(|e| e.exit()) + }; // Get the current directory let current_dir = env::current_dir()?; diff --git a/mega/src/lib.rs b/mega/src/lib.rs new file mode 100644 index 000000000..24f6abe28 --- /dev/null +++ b/mega/src/lib.rs @@ -0,0 +1,13 @@ +pub mod cli; +mod commands; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cli() { + let args = vec!["mega", "service", "http"]; + cli::parse(Some(args)).expect("Failed to start http service"); + } +} \ No newline at end of file diff --git a/mega/src/main.rs b/mega/src/main.rs index dd9029ffe..da8da7a56 100644 --- a/mega/src/main.rs +++ b/mega/src/main.rs @@ -9,7 +9,7 @@ mod commands; fn main() { // Parse the command line arguments - let result = cli::parse(); + let result = cli::parse(None); // If there was an error, print it if let Err(e) = result { From 0962a01e66a916e1c90947b34d06b390bb276176 Mon Sep 17 00:00:00 2001 From: Qihang Cai Date: Sat, 29 Jun 2024 18:54:53 +0800 Subject: [PATCH 2/2] ignore binary name (1st arg) in `args` Signed-off-by: Qihang Cai --- mega/src/cli.rs | 2 +- mega/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mega/src/cli.rs b/mega/src/cli.rs index 3eee38f15..fd496a4fe 100644 --- a/mega/src/cli.rs +++ b/mega/src/cli.rs @@ -22,7 +22,7 @@ use crate::commands::{builtin, builtin_exec}; /// If there is an error during the parsing, it will return an error. pub fn parse(args: Option>) -> MegaResult { let matches = match args { - Some(args) => cli().try_get_matches_from(args).unwrap_or_else(|e| e.exit()), + Some(args) => cli().no_binary_name(true).try_get_matches_from(args).unwrap_or_else(|e| e.exit()), None => cli().try_get_matches().unwrap_or_else(|e| e.exit()) }; diff --git a/mega/src/lib.rs b/mega/src/lib.rs index 24f6abe28..3ae7f75a0 100644 --- a/mega/src/lib.rs +++ b/mega/src/lib.rs @@ -7,7 +7,7 @@ mod tests { #[test] fn test_cli() { - let args = vec!["mega", "service", "http"]; + let args = "service http".split(' ').collect(); cli::parse(Some(args)).expect("Failed to start http service"); } } \ No newline at end of file