Bookmark and Share

Saturday, February 13, 2010

Command line user input


RStdin is tiny class that deals with interactive keyboard input from the command line. For testing purposes the keyboard input can be faked.


This example first shows regular use. The output is trimmed by default but this can be changed (see RStdin API Doc). In the second part you can see how the keyboard input can be faked for testing purposes.



# regular use
$input = RStdin::read('Please enter input: ');
print "$input";

# testing use
RStdin::$buffer[] = 'hello'; # for testing purposes the input buffer can be set
$input = RStdin::read('Please enter input: ');
print "$input";



This class is useful when dealing with command line applications. RawDev uses this class in the Dumper utility in which you can interactively view nested complex data structures.


RStdin API Doc

No comments:

Post a Comment