1: <?php
2: /**
3: * ConsoleArgumentOption file
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * For full copyright and license information, please see the LICENSE.txt
12: * Redistributions of files must retain the above copyright notice.
13: *
14: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15: * @link http://cakephp.org CakePHP(tm) Project
16: * @since CakePHP(tm) v 2.0
17: * @license http://www.opensource.org/licenses/mit-license.php MIT License
18: */
19:
20: /**
21: * An object to represent a single argument used in the command line.
22: * ConsoleOptionParser creates these when you use addArgument()
23: *
24: * @see ConsoleOptionParser::addArgument()
25: * @package Cake.Console
26: */
27: class ConsoleInputArgument {
28:
29: /**
30: * Name of the argument.
31: *
32: * @var string
33: */
34: protected $_name;
35:
36: /**
37: * Help string
38: *
39: * @var string
40: */
41: protected $_help;
42:
43: /**
44: * Is this option required?
45: *
46: * @var boolean
47: */
48: protected $_required;
49:
50: /**
51: * An array of valid choices for this argument.
52: *
53: * @var array
54: */
55: protected $_choices;
56:
57: /**
58: * Make a new Input Argument
59: *
60: * @param string|array $name The long name of the option, or an array with all the properties.
61: * @param string $help The help text for this option
62: * @param boolean $required Whether this argument is required. Missing required args will trigger exceptions
63: * @param array $choices Valid choices for this option.
64: */
65: public function __construct($name, $help = '', $required = false, $choices = array()) {
66: if (is_array($name) && isset($name['name'])) {
67: foreach ($name as $key => $value) {
68: $this->{'_' . $key} = $value;
69: }
70: } else {
71: $this->_name = $name;
72: $this->_help = $help;
73: $this->_required = $required;
74: $this->_choices = $choices;
75: }
76: }
77:
78: /**
79: * Get the value of the name attribute.
80: *
81: * @return string Value of this->_name.
82: */
83: public function name() {
84: return $this->_name;
85: }
86:
87: /**
88: * Generate the help for this argument.
89: *
90: * @param integer $width The width to make the name of the option.
91: * @return string
92: */
93: public function help($width = 0) {
94: $name = $this->_name;
95: if (strlen($name) < $width) {
96: $name = str_pad($name, $width, ' ');
97: }
98: $optional = '';
99: if (!$this->isRequired()) {
100: $optional = __d('cake_console', ' <comment>(optional)</comment>');
101: }
102: if (!empty($this->_choices)) {
103: $optional .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
104: }
105: return sprintf('%s%s%s', $name, $this->_help, $optional);
106: }
107:
108: /**
109: * Get the usage value for this argument
110: *
111: * @return string
112: */
113: public function usage() {
114: $name = $this->_name;
115: if (!empty($this->_choices)) {
116: $name = implode('|', $this->_choices);
117: }
118: $name = '<' . $name . '>';
119: if (!$this->isRequired()) {
120: $name = '[' . $name . ']';
121: }
122: return $name;
123: }
124:
125: /**
126: * Check if this argument is a required argument
127: *
128: * @return boolean
129: */
130: public function isRequired() {
131: return (bool)$this->_required;
132: }
133:
134: /**
135: * Check that $value is a valid choice for this argument.
136: *
137: * @param string $value
138: * @return boolean
139: * @throws ConsoleException
140: */
141: public function validChoice($value) {
142: if (empty($this->_choices)) {
143: return true;
144: }
145: if (!in_array($value, $this->_choices)) {
146: throw new ConsoleException(
147: __d('cake_console', '"%s" is not a valid value for %s. Please use one of "%s"',
148: $value, $this->_name, implode(', ', $this->_choices)
149: ));
150: }
151: return true;
152: }
153:
154: /**
155: * Append this arguments XML representation to the passed in SimpleXml object.
156: *
157: * @param SimpleXmlElement $parent The parent element.
158: * @return SimpleXmlElement The parent with this argument appended.
159: */
160: public function xml(SimpleXmlElement $parent) {
161: $option = $parent->addChild('argument');
162: $option->addAttribute('name', $this->_name);
163: $option->addAttribute('help', $this->_help);
164: $option->addAttribute('required', $this->isRequired());
165: $choices = $option->addChild('choices');
166: foreach ($this->_choices as $valid) {
167: $choices->addChild('choice', $valid);
168: }
169: return $parent;
170: }
171:
172: }
173: