5 use BadMethodCallException;
8 * An argument for a CLI app. This specifies the argument, what values it expects and
9 * how it's treated during validation.
11 * By default, the argument will be assumed to be an optional letter flag with no value following.
13 * If the argument may receive a value, call mayHaveValue(). If there's whitespace after the
14 * flag, the value will be returned as true instead of the string.
16 * If the argument MUST be accompanied by a value, call mustHaveValue(). In this case, whitespace
17 * is permitted between the flag and its value.
19 * Use assertFile() or assertDir() to indicate that the argument must return a string value
20 * specifying a file or directory. During validation, the value will be resolved to a
21 * full file/dir path (not necessarily existing!) and the original value will be accessible
22 * via a "*.raw" key. E.g. $cli->values['f.raw']
24 * Use assertReadable()/assertWritable() to cause the validator to test the file/dir for
25 * read/write permissions respectively.
27 * @method \MrClay\Cli\Arg mayHaveValue() Assert that the argument, if present, may receive a string value
28 * @method \MrClay\Cli\Arg mustHaveValue() Assert that the argument, if present, must receive a string value
29 * @method \MrClay\Cli\Arg assertFile() Assert that the argument's value must specify a file
30 * @method \MrClay\Cli\Arg assertDir() Assert that the argument's value must specify a directory
31 * @method \MrClay\Cli\Arg assertReadable() Assert that the specified file/dir must be readable
32 * @method \MrClay\Cli\Arg assertWritable() Assert that the specified file/dir must be writable
34 * @property-read bool mayHaveValue
35 * @property-read bool mustHaveValue
36 * @property-read bool assertFile
37 * @property-read bool assertDir
38 * @property-read bool assertReadable
39 * @property-read bool assertWritable
40 * @property-read bool useAsInfile
41 * @property-read bool useAsOutfile
43 * @author Steve Clay <steve@mrclay.org>
44 * @license http://www.opensource.org/licenses/mit-license.php MIT License
50 public function getDefaultSpec()
53 'mayHaveValue' => false,
54 'mustHaveValue' => false,
55 'assertFile' => false,
57 'assertReadable' => false,
58 'assertWritable' => false,
59 'useAsInfile' => false,
60 'useAsOutfile' => false,
67 protected $spec = array();
72 protected $required = false;
77 protected $description = '';
80 * @param bool $isRequired
82 public function __construct($isRequired = false)
84 $this->spec = $this->getDefaultSpec();
85 $this->required = (bool) $isRequired;
87 $this->spec['mustHaveValue'] = true;
92 * Assert that the argument's value points to a writable file. When
93 * Cli::openOutput() is called, a write pointer to this file will
97 public function useAsOutfile()
99 $this->spec['useAsOutfile'] = true;
100 return $this->assertFile()->assertWritable();
104 * Assert that the argument's value points to a readable file. When
105 * Cli::openInput() is called, a read pointer to this file will
109 public function useAsInfile()
111 $this->spec['useAsInfile'] = true;
112 return $this->assertFile()->assertReadable();
118 public function getSpec()
124 * @param string $desc
127 public function setDescription($desc)
129 $this->description = $desc;
136 public function getDescription()
138 return $this->description;
144 public function isRequired()
146 return $this->required;
150 * Note: magic methods declared in class PHPDOC
152 * @param string $name
155 * @throws BadMethodCallException
157 public function __call($name, array $args = array())
159 if (array_key_exists($name, $this->spec)) {
160 $this->spec[$name] = true;
161 if ($name === 'assertFile' || $name === 'assertDir') {
162 $this->spec['mustHaveValue'] = true;
165 throw new BadMethodCallException('Method does not exist');
171 * Note: magic properties declared in class PHPDOC
173 * @param string $name
176 public function __get($name)
178 if (array_key_exists($name, $this->spec)) {
179 return $this->spec[$name];