/usr/share/php/Horde/Argv/OptionContainer.php is in php-horde-argv 2.0.12-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | <?php
/**
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Mike Naberezny <mike@maintainable.com>
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Argv
*/
/**
* Abstract base class.
*
* Class attributes:
* standardOptionList : [Option]
* list of standard options that will be accepted by all instances
* of this parser class (intended to be overridden by subclasses).
*
* Instance attributes:
* optionList : [Option]
* the list of Option objects contained by this OptionContainer
* shortOpt : { string : Option }
* dictionary mapping short option strings, eg. "-f" or "-X",
* to the Option instances that implement them. If an Option
* has multiple short option strings, it will appears in this
* dictionary multiple times. [1]
* longOpt : { string : Option }
* dictionary mapping long option strings, eg. "--file" or
* "--exclude", to the Option instances that implement them.
* Again, a given Option can occur multiple times in this
* dictionary. [1]
* defaults : { string : any }
* dictionary mapping option destination names to default
* values for each destination [1]
*
* [1] These mappings are common to (shared by) all components of the
* controlling Horde_Argv_Parser, where they are initially created.
*
* @category Horde
* @package Argv
*/
class Horde_Argv_OptionContainer
{
public $description = '';
public $optionList = array();
public $optionClass = 'Horde_Argv_Option';
public $defaults = array();
public $shortOpt = array();
public $longOpt = array();
public $conflictHandler;
/**
* Initialize the option list and related data structures.
* This method must be provided by subclasses, and it must
* initialize at least the following instance attributes:
* optionList, shortOpt, longOpt, defaults.
*/
public function __construct($optionClass, $conflictHandler, $description)
{
$this->_createOptionList();
$this->optionClass = $optionClass;
$this->setConflictHandler($conflictHandler);
$this->setDescription($description);
}
/**
* For use by Horde_Argv_Parser constructor -- create the master
* option mappings used by this Horde_Argv_Parser and all
* OptionGroups that it owns.
*/
protected function _createOptionMappings()
{
$this->shortOpt = array(); // single letter -> Option instance
$this->longOpt = array(); // long option -> Option instance
$this->defaults = array(); // maps option dest -> default value
}
/**
* For use by OptionGroup constructor -- use shared option
* mappings from the Horde_Argv_Parser that owns this OptionGroup.
*/
protected function _shareOptionMappings($parser)
{
$this->shortOpt =& $parser->shortOpt;
$this->longOpt =& $parser->longOpt;
$this->defaults = $parser->defaults;
}
public function setConflictHandler($handler)
{
if (!in_array($handler, array('error', 'resolve'))) {
throw new InvalidArgumentException('invalid conflictHandler ' . var_export($handler, true));
}
$this->conflictHandler = $handler;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getDescription()
{
return $this->description;
}
// -- Option-adding methods -----------------------------------------
protected function _checkConflict($option)
{
$conflictOpts = array();
foreach ($option->shortOpts as $opt) {
if (isset($this->shortOpt[$opt])) {
$conflictOpts[$opt] = $this->shortOpt[$opt];
}
}
foreach ($option->longOpts as $opt) {
if (isset($this->longOpt[$opt])) {
$conflictOpts[$opt] = $this->longOpt[$opt];
}
}
if ($conflictOpts) {
$handler = $this->conflictHandler;
if ($handler == 'error') {
throw new Horde_Argv_OptionConflictException(sprintf(
'conflicting option string(s): %s',
implode(', ', array_keys($conflictOpts))), $option);
} elseif ($handler == 'resolve') {
foreach ($conflictOpts as $opt => $c_option) {
if (strncmp($opt, '--', 2) === 0) {
$key = array_search($opt, $c_option->longOpts);
if ($key !== false) {
unset($c_option->longOpts[$key]);
}
unset($this->longOpt[$opt]);
} else {
$key = array_search($opt, $c_option->shortOpts);
if ($key !== false) {
unset($c_option->shortOpts[$key]);
}
unset($this->shortOpt[$opt]);
}
if (! ($c_option->shortOpts || $c_option->longOpts)) {
$key = array_search($c_option, $c_option->container->optionList);
unset($c_option->container->optionList[$key]);
}
}
}
}
}
public function addOption()
{
$opts = func_get_args();
if (count($opts) && is_string($opts[0])) {
$optionFactory = new ReflectionClass($this->optionClass);
$option = $optionFactory->newInstanceArgs($opts);
} elseif (count($opts) == 1) {
$option = $opts[0];
if (!$option instanceof Horde_Argv_Option)
throw new InvalidArgumentException('not an Option instance: ' . var_export($option, true));
} else {
throw new InvalidArgumentException('invalid arguments');
}
$this->_checkConflict($option);
$this->optionList[] = $option;
$option->container = $this;
foreach ($option->shortOpts as $opt) {
$this->shortOpt[$opt] = $option;
}
foreach ($option->longOpts as $opt) {
$this->longOpt[$opt] = $option;
}
if (!is_null($option->dest)) {
// option has a dest, we need a default
if ($option->default !== Horde_Argv_Option::$NO_DEFAULT) {
$this->defaults[$option->dest] = $option->default;
} elseif (!isset($this->defaults[$option->dest])) {
$this->defaults[$option->dest] = null;
}
}
return $option;
}
public function addOptions($optionList)
{
foreach ($optionList as $option) {
$this->addOption($option);
}
}
// -- Option query/removal methods ----------------------------------
public function getOption($opt_str)
{
if (isset($this->shortOpt[$opt_str])) {
return $this->shortOpt[$opt_str];
} elseif (isset($this->longOpt[$opt_str])) {
return $this->longOpt[$opt_str];
} else {
return null;
}
}
public function hasOption($opt_str)
{
return isset($this->shortOpt[$opt_str])
|| isset($this->longOpt[$opt_str]);
}
public function removeOption($opt_str)
{
$option = $this->getOption($opt_str);
if (is_null($option)) {
throw new InvalidArgumentException("no such option '$opt_str'");
}
foreach ($option->shortOpts as $opt) {
unset($this->shortOpt[$opt]);
}
foreach ($option->longOpts as $opt) {
unset($this->longOpt[$opt]);
}
$key = array_search($option, $option->container->optionList);
unset($option->container->optionList[$key]);
}
// -- Help-formatting methods ---------------------------------------
public function formatOptionHelp($formatter = null)
{
if (!$this->optionList)
return '';
$result = array();
foreach ($this->optionList as $option) {
if ($option->help != Horde_Argv_Option::SUPPRESS_HELP)
$result[] = $formatter->formatOption($option);
}
return implode('', $result);
}
public function formatDescription($formatter = null)
{
return $formatter->formatDescription($this->getDescription());
}
public function formatHelp($formatter = null)
{
$result = array();
if ($this->description)
$result[] = $this->formatDescription($formatter);
if ($this->optionList)
$result[] = $this->formatOptionHelp($formatter);
return implode("\n", $result);
}
}
|