From: Marcus Boerger Date: Fri, 11 May 2007 18:07:22 +0000 (+0000) Subject: - Simplify argument handling X-Git-Tag: RELEASE_1_2_0~90 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a45852b27bbcdd7112e5b1278249923aa7cdc6c0;p=php - Simplify argument handling - Add -i & -x to commands list & tree --- diff --git a/ext/phar/phar/clicommand.inc b/ext/phar/phar/clicommand.inc index a2e0498662..c6b56f4eb8 100755 --- a/ext/phar/phar/clicommand.inc +++ b/ext/phar/phar/clicommand.inc @@ -235,6 +235,51 @@ abstract class CLICommand return "This help."; } + private function cli_help_get_args($args, $l, $sp, $required) + { + $inf = ""; + foreach(call_user_func(array($this, $args)) as $arg => $conf) + { + if ((isset($conf['required']) && $conf['required']) != $required) + { + continue; + } + if (strlen($arg)) + { + $arg = "-$arg "; + } + else + { + $arg = "... "; + } + $inf .= $sp . $arg . $conf['inf'] . "\n"; + if ($conf['typ'] == 'select') + { + $l2 = 0; + foreach($conf['select'] as $opt => $what) + { + $l2 = max($l2, strlen($opt)); + } + $sp2 = $this->cli_get_SP2($l, $l2, $inf); + foreach($conf['select'] as $opt => $what) + { + $inf .= $sp2 . sprintf("%-${l2}s ", $opt) . $what . "\n"; + } + } + } + if (strlen($inf)) + { + if ($required) + { + return $sp . "Required arguments:\n\n" . $inf; + } + else + { + return $sp . "Optional arguments:\n\n". $inf; + } + } + } + function cli_cmd_run_help() { $argv = $this->argv; @@ -262,31 +307,9 @@ Commands: if (isset($funcs['arg'])) { $inf .= "\n"; - foreach(call_user_func(array($this, $funcs['arg'])) as $arg => $conf) - { - if (strlen($arg)) - { - $arg = "-$arg "; - } - else - { - $arg = "... "; - } - $inf .= $sp . $arg . $conf['inf'] . "\n"; - if ($conf['typ'] == 'select') - { - $l2 = 0; - foreach($conf['select'] as $opt => $what) - { - $l2 = max($l2, strlen($opt)); - } - $sp2 = $this->cli_get_SP2($l, $l2, $inf); - foreach($conf['select'] as $opt => $what) - { - $inf .= $sp2 . sprintf("%-${l2}s ", $opt) . $what . "\n"; - } - } - } + $inf .= $this->cli_help_get_args($funcs['arg'], $l, $sp, true); + $inf .= "\n"; + $inf .= $this->cli_help_get_args($funcs['arg'], $l, $sp, false); } } echo "$inf\n\n"; diff --git a/ext/phar/phar/pharcommand.inc b/ext/phar/phar/pharcommand.inc index ed1c1becb5..bd6451e649 100755 --- a/ext/phar/phar/pharcommand.inc +++ b/ext/phar/phar/pharcommand.inc @@ -21,6 +21,31 @@ class PharCommand extends CLICommand return str_repeat(' ', $l1 + 2 + 17); } + static function phar_args($which, $phartyp) + { + $phar_args = array( + 'a' => array('typ'=>'alias', 'val'=>'newphar', 'inf'=>' Provide an alias name for the phar file.'), + 'c' => array('typ'=>'select', 'val'=>NULL, 'inf'=>' Compression algorithmus.', 'select'=>array('gz'=>'GZip compression','gzip'=>'GZip compression','bzip2'=>'BZip2 compression','bz'=>'BZip2 compression','bz2'=>'BZip2 compression','0'=>'No compression','none'=>'No compression')), + 'f' => array('typ'=>$phartyp, 'val'=>NULL, 'inf'=>' Specifies the phar file to work on.'), + 'i' => array('typ'=>'regex', 'val'=>NULL, 'inf'=>' Specifies a regular expression for input files.'), + 's' => array('typ'=>'file', 'val'=>NULL, 'inf'=>' Select the stub file (excluded from list of input files/dirs).'), + 'x' => array('typ'=>'regex', 'val'=>NULL, 'inf'=>' Regular expression for input files to exclude.'), + ); + $args = array(); + foreach($phar_args as $lkey => $cfg) + { + $ukey = strtoupper($lkey); + $required = strpos($which, $ukey) !== false; + $optional = strpos($which, $lkey) !== false; + if ($required || $optional) + { + $args[$lkey] = $cfg; + $args[$lkey]['required'] = $required; + } + } + return $args; + } + static function strEndsWith($haystack, $needle) { return substr($haystack, -strlen($needle)) == $needle; @@ -87,15 +112,9 @@ class PharCommand extends CLICommand static function cli_cmd_arg_pack() { - return array( - 'f' => array('typ'=>'pharnew', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the phar file to work on.'), - 'a' => array('typ'=>'alias', 'val'=>'newphar', 'required'=>1, 'inf'=>' Provide an alias name for the phar file.'), - 's' => array('typ'=>'file', 'val'=>NULL, 'inf'=>' Select the stub file (excluded from list of input files/dirs).'), - 'r' => array('typ'=>'regex', 'val'=>NULL, 'inf'=>' Specifies a regular expression for input files.'), - 'x' => array('typ'=>'regex', 'val'=>NULL, 'inf'=>' Regular expression for input files to exclude.'), - 'c' => array('typ'=>'select', 'val'=>NULL, 'inf'=>' Compression algorithmus.', 'select'=>array('gz'=>'GZip compression','gzip'=>'GZip compression','bzip2'=>'BZip2 compression','bz'=>'BZip2 compression','bz2'=>'BZip2 compression','0'=>'No compression','none'=>'No compression')), - '' => array('typ'=>'any', 'val'=>NULL, 'required'=>1, 'inf'=>' Any number of input files and directories.'), - ); + $args = self::phar_args('AcFrsx', 'pharnew'); + $args[''] = array('typ'=>'any', 'val'=>NULL, 'required'=>1, 'inf'=>' Any number of input files and directories.'); + return $args; } function cli_cmd_run_pack() @@ -114,7 +133,7 @@ class PharCommand extends CLICommand $archive = $this->args['f']['val']; $alias = $this->args['a']['val']; $stub = $this->args['s']['val']; - $regex = $this->args['r']['val']; + $regex = $this->args['i']['val']; $invregex= $this->args['x']['val']; $input = $this->args['']['val']; @@ -193,6 +212,32 @@ class PharCommand extends CLICommand } } + function phar_dir_echo($f) + { + echo "$f\n"; + } + + function phar_dir_operation(RecursiveIteratorIterator $dir, $func) + { + $regex = $this->args['i']['val']; + $invregex= $this->args['x']['val']; + + if (isset($regex)) + { + $dir = new RegexIterator($dir, '/'. $regex . '/'); + } + + if (isset($invregex)) + { + $dir = new InvertedRegexIterator($dir, '/'. $invregex . '/'); + } + + foreach($dir as $f) + { + call_user_func($func, $f); + } + } + static function cli_cmd_inf_list() { return "List contents of a PHAR archive."; @@ -200,17 +245,12 @@ class PharCommand extends CLICommand static function cli_cmd_arg_list() { - return array( - 'f' => array('typ'=>'pharurl', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the phar file to work on.'), - ); + return self::phar_args('Fix', 'pharurl'); } function cli_cmd_run_list() { - foreach(new DirectoryTreeIterator($this->args['f']['val']) as $f) - { - echo "$f\n"; - } + $this->phar_dir_operation(new DirectoryTreeIterator($this->args['f']['val']), array($this, 'phar_dir_echo')); } static function cli_cmd_inf_tree() @@ -220,17 +260,12 @@ class PharCommand extends CLICommand static function cli_cmd_arg_tree() { - return array( - 'f' => array('typ'=>'pharurl', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the phar file to work on.'), - ); + return self::phar_args('Fix', 'pharurl'); } function cli_cmd_run_tree() { - foreach(new DirectoryGraphIterator($this->args['f']['val']) as $f) - { - echo "$f\n"; - } + $this->phar_dir_operation(new DirectoryGraphIterator($this->args['f']['val']), array($this, 'phar_dir_echo')); } static function cli_cmd_inf_extract() @@ -240,10 +275,9 @@ class PharCommand extends CLICommand static function cli_cmd_arg_extract() { - return array( - 'f' => array('typ'=>'phar', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the PHAR file to extract.'), - '' => array('typ'=>'dir', 'val'=>'.', 'inf'=>' Directory to extract to (defaults to \'.\').'), - ); + $args = self::phar_args('Fix', 'phar'); + $args[''] = array('typ'=>'dir', 'val'=>'.', 'inf'=>' Directory to extract to (defaults to \'.\').'); + return $args; } static function cli_cmd_run_extract($args)