.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Term::UI 3perl" .TH Term::UI 3perl "2011-09-19" "perl v5.14.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Term::UI \- Term::ReadLine UI made easy .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Term::UI; \& use Term::ReadLine; \& \& my $term = Term::ReadLine\->new(\*(Aqbrand\*(Aq); \& \& my $reply = $term\->get_reply( \& prompt => \*(AqWhat is your favourite colour?\*(Aq, \& choices => [qw|blue red green|], \& default => blue, \& ); \& \& my $bool = $term\->ask_yn( \& prompt => \*(AqDo you like cookies?\*(Aq, \& default => \*(Aqy\*(Aq, \& ); \& \& \& my $string = q[some_command \-option \-\-no\-foo \-\-quux=\*(Aqthis thing\*(Aq]; \& \& my ($options,$munged_input) = $term\->parse_options($string); \& \& \& ### don\*(Aqt have Term::UI issue warnings \-\- default is \*(Aq1\*(Aq \& $Term::UI::VERBOSE = 0; \& \& ### always pick the default (good for non\-interactive terms) \& ### \-\- default is \*(Aq0\*(Aq \& $Term::UI::AUTOREPLY = 1; \& \& ### Retrieve the entire session as a printable string: \& $hist = Term::UI::History\->history_as_string; \& $hist = $term\->history_as_string; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Term::UI\*(C'\fR is a transparent way of eliminating the overhead of having to format a question and then validate the reply, informing the user if the answer was not proper and re-issuing the question. .PP Simply give it the question you want to ask, optionally with choices the user can pick from and a default and \f(CW\*(C`Term::UI\*(C'\fR will \s-1DWYM\s0. .PP For asking a yes or no question, there's even a shortcut. .SH "HOW IT WORKS" .IX Header "HOW IT WORKS" \&\f(CW\*(C`Term::UI\*(C'\fR places itself at the back of the \f(CW\*(C`Term::ReadLine\*(C'\fR \&\f(CW@ISA\fR array, so you can call its functions through your term object. .PP \&\f(CW\*(C`Term::UI\*(C'\fR uses \f(CW\*(C`Term::UI::History\*(C'\fR to record all interactions with the commandline. You can retrieve this history, or alter the filehandle the interaction is printed to. See the \&\f(CW\*(C`Term::UI::History\*(C'\fR manpage or the \f(CW\*(C`SYNOPSIS\*(C'\fR for details. .SH "METHODS" .IX Header "METHODS" .ie n .SS "$reply = $term\->get_reply( prompt => 'question?', [choices => \e@list, default => $list[0], multi => \s-1BOOL\s0, print_me => ""extra text to print & record"", allow => $ref] );" .el .SS "\f(CW$reply\fP = \f(CW$term\fP\->get_reply( prompt => 'question?', [choices => \e@list, default => \f(CW$list\fP[0], multi => \s-1BOOL\s0, print_me => ``extra text to print & record'', allow => \f(CW$ref\fP] );" .IX Subsection "$reply = $term->get_reply( prompt => 'question?', [choices => @list, default => $list[0], multi => BOOL, print_me => extra text to print & record, allow => $ref] );" \&\f(CW\*(C`get_reply\*(C'\fR asks a user a question, and then returns the reply to the caller. If the answer is invalid (more on that below), the question will be reposed, until a satisfactory answer has been entered. .PP You have the option of providing a list of choices the user can pick from using the \f(CW\*(C`choices\*(C'\fR argument. If the answer is not in the list of choices presented, the question will be reposed. .PP If you provide a \f(CW\*(C`default\*(C'\fR answer, this will be returned when either \&\f(CW$AUTOREPLY\fR is set to true, (see the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section further below), or when the user just hits \f(CW\*(C`enter\*(C'\fR. .PP You can indicate that the user is allowed to enter multiple answers by toggling the \f(CW\*(C`multi\*(C'\fR flag. Note that a list of answers will then be returned to you, rather than a simple string. .PP By specifying an \f(CW\*(C`allow\*(C'\fR hander, you can yourself validate the answer a user gives. This can be any of the types that the Params::Check \f(CW\*(C`allow\*(C'\fR function allows, so please refer to that manpage for details. .PP Finally, you have the option of adding a \f(CW\*(C`print_me\*(C'\fR argument, which is simply printed before the prompt. It's printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&A with the user, and retrieve it later using the \&\f(CW\*(C`Term::UI\->history_as_string\*(C'\fR function. .PP See the \f(CW\*(C`EXAMPLES\*(C'\fR section for samples of how to use this function. .ie n .SS "$bool = $term\->ask_yn( prompt => ""your question"", [default => (y|1,n|0), print_me => ""extra text to print & record""] )" .el .SS "\f(CW$bool\fP = \f(CW$term\fP\->ask_yn( prompt => ``your question'', [default => (y|1,n|0), print_me => ``extra text to print & record''] )" .IX Subsection "$bool = $term->ask_yn( prompt => your question, [default => (y|1,n|0), print_me => extra text to print & record] )" Asks a simple \f(CW\*(C`yes\*(C'\fR or \f(CW\*(C`no\*(C'\fR question to the user, returning a boolean indicating \f(CW\*(C`true\*(C'\fR or \f(CW\*(C`false\*(C'\fR to the caller. .PP The \f(CW\*(C`default\*(C'\fR answer will automatically returned, if the user hits \&\f(CW\*(C`enter\*(C'\fR or if \f(CW$AUTOREPLY\fR is set to true. See the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section further below. .PP Also, you have the option of adding a \f(CW\*(C`print_me\*(C'\fR argument, which is simply printed before the prompt. It's printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&A with the user, and retrieve it later using the \&\f(CW\*(C`Term::UI\->history_as_string\*(C'\fR function. .PP See the \f(CW\*(C`EXAMPLES\*(C'\fR section for samples of how to use this function. .ie n .SS "($opts, $munged) = $term\->parse_options( \s-1STRING\s0 );" .el .SS "($opts, \f(CW$munged\fP) = \f(CW$term\fP\->parse_options( \s-1STRING\s0 );" .IX Subsection "($opts, $munged) = $term->parse_options( STRING );" \&\f(CW\*(C`parse_options\*(C'\fR will convert all options given from an input string to a hash reference. If called in list context it will also return the part of the input string that it found no options in. .PP Consider this example: .PP .Vb 2 \& my $str = q[command \-\-no\-foo \-\-baz \-\-bar=0 \-\-quux=bleh ] . \& q[\-\-option="some\*(Aqthing" \-one\-dash \-single=blah\*(Aq arg]; \& \& my ($options,$munged) = $term\->parse_options($str); \& \& ### $options would contain: ### \& $options = { \& \*(Aqfoo\*(Aq => 0, \& \*(Aqbar\*(Aq => 0, \& \*(Aqone\-dash\*(Aq => 1, \& \*(Aqbaz\*(Aq => 1, \& \*(Aqquux\*(Aq => \*(Aqbleh\*(Aq, \& \*(Aqsingle\*(Aq => \*(Aqblah\e\*(Aq\*(Aq, \& \*(Aqoption\*(Aq => \*(Aqsome\e\*(Aqthing\*(Aq \& }; \& \& ### and this is the munged version of the input string, \& ### ie what\*(Aqs left of the input minus the options \& $munged = \*(Aqcommand arg\*(Aq; .Ve .PP As you can see, you can either use a single or a double \f(CW\*(C`\-\*(C'\fR to indicate an option. If you prefix an option with \f(CW\*(C`no\-\*(C'\fR and do not give it a value, it will be set to 0. If it has no prefix and no value, it will be set to 1. Otherwise, it will be set to its value. Note also that it can deal fine with single/double quoting issues. .ie n .SS "$str = $term\->history_as_string" .el .SS "\f(CW$str\fP = \f(CW$term\fP\->history_as_string" .IX Subsection "$str = $term->history_as_string" Convenience wrapper around \f(CW\*(C`Term::UI::History\->history_as_string\*(C'\fR. .PP Consult the \f(CW\*(C`Term::UI::History\*(C'\fR man page for details. .SH "GLOBAL VARIABLES" .IX Header "GLOBAL VARIABLES" The behaviour of Term::UI can be altered by changing the following global variables: .ie n .SS "$Term::UI::VERBOSE" .el .SS "\f(CW$Term::UI::VERBOSE\fP" .IX Subsection "$Term::UI::VERBOSE" This controls whether Term::UI will issue warnings and explanations as to why certain things may have failed. If you set it to 0, Term::UI will not output any warnings. The default is 1; .ie n .SS "$Term::UI::AUTOREPLY" .el .SS "\f(CW$Term::UI::AUTOREPLY\fP" .IX Subsection "$Term::UI::AUTOREPLY" This will make every question be answered by the default, and warn if there was no default provided. This is particularly useful if your program is run in non-interactive mode. The default is 0; .ie n .SS "$Term::UI::INVALID" .el .SS "\f(CW$Term::UI::INVALID\fP" .IX Subsection "$Term::UI::INVALID" This holds the string that will be printed when the user makes an invalid choice. You can override this string from your program if you, for example, wish to do localization. The default is \f(CW\*(C`Invalid selection, please try again: \*(C'\fR .ie n .SS "$Term::UI::History::HISTORY_FH" .el .SS "\f(CW$Term::UI::History::HISTORY_FH\fP" .IX Subsection "$Term::UI::History::HISTORY_FH" This is the filehandle all the print statements from this module are being sent to. Please consult the \f(CW\*(C`Term::UI::History\*(C'\fR manpage for details. .PP This defaults to \f(CW*STDOUT\fR. .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Basic get_reply sample" .IX Subsection "Basic get_reply sample" .Vb 2 \& ### ask a user (with an open question) for their favourite colour \& $reply = $term\->get_reply( prompt => \*(AqYour favourite colour? ); .Ve .PP which would look like: .PP .Vb 1 \& Your favourite colour? .Ve .PP and \f(CW$reply\fR would hold the text the user typed. .SS "get_reply with choices" .IX Subsection "get_reply with choices" .Vb 4 \& ### now provide a list of choices, so the user has to pick one \& $reply = $term\->get_reply( \& prompt => \*(AqYour favourite colour?\*(Aq, \& choices => [qw|red green blue|] ); .Ve .PP which would look like: .PP .Vb 3 \& 1> red \& 2> green \& 3> blue \& \& Your favourite colour? .Ve .PP \&\f(CW$reply\fR will hold one of the choices presented. \f(CW\*(C`Term::UI\*(C'\fR will repose the question if the user attempts to enter an answer that's not in the list of choices. The string presented is held in the \f(CW$Term::UI::INVALID\fR variable (see the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section for details. .SS "get_reply with choices and default" .IX Subsection "get_reply with choices and default" .Vb 5 \& ### provide a sensible default option \-\- everyone loves blue! \& $reply = $term\->get_reply( \& prompt => \*(AqYour favourite colour?\*(Aq, \& choices => [qw|red green blue|], \& default => \*(Aqblue\*(Aq ); .Ve .PP which would look like: .PP .Vb 3 \& 1> red \& 2> green \& 3> blue \& \& Your favourite colour? [3]: .Ve .PP Note the default answer after the prompt. A user can now just hit \f(CW\*(C`enter\*(C'\fR (or set \f(CW$Term::UI::AUTOREPLY\fR \*(-- see the \f(CW\*(C`GLOBAL VARIABLES\*(C'\fR section) and the sensible answer 'blue' will be returned. .SS "get_reply using print_me & multi" .IX Subsection "get_reply using print_me & multi" .Vb 7 \& ### allow the user to pick more than one colour and add an \& ### introduction text \& @reply = $term\->get_reply( \& print_me => \*(AqTell us what colours you like\*(Aq, \& prompt => \*(AqYour favourite colours?\*(Aq, \& choices => [qw|red green blue|], \& multi => 1 ); .Ve .PP which would look like: .PP .Vb 4 \& Tell us what colours you like \& 1> red \& 2> green \& 3> blue \& \& Your favourite colours? .Ve .PP An answer of \f(CW\*(C`3 2 1\*(C'\fR would fill \f(CW@reply\fR with \f(CW\*(C`blue green red\*(C'\fR .SS "get_reply & allow" .IX Subsection "get_reply & allow" .Vb 6 \& ### pose an open question, but do a custom verification on \& ### the answer, which will only exit the question loop, if \& ### the answer matches the allow handler. \& $reply = $term\->get_reply( \& prompt => "What is the magic number?", \& allow => 42 ); .Ve .PP Unless the user now enters \f(CW42\fR, the question will be reposed over and over again. You can use more sophisticated \f(CW\*(C`allow\*(C'\fR handlers (even subroutines can be used). The \f(CW\*(C`allow\*(C'\fR handler is implemented using \&\f(CW\*(C`Params::Check\*(C'\fR's \f(CW\*(C`allow\*(C'\fR function. Check its manpage for details. .SS "an elaborate ask_yn sample" .IX Subsection "an elaborate ask_yn sample" .Vb 5 \& ### ask a user if he likes cookies. Default to a sensible \*(Aqyes\*(Aq \& ### and inform him first what cookies are. \& $bool = $term\->ask_yn( prompt => \*(AqDo you like cookies?\*(Aq, \& default => \*(Aqy\*(Aq, \& print_me => \*(AqCookies are LOVELY!!!\*(Aq ); .Ve .PP would print: .PP .Vb 2 \& Cookies are LOVELY!!! \& Do you like cookies? [Y/n]: .Ve .PP If a user then simply hits \f(CW\*(C`enter\*(C'\fR, agreeing with the default, \&\f(CW$bool\fR would be set to \f(CW\*(C`true\*(C'\fR. (Simply hitting 'y' would also return \f(CW\*(C`true\*(C'\fR. Hitting 'n' would return \f(CW\*(C`false\*(C'\fR) .PP We could later retrieve this interaction by printing out the Q&A history as follows: .PP .Vb 1 \& print $term\->history_as_string; .Ve .PP which would then print: .PP .Vb 2 \& Cookies are LOVELY!!! \& Do you like cookies? [Y/n]: y .Ve .PP There's a chance we're doing this non-interactively, because a console is missing, the user indicated he just wanted the defaults, etc. .PP In this case, simply setting \f(CW$Term::UI::AUTOREPLY\fR to true, will return from every question with the default answer set for the question. Do note that if \f(CW\*(C`AUTOREPLY\*(C'\fR is true, and no default is set, \f(CW\*(C`Term::UI\*(C'\fR will warn about this and return \f(CW\*(C`undef\*(C'\fR. .SH "See Also" .IX Header "See Also" \&\f(CW\*(C`Params::Check\*(C'\fR, \f(CW\*(C`Term::ReadLine\*(C'\fR, \f(CW\*(C`Term::UI::History\*(C'\fR .SH "BUG REPORTS" .IX Header "BUG REPORTS" Please report bugs or other issues to . .SH "AUTHOR" .IX Header "AUTHOR" This module by Jos Boumans . .SH "COPYRIGHT" .IX Header "COPYRIGHT" This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.