Jaymon / captain

command line python scripts for humans
MIT License
13 stars 1 forks source link

Prompt should be able to normalize input #50

Closed Jaymon closed 5 years ago

Jaymon commented 5 years ago

For example, to do a yes or no:

echo.prompt("Proceed?", choices=["Y", "y", "yes", "Yes", "N", "n", "No", "no"])

would be cool if you could do something like:

echo.prompt("Proceed?", type=lambda x: x.lower(), choices=["y", "yes", "n", "no"])

and would be even better if you could also do:

answer = echo.prompt(
    "Proceed?", 
    type=lambda x: x.lower(), 
    choices={"yes": ["y", "yes"], "no": ["n", "no"]}
)

if answer == "yes":
    # do the yes thing

So the choices dict would have keys that are the value that would be returned if the value of the key is equal to input.

Jaymon commented 5 years ago

Another thing that should happen, choices like:

choices={"yes": ["y", "yes"], "no": ["n", "no"]}

should be able to be concisely written as:

choices={"yes": ["y"], "no": ["n"]}

Because the keys would be combined with the values for purposes of validating the input