From 64433c8077e118495ff157ae2394efe810f1e87c Mon Sep 17 00:00:00 2001 From: ardi Date: Tue, 24 Oct 2023 00:25:09 +0200 Subject: [PATCH] feat(derive): parse from string and try parse from string --- clap_builder/src/derive.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/clap_builder/src/derive.rs b/clap_builder/src/derive.rs index 7494e8461dc4..079949188d70 100644 --- a/clap_builder/src/derive.rs +++ b/clap_builder/src/derive.rs @@ -62,6 +62,30 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized { } } + /// Parse from a string, return Err on error. + fn try_parse_from_string(string: S) -> Result + where + S: AsRef, + { + let argv0 = std::iter::once(""); + let itr = string.as_ref().split_whitespace(); + let itr = argv0.chain(itr); + + Self::try_parse_from(itr) + } + + /// Parse from a string, exit on error. + fn parse_from_string(string: S) -> Self + where + S: AsRef, + { + let argv0 = std::iter::once(""); + let itr = string.as_ref().split_whitespace(); + let itr = argv0.chain(itr); + + Self::parse_from(itr) + } + /// Parse from iterator, return Err on error. fn try_parse_from(itr: I) -> Result where