@@ -51,7 +51,7 @@ class LiteApp
5151 /**
5252 * @param bool $exit
5353 */
54- public function dispatch ($ exit = true )
54+ public function run ($ exit = true )
5555 {
5656 $ this ->parseCliArgv ();
5757
@@ -60,6 +60,14 @@ public function dispatch($exit = true)
6060 unset($ this ->args [0 ]);
6161 }
6262
63+ $ this ->dispatch ($ exit );
64+ }
65+
66+ /**
67+ * @param bool $exit
68+ */
69+ public function dispatch ($ exit = true )
70+ {
6371 if (!$ command = $ this ->command ) {
6472 $ this ->showCommands ();
6573 }
@@ -73,22 +81,22 @@ public function dispatch($exit = true)
7381 $ this ->showCommands ("The command {$ command } not exists! " );
7482 }
7583 } catch (\Throwable $ e ) {
76- $ text = sprintf (
77- "Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" ,
78- $ e ->getCode (),
79- $ e ->getMessage (),
80- $ e ->getFile (),
81- $ e ->getLine (),
82- $ e ->getTraceAsString ()
83- );
84- exit ($ text );
84+ $ status = $ this ->handleException ($ e );
8585 }
8686
8787 if ($ exit ) {
88- exit (( int ) $ status );
88+ $ this -> stop ( $ status );
8989 }
9090 }
9191
92+ /**
93+ * @param int $code
94+ */
95+ public function stop ($ code = 0 )
96+ {
97+ exit ((int )$ code );
98+ }
99+
92100 /**
93101 * @param $command
94102 * @param $handler
@@ -120,6 +128,26 @@ public function runHandler($command, $handler)
120128 throw new \InvalidArgumentException ("Invalid handler of the command: $ command " );
121129 }
122130
131+ /**
132+ * @param \Throwable $e
133+ * @return int
134+ */
135+ protected function handleException (\Throwable $ e )
136+ {
137+ $ code = $ e ->getCode () !== 0 ? $ e ->getCode () : 133 ;
138+
139+ printf (
140+ "Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" ,
141+ $ code ,
142+ $ e ->getMessage (),
143+ $ e ->getFile (),
144+ $ e ->getLine (),
145+ $ e ->getTraceAsString ()
146+ );
147+
148+ return $ code ;
149+ }
150+
123151 /**
124152 * parseCliArgv
125153 */
@@ -158,18 +186,40 @@ public function parseCliArgv()
158186 /**
159187 * @param string $command
160188 * @param string|\Closure $handler
161- * @param string $desc
189+ * @param string $description
162190 */
163- public function addCommand ($ command , $ handler , $ desc = '' )
191+ public function addCommand ($ command , $ handler , $ description = '' )
164192 {
165193 if (!$ command || !$ handler ) {
166194 throw new \InvalidArgumentException ('Invalid arguments ' );
167195 }
168196
169197 $ this ->commands [$ command ] = $ handler ;
170- $ this ->messages [$ command ] = trim ($ desc );
198+ $ this ->messages [$ command ] = trim ($ description );
199+ }
200+
201+ /**
202+ * @param array $commands
203+ */
204+ public function commands (array $ commands )
205+ {
206+ foreach ($ commands as $ command => $ handler ) {
207+ $ des = '' ;
208+
209+ if (is_array ($ handler )) {
210+ $ conf = array_values ($ handler );
211+ $ handler = $ conf [0 ];
212+ $ des = $ conf [1 ] ?? '' ;
213+ }
214+
215+ $ this ->addCommand ($ command , $ handler , $ des );
216+ }
171217 }
172218
219+ ///////////////////////////////////////////////////////////////////////////////////
220+ /// helper methods
221+ ///////////////////////////////////////////////////////////////////////////////////
222+
173223 /**
174224 * @param string $err
175225 */
@@ -179,10 +229,11 @@ public function showCommands($err = '')
179229 echo "ERROR: $ err \n\n" ;
180230 }
181231
182- $ help = "Available Commands: \n" ;
232+ $ commandWidth = 12 ;
233+ $ help = "Welcome to the Lite Console Application. \n\nAvailable Commands: \n" ;
183234
184235 foreach ($ this ->messages as $ command => $ desc ) {
185- $ command = str_pad ($ command , 18 , ' ' );
236+ $ command = str_pad ($ command , $ commandWidth , ' ' );
186237 $ desc = $ desc ?: 'No description for the command ' ;
187238 $ help .= " $ command $ desc \n" ;
188239 }
@@ -191,11 +242,30 @@ public function showCommands($err = '')
191242 exit (0 );
192243 }
193244
245+ /**
246+ * @param string $name
247+ * @param mixed $default
248+ * @return mixed|null
249+ */
250+ public function getArg ($ name , $ default = null )
251+ {
252+ return $ this ->args [$ name ] ?? $ default ;
253+ }
254+
255+ /**
256+ * @param string $name
257+ * @param mixed $default
258+ * @return mixed|null
259+ */
260+ public function getOpt ($ name , $ default = null )
261+ {
262+ return $ this ->opts [$ name ] ?? $ default ;
263+ }
264+
194265///////////////////////////////////////////////////////////////////////////////////
195- /// helper methods
266+ /// getter/setter methods
196267///////////////////////////////////////////////////////////////////////////////////
197268
198-
199269 /**
200270 * @return array
201271 */
0 commit comments