@@ -54,51 +54,32 @@ public function __construct($parseArgv = true, $fillToGlobal = false)
5454
5555 /**
5656 * 读取输入信息
57- * @param string $message 若不为空,则先输出文本消息
57+ * @param string $question 若不为空,则先输出文本消息
5858 * @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
5959 * @return string
6060 */
61- public function read ($ message = null , $ nl = false )
61+ public function read ($ question = null , $ nl = false )
6262 {
63- fwrite (STDOUT , $ message . ($ nl ? "\n" : '' ));
63+ fwrite (STDOUT , $ question . ($ nl ? "\n" : '' ));
6464
6565 return trim (fgets ($ this ->inputStream ));
6666 }
6767
68- /**
69- * @return string
70- */
71- public function getScriptName ()
72- {
73- return self ::$ scriptName ;
74- }
68+ /////////////////////////////////////////////////////////////////////////////////////////
69+ /// arguments (eg: name=john city=chengdu)
70+ /////////////////////////////////////////////////////////////////////////////////////////
7571
7672 /**
77- * @return string
78- */
79- public function getScript ()
80- {
81- return self ::$ scriptName ;
82- }
83-
84- /**
85- * @return string
73+ * @param $name
74+ * @return bool
8675 */
87- public function getCommand ( )
76+ public function hasArg ( $ name )
8877 {
89- return self :: $ command ;
78+ return isset ( $ this -> args [ $ name ]) ;
9079 }
9180
9281 /**
93- * @return array
94- */
95- public function getArgs ()
96- {
97- return $ this ->args ;
98- }
99-
100- /**
101- * @param null|string $name
82+ * @param null|int|string $name
10283 * @param mixed $default
10384 * @return mixed
10485 */
@@ -119,6 +100,24 @@ public function get($name=null, $default = null)
119100 return isset ($ this ->args [$ name ]) ? $ this ->args [$ name ] : $ default ;
120101 }
121102
103+ /**
104+ * get first argument
105+ * @return string
106+ */
107+ public function getFirstArg ()
108+ {
109+ return $ this ->get (0 );
110+ }
111+
112+ /**
113+ * get second argument
114+ * @return string
115+ */
116+ public function getSecondArg ()
117+ {
118+ return $ this ->get (1 );
119+ }
120+
122121 /**
123122 * @param $key
124123 * @param int $default
@@ -132,13 +131,26 @@ public function getInt($key, $default = 0)
132131 }
133132
134133 /**
135- * @return array
134+ * get bool value form args
135+ * @param $key
136+ * @param bool $default
137+ * @return bool
136138 */
137- public function getOpts ( )
139+ public function getBool ( $ key , $ default = false )
138140 {
139- return $ this ->opts ;
141+ if ( !$ this ->hasArg ($ key ) ) {
142+ return (bool )$ default ;
143+ }
144+
145+ $ value = $ this ->args [$ key ];
146+
147+ return !in_array (strtolower ($ value ), ['0 ' , 'false ' ], true );
140148 }
141149
150+ /////////////////////////////////////////////////////////////////////////////////////////
151+ /// options (eg: -d --help)
152+ /////////////////////////////////////////////////////////////////////////////////////////
153+
142154 /**
143155 * @param $name
144156 * @param null $default
@@ -185,7 +197,7 @@ public function hasOpt($name)
185197 * @param bool $default
186198 * @return bool
187199 */
188- public function getBool ($ key , $ default = false )
200+ public function boolOpt ($ key , $ default = false )
189201 {
190202 return $ this ->getBoolOpt ($ key , $ default );
191203 }
@@ -200,6 +212,66 @@ public function getBoolOpt($key, $default = false)
200212 return !in_array (strtolower ($ value ), ['0 ' , 'false ' ], true );
201213 }
202214
215+ /////////////////////////////////////////////////////////////////////////////////////////
216+ /// getter/setter
217+ /////////////////////////////////////////////////////////////////////////////////////////
218+
219+ /**
220+ * @return string
221+ */
222+ public function getScriptName ()
223+ {
224+ return self ::$ scriptName ;
225+ }
226+
227+ /**
228+ * @return string
229+ */
230+ public function getScript ()
231+ {
232+ return self ::$ scriptName ;
233+ }
234+
235+ /**
236+ * @return string
237+ */
238+ public function getCommand ()
239+ {
240+ return self ::$ command ;
241+ }
242+
243+ /**
244+ * @param array $args
245+ */
246+ public function setArgs (array $ args )
247+ {
248+ $ this ->args = $ args ;
249+ }
250+
251+ /**
252+ * @return array
253+ */
254+ public function getArgs ()
255+ {
256+ return $ this ->args ;
257+ }
258+
259+ /**
260+ * @param array $opts
261+ */
262+ public function setOpts (array $ opts )
263+ {
264+ $ this ->opts = $ opts ;
265+ }
266+
267+ /**
268+ * @return array
269+ */
270+ public function getOpts ()
271+ {
272+ return $ this ->opts ;
273+ }
274+
203275 /**
204276 * @return resource
205277 */
0 commit comments