11<?php
22/**
3- * This file is part of RedisClient .
3+ * This file is part of CliArgs .
44 * git: https://github.com/cheprasov/php-cli-args
55 *
66 * (C) Alexander Cheprasov <cheprasov.84@ya.ru>
1212
1313class CliArgs
1414{
15- const FILTER_INT = 'int ' ;
16- const FILTER_FLOAT = ' float ' ;
15+ const VERSION = '1.0.0 ' ;
16+
1717 const FILTER_BOOL = 'bool ' ;
18+ const FILTER_FLAG = 'flag ' ;
19+ const FILTER_FLOAT = 'float ' ;
20+ const FILTER_INT = 'int ' ;
1821 const FILTER_JSON = 'json ' ;
1922 const FILTER_HELP = 'help ' ;
2023
@@ -49,7 +52,7 @@ public function __construct(array $config = null)
4952 /**
5053 * @param array|null $config
5154 */
52- public function setConfig (array $ config = null )
55+ protected function setConfig (array $ config = null )
5356 {
5457 $ this ->config = $ config ;
5558 $ this ->cache = [];
@@ -60,11 +63,24 @@ public function setConfig(array $config = null)
6063 $ this ->aliases = [];
6164 foreach ($ config as $ key => $ cfg ) {
6265 $ this ->aliases [$ key ] = &$ config [$ key ];
63- if (isset ($ cfg ['short ' ])) {
64- $ this ->aliases [$ cfg ['short ' ]] = &$ config [$ key ];
66+ $ config [$ key ]['key ' ] = $ key ;
67+
68+ if (isset ($ cfg ['alias ' ])) {
69+ $ this ->aliases [$ cfg ['alias ' ]] = &$ config [$ key ];
70+ } else {
71+ $ config [$ key ]['alias ' ] = null ;
72+ }
73+ if (!array_key_exists ('default ' , $ cfg )) {
74+ $ config [$ key ]['default ' ] = null ;
75+ }
76+ if (!isset ($ cfg ['help ' ])) {
77+ $ config [$ key ]['help ' ] = null ;
78+ }
79+ if (!isset ($ cfg ['filter ' ])) {
80+ $ config [$ key ]['filter ' ] = null ;
6581 }
66- $ config [$ key ]['long ' ] = $ key ;
6782 }
83+ $ this ->config = $ config ;
6884 }
6985
7086 /**
@@ -78,6 +94,16 @@ public function getArguments()
7894 return $ this ->arguments ;
7995 }
8096
97+ /**
98+ * @param string $arg
99+ * @param string|null $alias
100+ * @return bool
101+ */
102+ public function isFlagExists ($ arg , $ alias = null )
103+ {
104+ return array_key_exists ($ arg , $ this ->getArguments ()) || $ alias && array_key_exists ($ alias , $ this ->getArguments ());
105+ }
106+
81107 /**
82108 * @param string $arg
83109 * @return mixed
@@ -88,34 +114,49 @@ public function getArg($arg)
88114 return null ;
89115 }
90116 if (array_key_exists ($ arg , $ this ->cache )) {
91- return $ this ->cache [$ arg ];
117+ return $ this ->cache [$ cfg [ ' key ' ] ];
92118 }
93119 $ arguments = $ this ->getArguments ();
94120
95- if (isset ( $ cfg [ ' long ' ]) && isset ( $ arguments [ $ cfg ['long ' ] ])) {
96- $ value = $ arguments [$ cfg ['long ' ]];
97- } elseif (isset ( $ cfg [ ' short ' ]) && isset ( $ arguments [ $ cfg ['short ' ] ])) {
98- $ value = $ arguments [$ cfg ['short ' ]];
99- } elseif (isset ( $ cfg ['default ' ]) ) {
121+ if ($ this -> isFlagExists ( $ cfg ['key ' ])) {
122+ $ value = $ arguments [$ cfg ['key ' ]];
123+ } elseif ($ this -> isFlagExists ( $ cfg ['alias ' ])) {
124+ $ value = $ arguments [$ cfg ['alias ' ]];
125+ } elseif ($ cfg ['default ' ]) {
100126 return $ cfg ['default ' ];
101127 } else {
102128 return null ;
103129 }
104130
105- if (isset ($ cfg ['filter ' ])) {
106- $ value = $ this ->filterValue ($ cfg ['filter ' ], $ value , isset ($ cfg ['default ' ]) ? $ cfg ['default ' ] : null );
131+ if ($ cfg ['filter ' ] && $ cfg ['default ' ] !== $ value
132+ || $ cfg ['filter ' ] === self ::FILTER_FLAG
133+ || $ cfg ['filter ' ] === self ::FILTER_HELP
134+ ) {
135+ $ value = $ this ->filterValue ($ cfg ['filter ' ], $ value , $ cfg ['default ' ] ?: null );
107136 }
108137
109- if (isset ($ cfg ['long ' ])) {
110- $ this ->cache [$ cfg ['long ' ]] = $ value ;
111- }
112- if (isset ($ cfg ['short ' ])) {
113- $ this ->cache [$ cfg ['short ' ]] = $ value ;
114- }
138+ $ this ->cache [$ cfg ['key ' ]] = $ value ;
115139
116140 return $ value ;
117141 }
118142
143+ /**
144+ * @return mixed[]
145+ */
146+ public function getArgs ()
147+ {
148+ $ args = [];
149+ $ arguments = $ this ->getArguments ();
150+ foreach ($ arguments as $ key => $ arg ) {
151+ if (!isset ($ this ->aliases [$ key ])) {
152+ continue ;
153+ }
154+ $ args [$ key ] = $ this ->getArg ($ key );
155+ }
156+
157+ return $ args ;
158+ }
159+
119160 /**
120161 * @param mixed $filter
121162 * @param mixed $value
@@ -126,26 +167,23 @@ protected function filterValue($filter, $value, $default = null)
126167 {
127168 if (is_string ($ filter )) {
128169 switch ($ filter ) {
129- case self ::FILTER_BOOL :
170+ case self ::FILTER_FLAG :
171+ return true ;
172+
173+ case self ::FILTER_BOOL :
130174 return filter_var ($ value , FILTER_VALIDATE_BOOLEAN );
131175
132- case self ::FILTER_INT :
176+ case self ::FILTER_INT :
133177 return (int )$ value ;
134178
135- case self ::FILTER_FLOAT :
179+ case self ::FILTER_FLOAT :
136180 return (float )$ value ;
137181
138- case self ::FILTER_JSON :
182+ case self ::FILTER_JSON :
139183 return json_decode ($ value , true );
140184
141185 case self ::FILTER_HELP :
142186 return $ this ->getHelp ($ value );
143-
144- default :
145- if (preg_match ($ filter , $ value )
146- && preg_last_error () == PREG_NO_ERROR ) {
147- return $ value ;
148- }
149187 }
150188 return $ default ;
151189 }
@@ -174,16 +212,30 @@ protected function getArgFromConfig($arg)
174212 * @param mixed $value
175213 * @return string mixed
176214 */
177- protected function getHelp ($ value )
215+ protected function getHelp ($ value = null )
178216 {
179- $ lines = [];
180- foreach ($ this ->config as $ key => $ cfg ) {
181- $ lines [] = [
182- '-- ' . $ key . (isset ($ cfg ['short ' ]) ? ' or - ' . $ cfg ['short ' ] : '' ),
183- isset ($ cfg ['help ' ]) ? $ cfg ['help ' ] : '' ,
184- ];
217+ $ breakTitle = PHP_EOL . str_repeat (' ' , 4 );
218+ $ breakInfo = PHP_EOL . str_repeat (' ' , 8 );
219+ $ help = [];
220+ foreach ($ this ->config as $ cfg ) {
221+ if ($ value && ($ cfg ['key ' ] !== $ value && (!$ cfg ['alias ' ] || $ cfg ['alias ' ] !== $ value ))) {
222+ continue ;
223+ }
224+ $ title = [];
225+ if ($ cfg ['key ' ]) {
226+ $ title [] = (1 === strlen ($ cfg ['key ' ]) ? '- ' : '-- ' ) . $ cfg ['key ' ];
227+ }
228+ if ($ cfg ['alias ' ]) {
229+ $ title [] = (1 === strlen ($ cfg ['alias ' ]) ? '- ' : '-- ' ) . $ cfg ['alias ' ];
230+ }
231+ $ line = implode (' ' , $ title );
232+ if ($ cfg ['help ' ]) {
233+ $ line .= $ breakInfo . wordwrap ($ cfg ['help ' ], 75 , $ breakInfo );
234+ }
235+ $ help [] = $ breakTitle . $ line ;
185236 }
186- return $ value ;
237+
238+ return 'HELP: ' . PHP_EOL . (implode (PHP_EOL , $ help ) ?: 'Key is not found ' ) . PHP_EOL ;
187239 }
188240
189241 /**
0 commit comments