88
99namespace Inhere \Console \IO ;
1010
11- use Inhere \Console \Utils \CommandLineParse ;
11+ use Inhere \Console \Utils \CommandLine ;
1212
1313/**
14- * Class Input
14+ * Class Input - the input information. by parse global var $argv.
1515 * @package Inhere\Console\IO
1616 */
1717class Input implements InputInterface
1818{
1919 /**
20- * @var @ resource
20+ * @var resource
2121 */
22- protected $ inputStream = STDIN ;
22+ protected $ inputStream = \ STDIN ;
2323
2424 /**
25- * @var
25+ * @var string
2626 */
2727 private $ pwd ;
2828
2929 /**
30+ * eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
3031 * @var string
3132 */
3233 private $ fullScript ;
@@ -72,23 +73,25 @@ class Input implements InputInterface
7273 /**
7374 * Input constructor.
7475 * @param null|array $argv
76+ * @param bool $parsing
7577 */
76- public function __construct ($ argv = null )
78+ public function __construct ($ argv = null , $ parsing = true )
7779 {
7880 if (null === $ argv ) {
7981 $ argv = $ _SERVER ['argv ' ];
8082 }
8183
8284 $ this ->pwd = $ this ->getPwd ();
83-
85+ $ this -> tokens = $ argv ;
8486 $ this ->fullScript = implode (' ' , $ argv );
8587 $ this ->script = array_shift ($ argv );
86- $ this ->tokens = $ argv ;
8788
88- list ($ this ->args , $ this ->sOpts , $ this ->lOpts ) = CommandLineParse::byArgv ($ argv );
89+ if ($ parsing ) {
90+ list ($ this ->args , $ this ->sOpts , $ this ->lOpts ) = CommandLine::parseByArgv ($ argv );
8991
90- // collect command `server`
91- $ this ->command = isset ($ this ->args [0 ]) ? array_shift ($ this ->args ) : null ;
92+ // collect command. it is first argument.
93+ $ this ->command = isset ($ this ->args [0 ]) ? array_shift ($ this ->args ) : null ;
94+ }
9295 }
9396
9497 /**
@@ -98,11 +101,11 @@ public function __toString()
98101 {
99102 $ tokens = array_map (function ($ token ) {
100103 if (preg_match ('{^(-[^=]+=)(.+)} ' , $ token , $ match )) {
101- return $ match [1 ] . CommandLineParse ::escapeToken ($ match [2 ]);
104+ return $ match [1 ] . CommandLine ::escapeToken ($ match [2 ]);
102105 }
103106
104107 if ($ token && $ token [0 ] !== '- ' ) {
105- return CommandLineParse ::escapeToken ($ token );
108+ return CommandLine ::escapeToken ($ token );
106109 }
107110
108111 return $ token ;
@@ -641,4 +644,12 @@ public function getPwd(): string
641644
642645 return $ this ->pwd ;
643646 }
647+
648+ /**
649+ * @return array
650+ */
651+ public function getTokens (): array
652+ {
653+ return $ this ->tokens ;
654+ }
644655}
0 commit comments