I'd like to use a question maker with some property then<div><br></div><div> Val = xxx:ask_integer('What number do you want? ')</div><div><br></div><div>is defined in</div><div><br></div><div> ask_integer(Question) -></div>
<div> Asker = ask([{'Format', fun my_format:is_num/1}]),</div><div> Asker(Question).</div><div><br></div><div>Another type of asker is like</div><div><br></div><div> ask_enumerated(Question) -></div>
<div> Asker = ask([{'Format', fun my_format:in_enum/1}]),</div><div> Asker(Question).</div><div><br></div><div>The ask/1 is the core to serve customized dialogues.</div><div><br></div><div>
ask(Options) -></div><div> fun (Question) -></div><div> Reply = io:get_line(Question);</div><div> case Reply of</div><div> eof -> stop;</div>
<div> {error, Reason} -> {error, Reason};</div><div> "\n" -> stop;</div><div> Data -></div><div> case proplists:lookup('Format', Options) of</div>
<div> none -> {error, 'bad format'};</div><div> {'Format', Func} -></div><div> Word = string:left(Data, string:len(Data)-1),</div>
<div> case Func(Word) of</div><div> {ok, Value) -> value;</div><div> {error, Reason} -> {error, Reason}</div>
<div> end</div><div> end</div><div> end</div><div> end.</div><div><br></div><div>Then format functions my_format:is_num/1, my_format:in_enum/1, and at el.</div>
<div>are written to check if a word fits the corresponding format. Parsers may be</div><div>used in a format function for complicate format.</div><div><br></div><div>Best Regards,</div><div>-yau</div><div><br><div class="gmail_quote">
On Mon, Feb 27, 2012 at 3:32 AM, Joe Armstrong <span dir="ltr"><<a href="mailto:erlang@gmail.com">erlang@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
Has anybody written any code that makes inputting data from the shell<br>
less painful?<br>
<br>
Id like to say something like<br>
<br>
Val = xxx:read_integer([{prompt,'Port<br>
number:'},{expect,integer,1000,2000},{default,1234}, ....<br>
<br>
I'd like to customize what happens if the user does not enter an<br>
integer etc. (ie ask again, abort, etc.)<br>
<br>
also read_enumerated([....., {expect, ["foo","bar",friends"]} ,,,,<br>
<br>
(this would only accept one of the three strings in the command and<br>
give me tab completion etc.)<br>
<br>
I'd like to make a text interface that asks questions, and takes<br>
reasonable actions if the user enters<br>
bad data, without writing zillions of lines of code. Any pointers?<br>
<br>
Cheers<br>
<br>
/Joe<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div><br></div>Best Regards.<div><br></div><div>--- Y-H. H.</div><div><br></div><br>
</div>