[erlang-questions] shell io dialogues

黃耀賢 (Yau-Hsien Huang) g9414002.pccu.edu.tw@REDACTED
Mon Feb 27 13:12:15 CET 2012


I'd like to use a question maker with some property then

       Val = xxx:ask_integer('What number do you want? ')

is defined in

       ask_integer(Question) ->
             Asker = ask([{'Format', fun my_format:is_num/1}]),
             Asker(Question).

Another type of asker is like

       ask_enumerated(Question) ->
             Asker = ask([{'Format', fun my_format:in_enum/1}]),
             Asker(Question).

The ask/1 is the core to serve customized dialogues.

       ask(Options) ->
            fun (Question) ->
                   Reply = io:get_line(Question);
                   case Reply of
                         eof -> stop;
                         {error, Reason} -> {error, Reason};
                         "\n" -> stop;
                         Data ->
                                case proplists:lookup('Format', Options) of
                                     none -> {error, 'bad format'};
                                     {'Format', Func} ->
                                           Word = string:left(Data,
string:len(Data)-1),
                                           case Func(Word) of
                                                  {ok, Value) -> value;
                                                  {error, Reason} ->
{error, Reason}
                                           end
                                end
                   end
            end.

Then format functions my_format:is_num/1, my_format:in_enum/1, and at el.
are written to check if a word fits the corresponding format. Parsers may be
used in a format function for complicate format.

Best Regards,
-yau

On Mon, Feb 27, 2012 at 3:32 AM, Joe Armstrong <erlang@REDACTED> wrote:

> Hi,
>
> Has anybody written any code that makes inputting data from the shell
> less painful?
>
> Id like to say something like
>
>   Val = xxx:read_integer([{prompt,'Port
> number:'},{expect,integer,1000,2000},{default,1234}, ....
>
>  I'd like to customize what happens if the user does not enter an
> integer etc. (ie ask again, abort, etc.)
>
>  also read_enumerated([....., {expect, ["foo","bar",friends"]} ,,,,
>
>  (this would only accept one of the three strings in the command and
> give me tab completion etc.)
>
> I'd like to make a text interface that asks questions, and takes
> reasonable actions if the user enters
> bad data, without writing zillions of lines of code. Any pointers?
>
> Cheers
>
> /Joe
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 

Best Regards.

--- Y-H. H.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120227/08a8a7a1/attachment.htm>


More information about the erlang-questions mailing list