Skip to content

Commit 4294553

Browse files
committed
update some logic
1 parent 2e406be commit 4294553

File tree

5 files changed

+65
-24
lines changed

5 files changed

+65
-24
lines changed

src/AbstractApplication.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ abstract class AbstractApplication implements ApplicationInterface
5353
'list' => 'List all group and alone commands',
5454
];
5555

56-
/**
57-
* @var int[]
58-
*/
59-
protected static $globalOptionNames = [
60-
'debug' => 1,
61-
'profile' => 1,
62-
'no-color' => 1,
63-
'h' => 1,
64-
'help' => 1,
65-
'V' => 1,
66-
'version' => 1,
67-
'no-interactive' => 1,
68-
];
69-
7056
/** @var array */
7157
protected static $globalOptions = [
7258
'--debug' => 'Setting the application runtime debug level(0 - 4)',
@@ -96,8 +82,9 @@ abstract class AbstractApplication implements ApplicationInterface
9682
'updateAt' => '2019.01.01',
9783
'rootPath' => '',
9884
'strictMode' => false,
99-
'interactive' => true,
10085
'hideRootPath' => true,
86+
// global options
87+
'no-interactive' => true,
10188

10289
// 'timeZone' => 'Asia/Shanghai',
10390
// 'env' => 'prod', // dev test prod
@@ -169,7 +156,7 @@ protected function init(): void
169156
*/
170157
public static function isGlobalOption(string $name): bool
171158
{
172-
return isset(self::$globalOptionNames[$name]);
159+
return isset(GlobalOption::KEY_MAP[$name]);
173160
}
174161

175162
/**
@@ -563,7 +550,9 @@ public function isStrictMode(): bool
563550
*/
564551
public function getVerbLevel(): int
565552
{
566-
return (int)$this->input->getLongOpt('debug', (int)$this->config['debug']);
553+
$key = GlobalOption::DEBUG;
554+
555+
return (int)$this->input->getLongOpt($key, (int)$this->config[$key]);
567556
}
568557

569558
/**
@@ -573,7 +562,10 @@ public function getVerbLevel(): int
573562
*/
574563
public function isProfile(): bool
575564
{
576-
return (bool)$this->input->getOpt('profile', $this->getParam('profile', false));
565+
$key = GlobalOption::PROFILE;
566+
$def = (bool)$this->getParam($key, false);
567+
568+
return $this->input->getBoolOpt($key, $def);
577569
}
578570

579571
/**
@@ -583,8 +575,9 @@ public function isProfile(): bool
583575
*/
584576
public function isInteractive(): bool
585577
{
586-
$key = 'no-interactive';
587-
$val = (bool)$this->input->getOpt($key, $this->getParam($key, true));
578+
$key = GlobalOption::NO_INTERACTIVE;
579+
$def = (bool)$this->getParam($key, true);
580+
$val = $this->input->getBoolOpt($key, $def);
588581

589582
return $val === false;
590583
}

src/GlobalOption.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console;
4+
5+
/**
6+
* Class GlobalOption
7+
*
8+
* @package Inhere\Console
9+
*/
10+
class GlobalOption
11+
{
12+
public const HELP = 'help';
13+
14+
public const DEBUG = 'debug';
15+
16+
public const VERSION = 'version';
17+
18+
public const PROFILE = 'profile';
19+
20+
public const NO_COLOR = 'no-color';
21+
22+
public const NO_INTERACTIVE = 'no-interactive';
23+
24+
public const KEY_MAP = [
25+
'debug' => 1,
26+
'profile' => 1,
27+
'no-color' => 1,
28+
'h' => 1,
29+
'help' => 1,
30+
'V' => 1,
31+
'version' => 1,
32+
'no-interactive' => 1,
33+
];
34+
}

src/Traits/FormatOutputAwareTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
*
6464
* @method Generator counterTxt($msg = 'Pending ', $ended = false)
6565
*
66-
* @method confirm(string $question, bool $default = true, bool $nl = true): bool
66+
* @method confirm(string $question, bool $default = true): bool
67+
* @method unConfirm(string $question, bool $default = true): bool
6768
* @method select(string $description, $options, $default = null, bool $allowExit = true): string
6869
* @method checkbox(string $description, $options, $default = null, bool $allowExit = true): array
6970
* @method ask(string $question, string $default = '', Closure $validator = null): string
@@ -299,6 +300,7 @@ public function __call($method, array $args = [])
299300
return Show::$method(...$args);
300301
}
301302

303+
// interact methods
302304
if (method_exists(Interact::class, $method)) {
303305
return Interact::$method(...$args);
304306
}

src/Traits/UserInteractAwareTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @method array checkbox(string $description, $options, $default = null, $allowExit = true)
2626
* @method array multiSelect(string $description, $options, $default = null, $allowExit = true)
2727
*
28+
* @method unConfirm(string $question, bool $default = true): bool
29+
*
2830
* @method string askHiddenInput(string $prompt = 'Enter Password:')
2931
* @method string promptSilent(string $prompt = 'Enter Password:')
3032
* @method string askPassword(string $prompt = 'Enter Password:')
@@ -52,7 +54,6 @@ public function select(string $description, $options, $default = null, $allowExi
5254
* @param bool $allowExit
5355
*
5456
* @return string
55-
* @see Interact::choice()
5657
*/
5758
public function choice(string $description, $options, $default = null, $allowExit = true): string
5859
{
@@ -64,7 +65,6 @@ public function choice(string $description, $options, $default = null, $allowExi
6465
* @param bool $default
6566
*
6667
* @return bool
67-
* @see Interact::confirm()
6868
*/
6969
public function confirm(string $question, bool $default = true): bool
7070
{
@@ -77,7 +77,6 @@ public function confirm(string $question, bool $default = true): bool
7777
* @param Closure|null $validator
7878
*
7979
* @return string|null
80-
* @see Interact::question()
8180
*/
8281
public function ask(string $question, string $default = '', Closure $validator = null): ?string
8382
{

src/Util/Interact.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public static function confirm(string $question, bool $default = true): bool
165165
return Confirm::ask($question, $default);
166166
}
167167

168+
/**
169+
* Send a message request confirmation
170+
*
171+
* @param string $question The question message
172+
* @param bool $default Default value
173+
*
174+
* @return bool
175+
*/
176+
public static function unConfirm(string $question, bool $default = true): bool
177+
{
178+
return false === Confirm::ask($question, $default);
179+
}
180+
168181
/**
169182
* Usage:
170183
*

0 commit comments

Comments
 (0)