From e03eb6a30b3318e840e74f41bf4f98f180496ceb Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 7 Apr 2009 15:52:04 -0400 Subject: [PATCH] add hasDefault() to the Option class, and let --foo imply foo's default value if it has one --- framework/Argv/lib/Horde/Argv/Option.php | 128 +++++++++++++++++-------------- framework/Argv/lib/Horde/Argv/Parser.php | 31 ++++---- 2 files changed, 88 insertions(+), 71 deletions(-) diff --git a/framework/Argv/lib/Horde/Argv/Option.php b/framework/Argv/lib/Horde/Argv/Option.php index db8d5ec75..1e6e709fd 100644 --- a/framework/Argv/lib/Horde/Argv/Option.php +++ b/framework/Argv/lib/Horde/Argv/Option.php @@ -31,14 +31,14 @@ */ class Horde_Argv_Option { - const SUPPRESS_HELP = "SUPPRESS HELP"; - const SUPPRESS_USAGE = "SUPPRESS USAGE"; + const SUPPRESS_HELP = 'SUPPRESS HELP'; + const SUPPRESS_USAGE = 'SUPPRESS USAGE'; /** * Not supplying a default is different from a default of None, * so we need an explicit "not supplied" value. */ - public static $NO_DEFAULT = array("NO", "DEFAULT"); + public static $NO_DEFAULT = array('NO', 'DEFAULT'); public static function parseNumber($value) { @@ -125,33 +125,39 @@ class Horde_Argv_Option } - # The list of instance attributes that may be set through - # keyword args to the constructor. - public $ATTRS = array('action', - 'type', - 'dest', - 'default', - 'nargs', - 'const', - 'choices', - 'callback', - 'callbackArgs', - 'help', - 'metavar', + /** + * The list of instance attributes that may be set through keyword args to + * the constructor. + */ + public $ATTRS = array( + 'action', + 'type', + 'dest', + 'default', + 'nargs', + 'const', + 'choices', + 'callback', + 'callbackArgs', + 'help', + 'metavar', ); - # The set of actions allowed by option parsers. Explicitly listed - # here so the constructor can validate its arguments. - public $ACTIONS = array("store", - "store_const", - "store_true", - "store_false", - "append", - "append_const", - "count", - "callback", - "help", - "version", + /** + * The set of actions allowed by option parsers. Explicitly listed here so + * the constructor can validate its arguments. + */ + public $ACTIONS = array( + 'store', + 'store_const', + 'store_true', + 'store_false', + 'append', + 'append_const', + 'count', + 'callback', + 'help', + 'version', ); /** @@ -159,38 +165,40 @@ class Horde_Argv_Option * also listed just for constructor argument validation. (If * the action is one of these, there must be a destination.) */ - public $STORE_ACTIONS = array("store", - "store_const", - "store_true", - "store_false", - "append", - "append_const", - "count", + public $STORE_ACTIONS = array( + 'store', + 'store_const', + 'store_true', + 'store_false', + 'append', + 'append_const', + 'count', ); - # The set of actions for which it makes sense to supply a value - # type, ie. which may consume an argument from the command line. - public $TYPED_ACTIONS = array("store", - "append", - "callback", + /** + * The set of actions for which it makes sense to supply a value type, + * ie. which may consume an argument from the command line. + */ + public $TYPED_ACTIONS = array( + 'store', + 'append', + 'callback', ); /** - * The set of actions which *require* a value type, ie. that - * always consume an argument from the command line. + * The set of actions which *require* a value type, ie. that always consume + * an argument from the command line. */ - public $ALWAYS_TYPED_ACTIONS = array("store", - "append", - ); + public $ALWAYS_TYPED_ACTIONS = array('store', 'append'); - # The set of actions which take a 'const' attribute. - public $CONST_ACTIONS = array("store_const", - "append_const", - ); + /** + * The set of actions which take a 'const' attribute. + */ + public $CONST_ACTIONS = array('store_const', 'append_const'); # The set of known types for option parsers. Again, listed here for # constructor argument validation. - public $TYPES = array("string", "int", "long", "float", "complex", "choice"); + public $TYPES = array('string', 'int', 'long', 'float', 'complex', 'choice'); # Dictionary of argument checking functions, which convert and # validate option arguments according to the option type. @@ -472,12 +480,18 @@ class Horde_Argv_Option return !is_null($this->type); } + public function hasDefault() + { + return $this->default !== self::$NO_DEFAULT; + } + public function getOptString() { - if ($this->longOpts) + if ($this->longOpts) { return $this->longOpts[0]; - else + } else { return $this->shortOpts[0]; + } } @@ -522,15 +536,15 @@ class Horde_Argv_Option public function takeAction($action, $dest, $opt, $value, $values, $parser) { - if ($action == 'store') + if ($action == 'store') { $values->$dest = $value; - elseif ($action == 'store_const') + } elseif ($action == 'store_const') { $values->$dest = $this->const; - elseif ($action == 'store_true') + } elseif ($action == 'store_true') { $values->$dest = true; - elseif ($action == 'store_false') + } elseif ($action == 'store_false') { $values->$dest = false; - elseif ($action == 'append') { + } elseif ($action == 'append') { $values->{$dest}[] = $value; } elseif ($action == 'append_const') { $values->{$dest}[] = $this->const; diff --git a/framework/Argv/lib/Horde/Argv/Parser.php b/framework/Argv/lib/Horde/Argv/Parser.php index 1be8b8257..f4dfffe72 100644 --- a/framework/Argv/lib/Horde/Argv/Parser.php +++ b/framework/Argv/lib/Horde/Argv/Parser.php @@ -468,7 +468,7 @@ class Horde_Argv_Parser extends Horde_Argv_OptionContainer return; } if ($this->allowUnknownArgs) { - $option = $this->addOption($opt); + $option = $this->addOption($opt, array('default' => true, 'action' => 'append')); } else { throw $e; } @@ -477,11 +477,13 @@ class Horde_Argv_Parser extends Horde_Argv_OptionContainer if ($option->takesValue()) { $nargs = $option->nargs; if (count($rargs) < $nargs) { - if ($nargs == 1) - $this->parserError(sprintf(_("%s option requires an argument"), $opt)); - else - $this->parserError(sprintf(_("%s option requires %d arguments"), - $opt, $nargs)); + if (!$option->hasDefault()) { + if ($nargs == 1) { + $this->parserError(sprintf(_("%s option requires an argument"), $opt)); + } else { + $this->parserError(sprintf(_("%s option requires %d arguments"), $opt, $nargs)); + } + } } elseif ($nargs == 1) { $value = array_shift($rargs); } else { @@ -511,7 +513,7 @@ class Horde_Argv_Parser extends Horde_Argv_OptionContainer if (!$option) { if ($this->allowUnknownArgs) { - $option = $this->addOption($opt); + $option = $this->addOption($opt, array('default' => true, 'action' => 'append')); } else { throw new Horde_Argv_BadOptionException($opt); } @@ -527,11 +529,13 @@ class Horde_Argv_Parser extends Horde_Argv_OptionContainer $nargs = $option->nargs; if (count($rargs) < $nargs) { - if ($nargs == 1) - $this->parserError(sprintf(_("%s option requires an argument"), $opt)); - else - $this->parserError(sprintf(_("%s option requires %d arguments"), $opt, $nargs)); - + if (!$option->hasDefault()) { + if ($nargs == 1) { + $this->parserError(sprintf(_("%s option requires an argument"), $opt)); + } else { + $this->parserError(sprintf(_("%s option requires %d arguments"), $opt, $nargs)); + } + } } elseif ($nargs == 1) { $value = array_shift($rargs); } else { @@ -545,8 +549,7 @@ class Horde_Argv_Parser extends Horde_Argv_OptionContainer $option->process($opt, $value, $values, $this); - if ($stop) - break; + if ($stop) { break; } } } -- 2.11.0