Development

/branches/1.3/lib/command/sfCommandOptionSet.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/command/sfCommandOptionSet.class.php

Revision 17865, 3.9 kB (checked in by FabianLange, 5 years ago)

[1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Represent a set of command line options.
13  *
14  * @package    symfony
15  * @subpackage command
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfCommandOptionSet
20 {
21   protected
22     $options   = array(),
23     $shortcuts = array();
24
25   /**
26    * Constructor.
27    *
28    * @param array $options An array of sfCommandOption objects
29    */
30   public function __construct($options = array())
31   {
32     $this->setOptions($options);
33   }
34
35   /**
36    * Sets the sfCommandOption objects.
37    *
38    * @param array $options An array of sfCommandOption objects
39    */
40   public function setOptions($options = array())
41   {
42     $this->options = array();
43     $this->shortcuts = array();
44     $this->addOptions($options);
45   }
46
47   /**
48    * Add an array of sfCommandOption objects.
49    *
50    * @param array $options An array of sfCommandOption objects
51    */
52   public function addOptions($options = array())
53   {
54     foreach ($options as $option)
55     {
56       $this->addOption($option);
57     }
58   }
59
60   /**
61    * Add a sfCommandOption objects.
62    *
63    * @param sfCommandOption $option A sfCommandOption object
64    */
65   public function addOption(sfCommandOption $option)
66   {
67     if (isset($this->options[$option->getName()]))
68     {
69       throw new sfCommandException(sprintf('An option named "%s" already exist.', $option->getName()));
70     }
71     else if (isset($this->shortcuts[$option->getShortcut()]))
72     {
73       throw new sfCommandException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
74     }
75
76     $this->options[$option->getName()] = $option;
77     if ($option->getShortcut())
78     {
79       $this->shortcuts[$option->getShortcut()] = $option->getName();
80     }
81   }
82
83   /**
84    * Returns an option by name.
85    *
86    * @param string $name The option name
87    *
88    * @return sfCommandOption A sfCommandOption object
89    */
90   public function getOption($name)
91   {
92     if (!$this->hasOption($name))
93     {
94       throw new sfCommandException(sprintf('The "--%s" option does not exist.', $name));
95     }
96
97     return $this->options[$name];
98   }
99
100   /**
101    * Returns true if an option object exists by name.
102    *
103    * @param string $name The option name
104    *
105    * @return Boolean true if the option object exists, false otherwise
106    */
107   public function hasOption($name)
108   {
109     return isset($this->options[$name]);
110   }
111
112   /**
113    * Gets the array of sfCommandOption objects.
114    *
115    * @return array An array of sfCommandOption objects
116    */
117   public function getOptions()
118   {
119     return $this->options;
120   }
121
122   /**
123    * Returns true if an option object exists by shortcut.
124    *
125    * @param string $name The option shortcut
126    *
127    * @return Boolean true if the option object exists, false otherwise
128    */
129   public function hasShortcut($name)
130   {
131     return isset($this->shortcuts[$name]);
132   }
133
134   /**
135    * Gets an option by shortcut.
136    *
137    * @return sfCommandOption A sfCommandOption object
138    */
139   public function getOptionForShortcut($shortcut)
140   {
141     return $this->getOption($this->shortcutToName($shortcut));
142   }
143
144   /**
145    * Gets an array of default values.
146    *
147    * @return array An array of all default values
148    */
149   public function getDefaults()
150   {
151     $values = array();
152     foreach ($this->options as $option)
153     {
154       $values[$option->getName()] = $option->getDefault();
155     }
156
157     return $values;
158   }
159
160   /**
161    * Returns the option name given a shortcut.
162    *
163    * @param string $shortcut The shortcut
164    *
165    * @return string The option name
166    */
167   protected function shortcutToName($shortcut)
168   {
169     if (!isset($this->shortcuts[$shortcut]))
170     {
171       throw new sfCommandException(sprintf('The "-%s" option does not exist.', $shortcut));
172     }
173
174     return $this->shortcuts[$shortcut];
175   }
176 }
177
Note: See TracBrowser for help on using the browser.