Nick Sabalausky Wrote: > "Jesse Phillips" <jessekphillip...@gmail.com> wrote in message > news:j75t0k$2q6m$3...@digitalmars.com... > > > > I created a cmdln library to help with user interaction, which some seem > > to like. > > > > https://github.com/he-the-great/JPDLibs/tree/cmdln > > > > Looks very nice. I have a similar (but probably more basic) thing, too (just > one function: "prompt"): > > http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/cmd/plain.d#L216 > > > // -- Basic string prompt ------ > string input1 = prompt("Type some stuff: "); > > // -- String prompt with validation ------ > // (failureMsg is optional) > string promptMsg = "Do you want 'coffee' or 'tea'? "; > string failureMsg = "Please enter 'coffee' or 'tea', not '%s'"; > bool accept(string input) > { > return ["coffee", "tea"].contains(tolower(input)); > } > > // This will *not* return until the user enters a valid choice, > // so we don't need to do any more validation. > string input2 = prompt(promptMsg, &accept, failureMsg);
Interesting the similarities. I do provide more specialized functions for some of those: auto input2 = menu(promptMsg, ["coffee", "tea"]); auto input = require!(string, accept)(promptMsg); I haven't provide the option of displaying an error message, but it could go in the delegate you use in require. And I suppose prompt is a better name than userInput. On thing I've been kind of interested in is getting something set up that would allow providing the needed information via a config, args, or at runtime. but haven't done anything with that idea.