[Haskell-cafe] Command line prompt templates
Donald Bruce Stewart
dons at cse.unsw.edu.au
Thu Nov 23 21:03:03 EST 2006
ithika:
> I was trying to write my own equivalent to Don Stewart's mkcabal but
> ended up getting sidetracked. I made some generalised prompts for use at
> the command line and wanted to get some feedback on them.
>
> The full code can be found at [1] but the basic summary is like this:
>
> > prompt :: String -> IO String
>
> Simple prompt - supply a question and receive the user's answer.
>
> > promptYesNo :: String -> Maybe String -> IO Bool
>
> A yes/no question, with an optional value to ask about. I suppose this
> could be simplified to just one string.
>
> > promptList :: String -> [String] -> Maybe Integer -> IO Integer
>
> Ask the user to choose from a list, where the result is the number
> chosen. The optional value is a default if the user doesn't pick a
> number.
>
> > promptListEdit :: String -> [String] -> Maybe Integer -> IO String
>
> As above, but the last option in the list is an invitation to provide
> your own answer. Again the optional value is the default choice if
> nothing else is chosen. The result here is necessarily the string rather
> than the integer cos I didn't want to complicate matters further with
> stuff like Either types.
>
> If you can see any flaws or have any suggestions please let me know!
Looks pretty good, though you use
case x :: Bool of
True -> ...
False -> ...
when
if x then ... else ...
would be preferred.
I wonder if there's a prompt API out there for python or something you
could use for inspiration?
-- Don
More information about the Haskell-Cafe
mailing list