diff --git a/src/Decorate/SubCommandsWareTrait.php b/src/Decorate/SubCommandsWareTrait.php index 8b8b39a..ad961a3 100644 --- a/src/Decorate/SubCommandsWareTrait.php +++ b/src/Decorate/SubCommandsWareTrait.php @@ -16,7 +16,9 @@ use Inhere\Console\Handler\CommandWrapper; use Inhere\Console\Util\Helper; use InvalidArgumentException; +use RuntimeException; use Toolkit\Cli\Color\ColorTag; +use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Helper\Assert; use Toolkit\Stdlib\Obj\Traits\NameAliasTrait; use function array_keys; @@ -272,6 +274,18 @@ public function getParent(): ?AbstractHandler return $this->parent; } + /** + * @return FlagsParser + */ + public function getParentFlags(): FlagsParser + { + if (!$this->parent) { + throw new RuntimeException('no parent command of the: ' . $this->getCommandName()); + } + + return $this->parent->getFlags(); + } + /********************************************************** * helper methods **********************************************************/ diff --git a/src/Util/FormatUtil.php b/src/Util/FormatUtil.php index 2e9ed2c..e91b6f6 100644 --- a/src/Util/FormatUtil.php +++ b/src/Util/FormatUtil.php @@ -17,6 +17,7 @@ use Toolkit\Sys\Sys; use function array_merge; use function array_shift; +use function count; use function explode; use function implode; use function is_array; @@ -24,7 +25,6 @@ use function is_int; use function is_numeric; use function is_scalar; -use function rtrim; use function str_repeat; use function strpos; use function trim; @@ -204,20 +204,9 @@ public static function spliceKeyValue(array $data, array $opts = []): string $lines = []; - // if value is array, translate array to string + // if value is array, convert array to string if (is_array($value)) { - $temp = '['; - foreach ($value as $k => $val) { - if (is_bool($val)) { - $val = $val ? '(True)' : '(False)'; - } else { - $val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val); - } - - $temp .= (!is_numeric($k) ? "$k: " : '') . "$val, "; - } - - $value = rtrim($temp, ' ,') . ']'; + $value = self::arr2str($value); // } elseif (is_object($value)) { // $value = get_class($value); } elseif (is_bool($value)) { @@ -242,7 +231,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string // value has multi line if ($lines) { - $linePrefix = $opts['leftChar'] . Str::repeat(' ', $keyWidth + 1) . $opts['sepChar']; + $linePrefix = $opts['leftChar'] . Str::repeat(' ', $keyWidth) . $opts['sepChar']; foreach ($lines as $line) { $fmtLines[] = $linePrefix . $line; } @@ -255,4 +244,25 @@ public static function spliceKeyValue(array $data, array $opts = []): string return implode("\n", $fmtLines); } + + public static function arr2str(array $arr): string + { + if (count($arr) === 0) { + return '[]'; + } + + $temp = "[\n"; + foreach ($arr as $k => $val) { + if (is_bool($val)) { + $val = $val ? '(True)' : '(False)'; + } else { + $val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val); + } + + $temp .= (!is_numeric($k) ? " $k: " : '') . "$val,\n"; + } + + $temp .= " ]"; + return $temp; + } }