Skip to content

Commit

Permalink
some update, add new input class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 24, 2017
1 parent 6127703 commit cf82404
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 26 deletions.
108 changes: 108 additions & 0 deletions src/IO/FixedInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 2017/12/24 0024
* Time: 18:39
*/

namespace Inhere\Console\IO;

/**
* Class FixedInput
* - 初始化时不全部解析,只取出 '-h' '--help' 还有命令名
* - 到运行命令时根据命令的参数选项配置(InputDefinition)来进行解析
* - @todo un-completed
* @package Inhere\Console\IO
*/
class FixedInput extends Input
{
/**
* the prepare parsed options.
* @see AbstractApplication::$internalOptions
* @var array
*/
private $preParsed =[
// opt name => has value
'h' => false,
'V' => false,
'help' => false,
'debug' => true,
'profile' => false,
'version' => false,
];

/** @var array */
private $cleanedTokens;

/**
* FixedInput constructor.
* @param null|array $argv
*/
public function __construct($argv = null)
{
if (null === $argv) {
$argv = $_SERVER['argv'];
}

parent::__construct($argv, false);

$copy = $argv;

// command name
if (!empty($copy[1]) && $copy[1][0] !== '-' && false === strpos($copy[1], '=')) {
$this->setCommand($copy[1]);

// unset command
unset($copy[1]);
}

// pop script name
array_shift($copy);

$this->cleanedTokens = $copy;
$this->collectPreParsed($copy);
}

private function collectPreParsed(array $tokens)
{
foreach ($this->preParsed as $name => $hasVal) {

}
}

/**
* @param array $allowArray
* @param array $noValues
*/
public function parseTokens(array $allowArray = [], array $noValues = [])
{
$params = $this->getTokens();
array_shift($params); // pop script name
}

/**
* @return array
*/
public function getPreParsed(): array
{
return $this->preParsed;
}

/**
* @param array $preParsed
*/
public function setPreParsed(array $preParsed)
{
$this->preParsed = $preParsed;
}

/**
* @return array|null
*/
public function getCleanedTokens(): array
{
return $this->cleanedTokens;
}

}
37 changes: 24 additions & 13 deletions src/IO/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@

namespace Inhere\Console\IO;

use Inhere\Console\Utils\CommandLineParse;
use Inhere\Console\Utils\CommandLine;

/**
* Class Input
* Class Input - the input information. by parse global var $argv.
* @package Inhere\Console\IO
*/
class Input implements InputInterface
{
/**
* @var @resource
* @var resource
*/
protected $inputStream = STDIN;
protected $inputStream = \STDIN;

/**
* @var
* @var string
*/
private $pwd;

/**
* eg `./examples/app home:useArg status=2 name=john arg0 -s=test --page=23`
* @var string
*/
private $fullScript;
Expand Down Expand Up @@ -72,23 +73,25 @@ class Input implements InputInterface
/**
* Input constructor.
* @param null|array $argv
* @param bool $parsing
*/
public function __construct($argv = null)
public function __construct($argv = null, $parsing = true)
{
if (null === $argv) {
$argv = $_SERVER['argv'];
}

$this->pwd = $this->getPwd();

$this->tokens = $argv;
$this->fullScript = implode(' ', $argv);
$this->script = array_shift($argv);
$this->tokens = $argv;

list($this->args, $this->sOpts, $this->lOpts) = CommandLineParse::byArgv($argv);
if ($parsing) {
list($this->args, $this->sOpts, $this->lOpts) = CommandLine::parseByArgv($argv);

// collect command `server`
$this->command = isset($this->args[0]) ? array_shift($this->args) : null;
// collect command. it is first argument.
$this->command = isset($this->args[0]) ? array_shift($this->args) : null;
}
}

/**
Expand All @@ -98,11 +101,11 @@ public function __toString()
{
$tokens = array_map(function ($token) {
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
return $match[1] . CommandLineParse::escapeToken($match[2]);
return $match[1] . CommandLine::escapeToken($match[2]);
}

if ($token && $token[0] !== '-') {
return CommandLineParse::escapeToken($token);
return CommandLine::escapeToken($token);
}

return $token;
Expand Down Expand Up @@ -641,4 +644,12 @@ public function getPwd(): string

return $this->pwd;
}

/**
* @return array
*/
public function getTokens(): array
{
return $this->tokens;
}
}
4 changes: 2 additions & 2 deletions src/IO/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Output implements OutputInterface
* 正常输出流
* Property outStream.
*/
protected $outputStream = STDOUT;
protected $outputStream = \STDOUT;

/**
* 错误输出流
* Property errorStream.
*/
protected $errorStream = STDERR;
protected $errorStream = \STDERR;

/**
* 控制台窗口(字体/背景)颜色添加处理
Expand Down
19 changes: 12 additions & 7 deletions src/Utils/CommandLineParse.php → src/Utils/CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace Inhere\Console\Utils;

/**
* Class CommandLineParse - console argument and option parse
* Class CommandLine - console argument and option parse
* @package Inhere\Console\Utils
*/
final class CommandLineParse
final class CommandLine
{
/**
* These words will be as a Boolean value
Expand Down Expand Up @@ -51,7 +51,7 @@ final class CommandLineParse
* @param bool $mergeOpts Whether merge short-opts and long-opts
* @return array
*/
public static function byArgv(array $params, array $noValues = [], $mergeOpts = false): array
public static function parseByArgv(array $params, array $noValues = [], $mergeOpts = false): array
{
$args = $sOpts = $lOpts = [];

Expand Down Expand Up @@ -125,11 +125,16 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
return [$args, $sOpts, $lOpts];
}

public static function parseByDefinition(array $tokens, array $allowArray = [], array $noValues = [])
{

}

/**
* parse custom array params
*
* ```php
* $result = CommandLineParse::byArray([
* $result = CommandLine::parseByArray([
* 'arg' => 'val',
* '--lp' => 'val2',
* '--s' => 'val3',
Expand All @@ -139,7 +144,7 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
* @param array $params
* @return array
*/
public static function byArray(array $params)
public static function parseByArray(array $params)
{
$args = $sOpts = $lOpts = [];

Expand All @@ -163,12 +168,12 @@ public static function byArray(array $params)
/**
*
* ```php
* $result = CommandLineParse::byString('foo --bar="foobar"');
* $result = CommandLine::parseByString('foo --bar="foobar"');
* ```
* @todo ...
* @param string $string
*/
public static function byString(string $string)
public static function parseByString(string $string)
{

}
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static function isRoot(): bool
/**
* @return bool
*/
public static function supportColor()
public static function isSupportColor()
{
return self::isSupportColor();
return self::supportColor();
}

/**
Expand All @@ -77,7 +77,7 @@ public static function supportColor()
* \Symfony\Component\Console\Output\OutputStream.
* @return boolean
*/
public static function isSupportColor()
public static function supportColor()
{
if (DIRECTORY_SEPARATOR === '\\') {
return
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ public static function progressBar($total, array $opts = [])
{
$current = 0;
$finished = false;
$tplPrefix = Helper::isSupportColor() ? "\x0D\x1B[2K" : "\x0D\r";
$tplPrefix = Helper::supportColor() ? "\x0D\x1B[2K" : "\x0D\r";
$opts = array_merge([
'doneChar' => '=',
'waitChar' => ' ',
Expand Down

0 comments on commit cf82404

Please sign in to comment.