Skip to content

Commit 04df613

Browse files
feat: Add support for default option values
1 parent 6967c3c commit 04df613

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

src/CommandParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class CommandParser
1515
private array $options = [];
1616
private array $arguments = [];
1717

18-
public function __construct(array $argv = null)
18+
public function __construct(?array $argv = null)
1919
{
2020
$argv = $argv ?? $_SERVER['argv'] ?? [];
2121
array_shift($argv);

src/CommandRunner.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ final class CommandRunner
2323
*/
2424
private array $commands = [];
2525

26-
/**
27-
* @var CommandInterface
28-
*/
29-
private CommandInterface $defaultCommand;
26+
private HelpCommand $defaultCommand;
3027

3128
/**
3229
* Application constructor.
@@ -117,10 +114,12 @@ private function execute(CommandInterface $command, CommandParser $commandParser
117114
}
118115
}
119116

120-
$options[] = new CommandOption('verbose', 'v', 'Enable verbose output', true);
117+
$options[] = CommandOption::flag('verbose', 'v', 'Enable verbose output');
121118
foreach ($options as $option) {
122119
if ($option->isFlag()) {
123120
$argvOptions["--{$option->getName()}"] = false;
121+
}elseif ($option->getDefaultValue() !== null) {
122+
$argvOptions["--{$option->getName()}"] = $option->getDefaultValue();
124123
}
125124
}
126125
foreach ($commandParser->getOptions() as $name => $value) {
@@ -203,6 +202,10 @@ private function showCommandHelp(CommandInterface $selectedCommand, OutputInterf
203202
if (!$option->isFlag()) {
204203
$name = sprintf('%s=VALUE', $name);
205204
}
205+
if ($option->getDefaultValue() !== null) {
206+
$name = sprintf('%s (default: %s)', $name, $option->getDefaultValue());
207+
}
208+
206209
$options[$name] = $option->getDescription();
207210
}
208211
$consoleOutput->listKeyValues($options, true);

src/Option/CommandOption.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ final class CommandOption
99
private ?string $description;
1010
private bool $isFlag;
1111

12+
/**
13+
* @var bool|float|int|string
14+
*/
15+
private $default = null;
16+
1217
public function __construct(
1318
string $name,
1419
?string $shortcut = null,
1520
?string $description = null,
16-
bool $isFlag = false
21+
bool $isFlag = false,
22+
$default = null
1723
)
1824
{
1925

@@ -31,10 +37,20 @@ public function __construct(
3137
throw new \InvalidArgumentException('Shortcut must be a single character and contain only letters. "' . $shortcut . '" is invalid.');
3238
}
3339

40+
if (!is_null($default) && !is_scalar($default)) {
41+
throw new \InvalidArgumentException(
42+
sprintf(
43+
'Invalid default value: expected a scalar (int, float, string, or bool), got "%s".',
44+
gettype($default)
45+
)
46+
);
47+
}
48+
3449
$this->name = strtolower($name);
3550
$this->shortcut = $shortcut !== null ? strtolower($shortcut) : null;
3651
$this->description = $description;
3752
$this->isFlag = $isFlag;
53+
$this->default = $default;
3854
}
3955

4056
public function getName(): string
@@ -57,11 +73,17 @@ public function isFlag(): bool
5773
return $this->isFlag;
5874
}
5975

76+
public function getDefaultValue()
77+
{
78+
return $this->default;
79+
}
80+
6081
public static function flag(string $name, ?string $shortcut = null, ?string $description = null): self {
61-
return new self($name, $shortcut, $description, true);
82+
return new self($name, $shortcut, $description, true, false);
6283
}
6384

64-
public static function withValue(string $name, ?string $shortcut = null, ?string $description = null): self {
65-
return new self($name, $shortcut, $description, false);
85+
public static function withValue(string $name, ?string $shortcut = null, ?string $description = null, $default = null): self {
86+
return new self($name, $shortcut, $description, false, $default);
6687
}
67-
}
88+
89+
}

tests/Command/UserListCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpDevCommunity\Console\Command\CommandInterface;
66
use PhpDevCommunity\Console\InputInterface;
7+
use PhpDevCommunity\Console\Option\CommandOption;
78
use PhpDevCommunity\Console\OutputInterface;
89

910
class UserListCommand implements CommandInterface
@@ -21,6 +22,7 @@ public function getDescription(): string
2122
public function getOptions(): array
2223
{
2324
return [
25+
CommandOption::withValue('limit', 'l', 'Limit the number of results', 100),
2426
];
2527
}
2628

@@ -33,5 +35,6 @@ public function getArguments(): array
3335
public function execute(InputInterface $input, OutputInterface $output): void
3436
{
3537
$output->write('Test OK : User list');
38+
$output->write(sprintf('LIMIT : %d', $input->getOptionValue('limit')));
3639
}
3740
}

tests/CommandRunnerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ protected function execute(): void
5353
$this->assertStringContains( $message, 'Execution time:');
5454
$this->assertStringContains( $message, 'Peak memory usage:');
5555
$this->assertEquals(0, $exitCode);
56+
57+
58+
$messages = [];
59+
$exitCode = $this->commandRunner->run(new CommandParser(self::createArgv(['app:user:list'])), new Output(function ($message) use(&$messages){
60+
$messages[] = $message;
61+
})
62+
);
63+
$this->assertEquals(0, $exitCode);
64+
$this->assertEquals('Test OK : User list', $messages[0]);
65+
$this->assertEquals('LIMIT : 100', $messages[1]);
66+
67+
$messages = [];
68+
$exitCode = $this->commandRunner->run(new CommandParser(self::createArgv(['app:user:list', '-l', '1000'])), new Output(function ($message) use(&$messages){
69+
$messages[] = $message;
70+
})
71+
);
72+
$this->assertEquals(0, $exitCode);
73+
$this->assertEquals('Test OK : User list', $messages[0]);
74+
$this->assertEquals('LIMIT : 1000', $messages[1]);
5675
}
5776

5877
private static function createArgv(array $argv): array

0 commit comments

Comments
 (0)