Command Line Widgets

Interactive Prompts

The "Nag" prompts will keep repeating until a valid reply is received, as defined by the user-supplied list of options (note that "exit" is always a valid option whether specified or not).

You can optionally specify a question (optional 2nd parameter) otherwise the default will be used.

Multiple Choice

This is the easiest for the user to respond to (just type a number).

<?php    

$prompt 
= new Nag(new MultipleChoice(array('foo''bar')));
$chosen $prompt->getReply();

This will output:

please type a number to select an option:
0   foo
1   bar

The prompt will repeat indefinitely until the user types a 0 or 1 (or "exit").

Select From List

Here, the options are presented in brackets and the user must type in one of the options — hence it's better if these are short. An optional message is passed in this example and overrides the default "please select an option:".

<?php    

$prompt 
= new Nag(
    new 
ChooseOne(
        array(
'm''f'), 
        
'Are you male or female?'));
$chosen $prompt->getReply();

This will output:

Are you male or female? (m, f)

The prompt will repeat indefinitely until the user types an "m" or "f" (or "exit").

Add an empty string to the array if you need to allow the user to select "none".

<?php    

$prompt 
= new Nag(
    new 
ChooseOne(
        array(
'4.1''5.0'''), 
        
'Which mysql version?'));
$chosen $prompt->getReply();

This will output:

Which mysql version? (4.1, 5.0, or press [enter] to select none)

Table Display

Align columns with unpredictably-sized elements.

<?php    

$table 
= new TableDisplayer;
$table->newRow(array('foo''short'1));
$table->newRow(array('bar''enormousmonstername'2));
$table->display('Table Title');

This will output:

[Table Title]
foo        short                      1        
bar        enormousmonstername        2

ShowQueryResult is much the same but adds column headers. The class assumes that input rows are hashes and creates headers from row keys. You can probably guess what I wrote this for.

<?php    

$table 
= new ShowQueryResult;
$table->newRow(array('a'=>'foo''b'=>'short''c'=>1));
$table->newRow(array('a'=>'bar','b'=>'enormousmonstername''c'=>2));
$table->display('Table Title');

This will output:

[Table Title]
a          b                          c        
-          -                          -
foo        short                      1        
bar        enormousmonstername        2

SourceForge.net Logo