1010 */
1111namespace CliArgs ;
1212
13+ use CliArgs \Exception \ConfigErrorException ;
14+
1315class CliArgs
1416{
15- const VERSION = '2.1 .0 ' ;
17+ const VERSION = '3.0 .0 ' ;
1618
1719 const FILTER_BOOL = 'bool ' ;
1820 const FILTER_FLAG = 'flag ' ;
@@ -42,6 +44,7 @@ class CliArgs
4244
4345 /**
4446 * @param array $config
47+ * @throws ConfigErrorException
4548 */
4649 public function __construct (array $ config = null )
4750 {
@@ -50,6 +53,7 @@ public function __construct(array $config = null)
5053
5154 /**
5255 * @param array|null $config
56+ * @throws ConfigErrorException
5357 */
5458 protected function setConfig (array $ config = null )
5559 {
@@ -71,7 +75,7 @@ protected function setConfig(array $config = null)
7175 $ newConfig [$ key ] = [
7276 'key ' => $ key ,
7377 'alias ' => isset ($ cfg ['alias ' ]) ? $ cfg ['alias ' ] : null ,
74- 'default ' => array_key_exists ( 'default ' , $ cfg ) ? $ cfg ['default ' ] : null ,
78+ 'default ' => isset ( $ cfg [ 'default ' ] ) ? $ cfg ['default ' ] : null ,
7579 'help ' => isset ($ cfg ['help ' ]) ? $ cfg ['help ' ] : null ,
7680 'filter ' => isset ($ cfg ['filter ' ]) ? $ cfg ['filter ' ] : null ,
7781 ];
@@ -80,8 +84,14 @@ protected function setConfig(array $config = null)
8084
8185 $ this ->aliases = [];
8286 foreach ($ this ->config as $ key => $ cfg ) {
87+ if (isset ($ this ->aliases [$ key ])) {
88+ throw new ConfigErrorException ("Key or alias ` {$ key }` is already defined " );
89+ }
8390 $ this ->aliases [$ key ] = &$ this ->config [$ key ];
8491 if ($ cfg ['alias ' ]) {
92+ if (isset ($ this ->aliases [$ cfg ['alias ' ]])) {
93+ throw new ConfigErrorException ("Key or alias ` {$ cfg ['alias ' ]}` is already defined " );
94+ }
8595 $ this ->aliases [$ cfg ['alias ' ]] = &$ this ->config [$ key ];
8696 }
8797 }
@@ -101,61 +111,71 @@ public function getArguments()
101111
102112 /**
103113 * Checks if the given key exists in the arguments console list. Returns true if $arg or $alias are exists
104- * @param string $arg
105- * @param string|null $alias
114+ * @param string $key
115+ * @param boolean $checkAlias
106116 * @return bool
107117 */
108- public function isFlagExists ( $ arg , $ alias = null )
118+ public function isFlagExist ( $ key , $ checkAlias = true )
109119 {
110- return array_key_exists ($ arg , $ this ->getArguments ()) || $ alias && array_key_exists ($ alias , $ this ->getArguments ());
120+ $ arguments = $ this ->getArguments ();
121+ if (array_key_exists ($ key , $ arguments )) {
122+ return true ;
123+ }
124+
125+ if ($ checkAlias && ($ alias = $ this ->getAlias ($ key ))) {
126+ return array_key_exists ($ alias , $ arguments );
127+ }
128+
129+ return false ;
111130 }
112131
113132 /**
114- * Checks if the given key (or alias) exists in the arguments console list.
115- * @param string $arg
116- * @return bool
133+ * @param string $key
134+ * @return string|null
117135 */
118- public function isFlagOrAliasExists ( $ arg )
136+ protected function getAlias ( $ key )
119137 {
120- if (!isset ($ this ->aliases [$ arg ])) {
121- return false ;
138+ if (!isset ($ this ->aliases [$ key ])) {
139+ return null ;
122140 }
123- $ key = $ this ->aliases [$ arg ]['key ' ];
124- if (isset ($ this ->aliases [$ arg ]['alias ' ])) {
125- $ alias = $ this ->aliases [$ arg ]['alias ' ];
126- } else {
127- $ alias = null ;
141+ $ cfg = $ this ->aliases [$ key ];
142+ if ($ cfg ['key ' ] === $ key ) {
143+ return $ cfg ['alias ' ];
128144 }
129- return $ this -> isFlagExists ( $ key, $ alias ) ;
145+ return $ cfg [ ' key ' ] ;
130146 }
131147
132148 /**
133149 * Get one param
134- * @param string $arg
150+ * @param string $key
135151 * @return mixed
136152 */
137- public function getArg ($ arg )
153+ public function getArg ($ key )
138154 {
139- if (!$ cfg = $ this ->getArgFromConfig ( $ arg )) {
155+ if (!$ cfg = $ this ->getArgConfig ( $ key )) {
140156 return null ;
141157 }
142- if (array_key_exists ($ arg , $ this ->cache )) {
158+ if (array_key_exists ($ key , $ this ->cache )) {
143159 return $ this ->cache [$ cfg ['key ' ]];
144160 }
145161 $ arguments = $ this ->getArguments ();
146162
147- if ($ this ->isFlagExists ($ cfg ['key ' ])) {
163+ if ($ this ->isFlagExist ($ cfg ['key ' ], false )) {
148164 $ value = $ arguments [$ cfg ['key ' ]];
149- } elseif ($ this ->isFlagExists ($ cfg ['alias ' ])) {
165+ } elseif ($ cfg [ ' alias ' ] && $ this ->isFlagExist ($ cfg ['alias ' ], false )) {
150166 $ value = $ arguments [$ cfg ['alias ' ]];
151167 } elseif (isset ($ cfg ['default ' ])) {
152- return $ cfg ['default ' ];
168+ $ value = $ cfg ['default ' ];
153169 } else {
154- return null ;
170+ $ value = null ;
155171 }
156172
157- if ($ cfg ['filter ' ] && $ cfg ['default ' ] !== $ value || $ cfg ['filter ' ] === self ::FILTER_FLAG ) {
158- $ value = $ this ->filterValue ($ cfg ['filter ' ], $ value , $ cfg ['default ' ] ?: null );
173+ if ($ cfg ['filter ' ]) {
174+ if ($ cfg ['filter ' ] === self ::FILTER_FLAG ) {
175+ $ value = $ this ->isFlagExist ($ key );
176+ } elseif ($ cfg ['default ' ] !== $ value ) {
177+ $ value = $ this ->filterValue ($ cfg ['filter ' ], $ value , $ cfg ['default ' ]);
178+ }
159179 }
160180
161181 $ this ->cache [$ cfg ['key ' ]] = $ value ;
@@ -171,7 +191,7 @@ public function getArgs()
171191 {
172192 $ args = [];
173193 $ arguments = $ this ->getArguments ();
174- foreach ($ arguments as $ key => $ arg ) {
194+ foreach ($ arguments as $ key => $ value ) {
175195 if (!isset ($ this ->aliases [$ key ])) {
176196 continue ;
177197 }
@@ -191,9 +211,6 @@ protected function filterValue($filter, $value, $default = null)
191211 {
192212 if (is_string ($ filter )) {
193213 switch ($ filter ) {
194- case self ::FILTER_FLAG :
195- return true ;
196-
197214 case self ::FILTER_BOOL :
198215 return filter_var ($ value , FILTER_VALIDATE_BOOLEAN );
199216
@@ -218,13 +235,13 @@ protected function filterValue($filter, $value, $default = null)
218235 }
219236
220237 /**
221- * @param $arg
238+ * @param $key
222239 * @return null
223240 */
224- protected function getArgFromConfig ( $ arg )
241+ protected function getArgConfig ( $ key )
225242 {
226- if (isset ($ this ->aliases [$ arg ])) {
227- return $ this ->aliases [$ arg ];
243+ if (isset ($ this ->aliases [$ key ])) {
244+ return $ this ->aliases [$ key ];
228245 }
229246 return null ;
230247 }
0 commit comments