Skip to content

Optional input API #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joepio opened this issue Apr 9, 2022 · 5 comments
Closed

Optional input API #186

joepio opened this issue Apr 9, 2022 · 5 comments
Labels
p-input Prompt: Input

Comments

@joepio
Copy link

joepio commented Apr 9, 2022

Hi there! Thanks for creating this :)

I have some input fields that are optional. I'd like to have an API similar to this:

let number: Option<u32> = Input::new().with_prompt("What's your age?").interact()?;

Maybe I'm missing something in the docs, but I don't really see how I should achieve these optional inputs.

I know you can use .allow_empty, but that doesn't really work for things like numbers.

@joepio
Copy link
Author

joepio commented Jun 9, 2024

Back here 2yrs later, trying again to switch from promptly to dialoguer.

Anyways, I still want to have a "skippable" form field, but I don't know how I can achieve this.

fn prompt_opt(msg: &str) -> AtomicResult<Option<String>> {
    dialoguer::Input::new()
        .with_prompt(msg)
        .interact()
        .map(|s: String| if s.is_empty() { None } else { Some(s) })
        .map_err(|e| format!("Error while prompting: {}", e).into())
}

This seemed sensible, but if I press enter when the string is empty nothing happens. I'm assuming I'm missing something obvious!

@jonassmedegaard
Copy link

I guess you are missing .allow_empty(true).

@pksunkara
Copy link
Collaborator

pksunkara commented Jun 9, 2024

Try interact_opt instead of interact

@jonassmedegaard
Copy link

Try interact_opt instead of interact

Ah, right - that too.

@pksunkara
Copy link
Collaborator

Closing this until further response that the suggestion doesn't work.

@pksunkara pksunkara closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p-input Prompt: Input
Projects
None yet
Development

No branches or pull requests

3 participants