From be41329608a62684fd49a1dfc24c753a232bcbe3 Mon Sep 17 00:00:00 2001 From: Sliman4 <99507892+Sliman4@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:18:35 +0300 Subject: [PATCH] fix!: Proxy `try_parse_from` to Clap's `try_parse_from` as is, instead of naive parsing of `&str` (#21) `try_parse_from` doesn't have special treatment for quotes (`command "some thing"` is split into 3 arguments). --- .../src/derives/interactive_clap/mod.rs | 16 ++++++++++++---- ..._derive__tests__test_simple_struct__flag.snap | 14 ++++++++++---- ...tests__test_simple_struct__simple_struct.snap | 14 ++++++++++---- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/interactive-clap-derive/src/derives/interactive_clap/mod.rs b/interactive-clap-derive/src/derives/interactive_clap/mod.rs index 271bc67..8124376 100644 --- a/interactive-clap-derive/src/derives/interactive_clap/mod.rs +++ b/interactive-clap-derive/src/derives/interactive_clap/mod.rs @@ -199,8 +199,12 @@ pub fn impl_interactive_clap(ast: &syn::DeriveInput) -> TokenStream { <#cli_name as clap::Parser>::parse() } - pub fn try_parse_from(s: &str) -> Result<#cli_name, clap::Error> { - <#cli_name as clap::Parser>::try_parse_from(s.split(" ")) + pub fn try_parse_from(itr: I) -> Result<#cli_name, clap::Error> + where + I: ::std::iter::IntoIterator, + T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone, + { + <#cli_name as clap::Parser>::try_parse_from(itr) } } @@ -337,8 +341,12 @@ pub fn impl_interactive_clap(ast: &syn::DeriveInput) -> TokenStream { <#cli_name as clap::Parser>::parse() } - pub fn try_parse_from(s: &str) -> Result<#cli_name, clap::Error> { - <#cli_name as clap::Parser>::try_parse_from(s.split(" ")) + pub fn try_parse_from(itr: I) -> Result<#cli_name, clap::Error> + where + I: ::std::iter::IntoIterator, + T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone, + { + <#cli_name as clap::Parser>::try_parse_from(itr) } } } diff --git a/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__flag.snap b/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__flag.snap index 128cfcb..5dcb81d 100644 --- a/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__flag.snap +++ b/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__flag.snap @@ -30,7 +30,7 @@ impl interactive_clap::FromCli for Args { where Self: Sized + interactive_clap::ToCli, { - let mut clap_variant = optional_clap_variant.unwrap_or_default(); + let mut clap_variant = optional_clap_variant.clone().unwrap_or_default(); let offline = clap_variant.offline.clone(); let new_context_scope = InteractiveClapContextScopeForArgs { offline: offline.into(), @@ -49,12 +49,19 @@ impl Args { Err(err) => Err(err.into()), } } - fn try_parse() -> Result { + pub fn try_parse() -> Result { ::try_parse() } - fn parse() -> CliArgs { + pub fn parse() -> CliArgs { ::parse() } + pub fn try_parse_from(itr: I) -> Result + where + I: ::std::iter::IntoIterator, + T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone, + { + ::try_parse_from(itr) + } } impl From for CliArgs { fn from(args: Args) -> Self { @@ -63,4 +70,3 @@ impl From for CliArgs { } } } - diff --git a/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__simple_struct.snap b/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__simple_struct.snap index f5a3daf..8b00ce9 100644 --- a/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__simple_struct.snap +++ b/interactive-clap-derive/src/tests/snapshots/interactive_clap_derive__tests__test_simple_struct__simple_struct.snap @@ -33,7 +33,7 @@ impl interactive_clap::FromCli for Args { where Self: Sized + interactive_clap::ToCli, { - let mut clap_variant = optional_clap_variant.unwrap_or_default(); + let mut clap_variant = optional_clap_variant.clone().unwrap_or_default(); if clap_variant.age.is_none() { clap_variant .age = match Self::input_age(&context) { @@ -112,12 +112,19 @@ impl Args { Err(err) => Err(err.into()), } } - fn try_parse() -> Result { + pub fn try_parse() -> Result { ::try_parse() } - fn parse() -> CliArgs { + pub fn parse() -> CliArgs { ::parse() } + pub fn try_parse_from(itr: I) -> Result + where + I: ::std::iter::IntoIterator, + T: ::std::convert::Into<::std::ffi::OsString> + ::std::clone::Clone, + { + ::try_parse_from(itr) + } } impl From for CliArgs { fn from(args: Args) -> Self { @@ -128,4 +135,3 @@ impl From for CliArgs { } } } -