sfCommandManager.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Class to manage command line arguments and options.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandManager.class.php 16343 2009-03-16 16:48:40Z fabien $
  16. */
  17. class sfCommandManager
  18. {
  19. protected
  20. $arguments = '',
  21. $errors = array(),
  22. $optionSet = null,
  23. $argumentSet = array(),
  24. $optionValues = array(),
  25. $argumentValues = array(),
  26. $parsedArgumentValues = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param sfCommandArgumentSet $argumentSet A sfCommandArgumentSet object
  31. * @param sfCommandOptionSet $optionSet A setOptionSet object
  32. */
  33. public function __construct(sfCommandArgumentSet $argumentSet = null, sfCommandOptionSet $optionSet = null)
  34. {
  35. if (is_null($argumentSet))
  36. {
  37. $argumentSet = new sfCommandArgumentSet();
  38. }
  39. $this->setArgumentSet($argumentSet);
  40. if (is_null($optionSet))
  41. {
  42. $optionSet = new sfCommandOptionSet();
  43. }
  44. $this->setOptionSet($optionSet);
  45. }
  46. /**
  47. * Sets the argument set.
  48. *
  49. * @param sfCommandArgumentSet $argumentSet A sfCommandArgumentSet object
  50. */
  51. public function setArgumentSet(sfCommandArgumentSet $argumentSet)
  52. {
  53. $this->argumentSet = $argumentSet;
  54. }
  55. /**
  56. * Gets the argument set.
  57. *
  58. * @return sfCommandArgumentSet A sfCommandArgumentSet object
  59. */
  60. public function getArgumentSet()
  61. {
  62. return $this->argumentSet;
  63. }
  64. /**
  65. * Sets the option set.
  66. *
  67. * @param sfCommandOptionSet $optionSet A sfCommandOptionSet object
  68. */
  69. public function setOptionSet(sfCommandOptionSet $optionSet)
  70. {
  71. $this->optionSet = $optionSet;
  72. }
  73. /**
  74. * Gets the option set.
  75. *
  76. * @return sfCommandOptionSet A sfCommandOptionSet object
  77. */
  78. public function getOptionSet()
  79. {
  80. return $this->optionSet;
  81. }
  82. /**
  83. * Processes command line arguments.
  84. *
  85. * @param mixed $arguments A string or an array of command line parameters
  86. */
  87. public function process($arguments = null)
  88. {
  89. if (is_null($arguments))
  90. {
  91. $arguments = $_SERVER['argv'];
  92. // we strip command line program
  93. if (isset($arguments[0]) && '-' != $arguments[0][0])
  94. {
  95. array_shift($arguments);
  96. }
  97. }
  98. else if (!is_array($arguments))
  99. {
  100. // hack to split arguments with spaces : --test="with some spaces"
  101. $arguments = preg_replace('/(\'|")(.+?)\\1/e', "str_replace(' ', '=PLACEHOLDER=', '\\2')", $arguments);
  102. $arguments = preg_split('/\s+/', $arguments);
  103. $arguments = str_replace('=PLACEHOLDER=', ' ', $arguments);
  104. }
  105. $this->arguments = $arguments;
  106. $this->optionValues = $this->optionSet->getDefaults();
  107. $this->argumentValues = $this->argumentSet->getDefaults();
  108. $this->parsedArgumentValues = array();
  109. $this->errors = array();
  110. while (!in_array($argument = array_shift($this->arguments), array('', null)))
  111. {
  112. if ('--' == $argument)
  113. {
  114. // stop options parsing
  115. $this->parsedArgumentValues = array_merge($this->parsedArgumentValues, $this->arguments);
  116. break;
  117. }
  118. if ('--' == substr($argument, 0, 2))
  119. {
  120. $this->parseLongOption(substr($argument, 2));
  121. }
  122. else if ('-' == $argument[0])
  123. {
  124. $this->parseShortOption(substr($argument, 1));
  125. }
  126. else
  127. {
  128. $this->parsedArgumentValues[] = $argument;
  129. }
  130. }
  131. $position = 0;
  132. foreach ($this->argumentSet->getArguments() as $argument)
  133. {
  134. if (array_key_exists($position, $this->parsedArgumentValues))
  135. {
  136. if ($argument->isArray())
  137. {
  138. $this->argumentValues[$argument->getName()] = array_slice($this->parsedArgumentValues, $position);
  139. break;
  140. }
  141. else
  142. {
  143. $this->argumentValues[$argument->getName()] = $this->parsedArgumentValues[$position];
  144. }
  145. }
  146. ++$position;
  147. }
  148. $this->arguments = $arguments;
  149. if (count($this->parsedArgumentValues) < $this->argumentSet->getArgumentRequiredCount())
  150. {
  151. $this->errors[] = 'Not enough arguments.';
  152. }
  153. else if (count($this->parsedArgumentValues) > $this->argumentSet->getArgumentCount())
  154. {
  155. $this->errors[] = sprintf('Too many arguments ("%s" given).', implode(' ', $this->parsedArgumentValues));
  156. }
  157. }
  158. /**
  159. * Returns true if the current command line options validate the argument and option sets.
  160. *
  161. * @return true if there are some validation errors, false otherwise
  162. */
  163. public function isValid()
  164. {
  165. return count($this->errors) ? false : true;
  166. }
  167. /**
  168. * Gets the current errors.
  169. *
  170. * @return array An array of errors
  171. */
  172. public function getErrors()
  173. {
  174. return $this->errors;
  175. }
  176. /**
  177. * Returns the argument values.
  178. *
  179. * @return array An array of argument values
  180. */
  181. public function getArgumentValues()
  182. {
  183. return $this->argumentValues;
  184. }
  185. /**
  186. * Returns the argument value for a given argument name.
  187. *
  188. * @param string $name The argument name
  189. *
  190. * @return mixed The argument value
  191. */
  192. public function getArgumentValue($name)
  193. {
  194. if (!$this->argumentSet->hasArgument($name))
  195. {
  196. throw new sfCommandException(sprintf('The "%s" argument does not exist.', $name));
  197. }
  198. return $this->argumentValues[$name];
  199. }
  200. /**
  201. * Returns the options values.
  202. *
  203. * @return array An array of option values
  204. */
  205. public function getOptionValues()
  206. {
  207. return $this->optionValues;
  208. }
  209. /**
  210. * Returns the option value for a given option name.
  211. *
  212. * @param string $name The option name
  213. *
  214. * @return mixed The option value
  215. */
  216. public function getOptionValue($name)
  217. {
  218. if (!$this->optionSet->hasOption($name))
  219. {
  220. throw new sfCommandException(sprintf('The "%s" option does not exist.', $name));
  221. }
  222. return $this->optionValues[$name];
  223. }
  224. /**
  225. * Parses a short option.
  226. *
  227. * @param string $argument The option argument
  228. */
  229. protected function parseShortOption($argument)
  230. {
  231. // short option can be aggregated like in -vd (== -v -d)
  232. for ($i = 0, $count = strlen($argument); $i < $count; $i++)
  233. {
  234. $shortcut = $argument[$i];
  235. $value = true;
  236. if (!$this->optionSet->hasShortcut($shortcut))
  237. {
  238. $this->errors[] = sprintf('The option "-%s" does not exist.', $shortcut);
  239. continue;
  240. }
  241. $option = $this->optionSet->getOptionForShortcut($shortcut);
  242. // required argument?
  243. if ($option->isParameterRequired())
  244. {
  245. if ($i + 1 < strlen($argument))
  246. {
  247. $value = substr($argument, $i + 1);
  248. $this->setOption($option, $value);
  249. break;
  250. }
  251. else
  252. {
  253. // take next element as argument (if it doesn't start with a -)
  254. if (count($this->arguments) && $this->arguments[0][0] != '-')
  255. {
  256. $value = array_shift($this->arguments);
  257. $this->setOption($option, $value);
  258. break;
  259. }
  260. else
  261. {
  262. $this->errors[] = sprintf('Option "-%s" requires an argument', $shortcut);
  263. $value = null;
  264. }
  265. }
  266. }
  267. else if ($option->isParameterOptional())
  268. {
  269. if (substr($argument, $i + 1) != '')
  270. {
  271. $value = substr($argument, $i + 1);
  272. }
  273. else
  274. {
  275. // take next element as argument (if it doesn't start with a -)
  276. if (count($this->arguments) && $this->arguments[0][0] != '-')
  277. {
  278. $value = array_shift($this->arguments);
  279. }
  280. else
  281. {
  282. $value = $option->getDefault();
  283. }
  284. }
  285. $this->setOption($option, $value);
  286. break;
  287. }
  288. $this->setOption($option, $value);
  289. }
  290. }
  291. /**
  292. * Parses a long option.
  293. *
  294. * @param string $argument The option argument
  295. */
  296. protected function parseLongOption($argument)
  297. {
  298. if (false !== strpos($argument, '='))
  299. {
  300. list($name, $value) = explode('=', $argument, 2);
  301. if (!$this->optionSet->hasOption($name))
  302. {
  303. $this->errors[] = sprintf('The "--%s" option does not exist.', $name);
  304. return;
  305. }
  306. $option = $this->optionSet->getOption($name);
  307. if (!$option->acceptParameter())
  308. {
  309. $this->errors[] = sprintf('Option "--%s" does not take an argument.', $name);
  310. $value = true;
  311. }
  312. }
  313. else
  314. {
  315. $name = $argument;
  316. if (!$this->optionSet->hasOption($name))
  317. {
  318. $this->errors[] = sprintf('The "--%s" option does not exist.', $name);
  319. return;
  320. }
  321. $option = $this->optionSet->getOption($name);
  322. if ($option->isParameterRequired())
  323. {
  324. $this->errors[] = sprintf('Option "--%s" requires an argument.', $name);
  325. }
  326. $value = $option->acceptParameter() ? $option->getDefault() : true;
  327. }
  328. $this->setOption($option, $value);
  329. }
  330. public function setOption(sfCommandOption $option, $value)
  331. {
  332. if ($option->isArray())
  333. {
  334. $this->optionValues[$option->getName()][] = $value;
  335. }
  336. else
  337. {
  338. $this->optionValues[$option->getName()] = $value;
  339. }
  340. }
  341. }