note 25754 added to features.commandline
| From: | bbisgod at rack1 dot php dot net | Date: | Fri, 04 Oct 2002 11:28:44 +0000 |
| Subject: | note 25754 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37663@lists.php.net to get a copy of this message | ||
I just wrote a nice addition to any CLI user's library:
screen.php:
<?
if (version_compare(PHP_VERSION, "4.3.0") == -1) {
if (!defined("STDIN")) define("STDIN", fopen("php://stdin",
"r"));
if (!defined("STDOUT")) define("STDOUT", fopen("php://stdout",
"w"));
if (!defined("STDERR")) define("STDERR", fopen("php://stderr",
"w"));
}
class screen {
var $_question = array();
var $_answer = array();
var $_default = array();
var $_regexp = array();
var $_qdef;
function screen ($items, $qdef = "", $load = true) {
$this->_qdef = $qdef;
$this->loadDefaults();
foreach ($items as $key => $val) {
$this->_question[$key] = $val['text'];
if (isset($val['regexp'])) $this->_regexp[$key] = $val['regexp'];
if (isset($val['default'])) $this->_default[$key] = $val['default'];
}
}
function askQuestions ($save = false) {
foreach ($this->_question as $key => $q) {
$input = "";
if (isset($this->_regexp[$key])) {
while (!preg_match("/^".$this->_regexp[$key]."$/i",$input)) {
echo $q." ".((isset($this->_default[$key])) ? "[" .
$this->_default[$key] . "] " : "").": ";
$input = $this->input();
if ($input == "") $input = ((isset($this->_default[$key])) ?
$this->_default[$key] : "");
}
} else {
echo $q." ".((isset($this->_default[$key])) ? "[" .
$this->_default[$key] . "] " : "").": ";
$input = $this->input();
if ($input == "") $input = ((isset($this->_default[$key])) ?
$this->_default[$key] : "");
}
$this->_answer[$key] = $input;
}
if ($save) {
$this->saveDefaults();
}
return $this->_answer;
}
function input() {
$in = fgets(STDIN, 255);
return trim($in);
}
function saveDefaults() {
$savestr = "";
if ($this->_qdef != "") {
foreach ($this->_answer as $key => $val) {
$savestr .= "$key=$val\n";
}
}
$fp = fopen($this->_qdef, "wb");
fputs($fp, $savestr);
fclose($fp);
}
function loadDefaults () {
if ($this->_qdef == "" OR !file_exists($this->_qdef)) return;
$loadstr = "";
$fp = fopen($this->_qdef, "rb");
while ($get = fgets($fp, 1024)) {
$loadstr .= $get;
}
$loadstr = explode("\n", $loadstr);
foreach($loadstr as $get) {
$qa = explode("=", $get, 2);
if (count($qa) == 2) {
$this->_default[$qa[0]] = trim($qa[1]);
}
}
}
}
?>
--EOF--
Then we can use some code like this:
$q = array(
"ip" => array("text" => "Please enter the servers IP",
"regexp" => "[\d\.]+"),
"port" => array("text" => "Please enter the servers port",
"regexp" => "\d+"),
"r_eol" => array("text" => "Please enter the recieved EoL
(null/cr/lf/crlf)", "regexp" => "null|cr|lf|crlf"),
"s_eol" => array("text" => "Please enter the send EoL
(null/cr/lf/crlf)", "regexp" => "null|cr|lf|crlf"),
"ref" => array("text" => "Please enter the server referece",
"regexp" => "[A-z0-9_]+"),
"again" => array("text" => "Would you like to add another server
(y/n)", "regexp" => "[yn]"));
$count = 0;
$again = 'y';
while (strtolower($again) == "y") {
$qs = new screen($q, "data/qdef_".$count.".dat");
$servs[$count] = $qs->askQuestions(true);
$again = $servs[$count]['again'];
$count++;
}
To gather information from the user at the command line. Most of you should be firmiliar with this
format.
Features:
Saves defaults,
Manual override defaults,
Regexp validation,
Tidy!
Suggestions?
--
http://www.php.net/manual/en/features.commandline.php
http://master.php.net/manage/user-notes.php?action=edit+25754
http://master.php.net/manage/user-notes.php?action=delete+25754
http://master.php.net/manage/user-notes.php?action=reject+25754