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