Bookmark and Share

Saturday, February 13, 2010

Command line user input

Introduction

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

example


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.

<?php

require_once("rawdev/RawDev.php");
require_once(RAWDEV_LIB.'/Util/Stdin.php');

# 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";

?>

Conclusion


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.

Links

RStdin API Doc

No comments:

Post a Comment