You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Often, user input can be Optional, and an empty String or T::default() value is not desirable.
Describe the solution you'd like
Allow Text and CustomType inputs to return Option<String> and Option<T> -- or implement OptionText / OptionType structs (because the trait bound Option<f64>: std::str::FromStr, etc. is not satisfied, etc., which may be non-trivial to work around) -- that return None if the field is left blank or an optional "default" or "none" value is provided.
Describe alternatives you've considered
Here are a couple illustrative wrappers I just hacked together that are not feature-complete. The drawback to these is the lack of ability to chain the .with_ methods as in the API -- requiring arguments or a struct argument. Would be nice to have the Option<T> ability "built-in" to the crate.
EDIT: It's complicated to handle None values + all the other options and get it to work right.
use std::{fmt::Display, str::FromStr};use anyhow::Result;use inquire::{CustomType, formatter::StringFormatter, validator::StringValidator};/// additional_none_values: `&str` values (in addition to an empty string) that are treated as None.pubfnoptional_text(prompt:&str,current:&Option<String>,additional_none_values:&[&str],validators:&[Box<dynStringValidator>],formatter:Option<StringFormatter<'_>>,) -> Result<Option<String>>{letmut input = inquire::Text::new(prompt).with_validators(validators);ifletSome(current) = current {
input = input.with_default(current);};ifletSome(formatter) = formatter {
input = input.with_formatter(formatter);}let none_values = additional_none_values
.iter().map(|v| v.to_lowercase()).chain(["null".to_string(),"<none>".to_string(),"none".to_string()]).collect::<Vec<_>>();let formatter = &|s:&str| {if s.is_empty() || none_values.contains(&s.to_lowercase()){"<None>".to_string()}else{
s.to_string()}};
input = input.with_formatter(formatter);let text = input.prompt()?.trim().to_string();if text.is_empty() || none_values.contains(&text.to_lowercase()){Ok(None)}else{Ok(Some(text))}}/// `none_value`: The value of T that represents None. If None, `T::default()` is used./// additional_none_values: `&str` values that are converted to `none_value` by the parser.pubfnoptional_custom_input<T>(prompt:&str,current:&Option<T>,none_value:Option<&T>,additional_none_values:&[&str],) -> Result<Option<T>>whereT:Default + PartialEq + Clone + FromStr + Display,{let none_value = match none_value {Some(none_value) => none_value,None => &T::default(),};let formatter = &|v:T| {if v == *none_value {"<None>".to_string()}else{
v.to_string()}};let none_values = additional_none_values
.iter().map(|v| v.to_lowercase()).chain(["null".to_string(),"<none>".to_string(),"none".to_string()]).collect::<Vec<_>>();let value = CustomType::<T>::new(prompt).with_default(current.clone().unwrap_or(none_value.clone())).with_formatter(formatter).with_default_value_formatter(formatter).with_parser(&|s| {let s = s.trim();if s.is_empty() || none_values.contains(&s.to_lowercase()){Ok(none_value.clone())}else{
s.parse().map_err(|_| ())}}).prompt()?;if value == *none_value {Ok(None)}else{Ok(Some(value))}}
Additional context
I assume this section is Optional. 🤣
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
Often, user input can be
Option
al, and an emptyString
orT::default()
value is not desirable.Describe the solution you'd like
Allow
Text
andCustomType
inputs to returnOption<String>
andOption<T>
-- or implementOptionText
/OptionType
structs (because the trait boundOption<f64>: std::str::FromStr
, etc. is not satisfied, etc., which may be non-trivial to work around) -- that returnNone
if the field is left blank or an optional "default" or "none" value is provided.Describe alternatives you've considered
Here are a couple illustrative wrappers I just hacked together that are not feature-complete. The drawback to these is the lack of ability to chain the
.with_
methods as in the API -- requiring arguments or astruct
argument. Would be nice to have theOption<T>
ability "built-in" to the crate.EDIT: It's complicated to handle
None
values + all the other options and get it to work right.Additional context
I assume this section is
Option
al. 🤣The text was updated successfully, but these errors were encountered: