summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Weber2007-09-18 16:23:55 +0000
committerChristian Weber2007-09-18 16:23:55 +0000
commitdca2691f83365aa3f7a953b4c11c89ce2d76c5ce (patch)
treee32fd7fc329ac545a3ec808c9fbbd70d4a4a4946
parent12a54427b17240ed3198441ef1f7db71052d33c5 (diff)
downloaditools-dca2691f83365aa3f7a953b4c11c89ce2d76c5ce.tar.gz
itools-dca2691f83365aa3f7a953b4c11c89ce2d76c5ce.tar.bz2
itools-dca2691f83365aa3f7a953b4c11c89ce2d76c5ce.zip
add getopt() and gets(), remove obsolet dba_version()
-rw-r--r--it.class135
1 files changed, 126 insertions, 9 deletions
diff --git a/it.class b/it.class
index 9595080..95bb2f7 100644
--- a/it.class
+++ b/it.class
@@ -23,6 +23,10 @@
class it
{
+ static $stdin_args = array();
+ static $stdin = null;
+ static $stdin_filename = "";
+ static $stdin_line = 0;
/**
* Clone an object and return copy, works for all PHP versions
@@ -184,15 +188,6 @@ function bail($message = "Bailed.\n")
/**
- * Return "db4" or "db2" depending on availability
- */
-function dba_handler()
-{
- return in_array("db4", dba_handlers()) ? "db4" : "db2";
-}
-
-
-/**
* Convert a string to ASCII-only chars, map/strip ISO-8859-1 accents
* @param $text Text to convert
* @return mapped/stripped version of $text contains only chars [0..127]
@@ -377,6 +372,7 @@ function _exec_quotevalue($value)
return escapeshellarg($result);
}
+
/**
* Convert an image to a given size and type (ensures input is an image)
* @param $p['in'] Input filename (mandatory)
@@ -398,6 +394,127 @@ function imageconvert($p)
return $result;
}
+
+/**
+ * Parse command line options with Usage given as template. Example:
+ * Usage: myprogram.php [OPTIONS] PATTERN
+ * -s, --short Use short ouput
+ * -f, --file=FILE Use FILE for input
+ * -x EXTENSION Ignore EXTENSION
+ * Value always stored in long opt. Use double blank before explanation. Non-option text ignored.
+ * Mandatory non-option arguments are stored under their lowercased name.
+ * @param $helplines Usage parsed to determine options
+ * @return Associative array of opts: Boolean options are set to true, remaining args returned in 'args'
+ */
+function getopt($helplines)
+{
+ $result = array('args' => array());
+
+ if ($indentation = it::match('^\s+', $helplines))
+ $helplines = it::replace($indentation => "\n", $helplines);
+
+ foreach(explode("\n", trim($helplines)) as $helpline)
+ {
+ $shortoptname = $shortoptarg = $longoptname = $longoptarg = "";
+ foreach (explode(',', $helpline) as $optdesc)
+ {
+ $optdesc = trim($optdesc);
+ if ($matches = (array)it::match('^--(\w[\w-]*)(=[A-Z])?', $optdesc))
+ list($longoptname, $longoptarg) = $matches;
+ elseif ($matches = (array)it::match('^-(\w)( [A-Z])?', $optdesc))
+ list($shortoptname, $shortoptarg) = $matches;
+ }
+
+ if ($longoptname || $shortoptname)
+ {
+ if ($longoptname && $shortoptname)
+ $alias[$shortoptname] = $longoptname;
+
+ $witharg[$longoptname ? $longoptname : $shortoptname] = $longoptarg || $shortoptarg;
+ }
+ }
+
+ $mandatoryargs = array();
+ if ($tmp = trim(it::replace("\n.*" => "", "^\S+\s+\S+\s*" => "", "\[.*?\]\s*" => "", trim($helplines))))
+ $mandatoryargs = preg_split('/\s+/', $tmp);
+
+ foreach (array_slice($_SERVER['argv'], 1) as $arg)
+ {
+ if ($eat)
+ {
+ if (it::match('^--?\w', $arg)) # Already next option => Missing argument?
+ $err = true;
+ else
+ $result[array_shift($eat)] = $arg;
+ }
+ elseif ($matches = (array)it::match('^--(\w[\w-]*)(=\S*)?', $arg))
+ {
+ list($optname, $val) = $matches;
+ if (!isset($witharg[$optname]) || isset($val) && !$witharg[$optname])
+ $err = true;
+ else if($witharg[$optname] && !$val)
+ $eat[] = $optname;
+ else
+ $result[$optname] = $val ? substr($val, 1) : true;
+ }
+ else if ($letters = it::match('^-(\w+)', $arg))
+ {
+ foreach (str_split($letters, 1) as $letter)
+ {
+ $optname = $alias[$letter] ? $alias[$letter] : $letter;
+ if ($witharg[$optname])
+ $eat[] = $optname;
+ else if (!isset($witharg[$optname]))
+ $err = true;
+ else
+ $result[$optname] = true;
+ }
+ }
+ elseif($mandatoryargs)
+ $result[strtolower(array_shift($mandatoryargs))] = $arg;
+ else
+ $result['args'][] = $arg;
+ }
+
+ if ($err || $eat || $result['h'] || $result['help'] || $mandatoryargs)
+ {
+ fputs(($result['h'] || $result['help'] ? STDOUT : STDERR), trim($helplines) . "\n");
+ exit(1);
+ }
+
+ it::$stdin_args = $result['args'] ? $result['args'] : array("-");
+ it::_stdin_next();
+
+ return $result;
+}
+
+function _stdin_next()
+{
+ if ($result = it::$stdin_args)
+ {
+ it::$stdin_filename = array_shift(it::$stdin_args);
+ it::$stdin = (it::$stdin_filename == "-") ? STDIN : @fopen(it::$stdin_filename, "r");
+ it::$stdin_line = 0;
+ }
+
+ return $result;
+}
+
+/**
+ * Get one line from stdin (or files given on command line) a la Perl <>.
+ * Note: You need to call getopt() before using this function.
+ * @return Line (including newline) or false on eof
+ */
+function gets()
+{
+ do {
+ $result = fgets(it::$stdin);
+ } while (($result === false) && it::_stdin_next());
+
+ it::$stdin_line++;
+ return $result;
+}
+
}
?>