Conditional Prompt #1845
-
Hi! I would like to know if it's possible to create a Prompt that needs a conditional function to return true to continue. This could be useful for example if I need the user to input a file or a host and I need to check whether their input is correct (file exists or host is up). It could be used the following way: import os
from rich.prompt import Prompt
def file_exists_condition(user_in):
return os.path.exists(user_in)
filename = Prompt.ask('Enter filename', file_exists_condition) Inside prompt's code, it simply needs to run the custom function with the user input as a parameter and if it fails, re-ask for another input. Is there a current way to do this or does it need to be a feature request? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Not as such, but this doesn't need to be a part of the core library as you could implement it while a loop. while True:
filename = Prompt.ask("Enter a filename")
if not os.path.exists(filename):
print("file must exist")
else:
break |
Beta Was this translation helpful? Give feedback.
Not as such, but this doesn't need to be a part of the core library as you could implement it while a loop.