Skip to content

Commit d16d233

Browse files
fix bug default argument value
1 parent 6bac55b commit d16d233

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

Diff for: src/CommandRunner.php

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ private function execute(CommandInterface $command, CommandParser $commandParser
9494
$argvOptions = [];
9595

9696
$options = $command->getOptions();
97+
foreach ($options as $option) {
98+
if ($option->isFlag()) {
99+
$argvOptions["--{$option->getName()}"] = false;
100+
}
101+
}
97102
foreach ($commandParser->getOptions() as $name => $value) {
98103
$hasOption = false;
99104
foreach ($options as $option) {
@@ -121,6 +126,8 @@ private function execute(CommandInterface $command, CommandParser $commandParser
121126
}
122127
if ($commandParser->hasArgument($key)) {
123128
$argv["--{$argument->getName()}"] = $commandParser->getArgumentValue($key);
129+
}else {
130+
$argv["--{$argument->getName()}"] = $argument->getDefaultValue();
124131
}
125132
}
126133

Diff for: src/Input.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public function hasOption(string $name): bool
3939
if (!self::startsWith($name, '--')) {
4040
$name = "--$name";
4141
}
42-
4342
return array_key_exists($name, $this->options);
4443
}
4544

@@ -57,6 +56,9 @@ public function getOptionValue(string $name)
5756

5857
public function getArgumentValue(string $name)
5958
{
59+
if (!self::startsWith($name, '--')) {
60+
$name = "--$name";
61+
}
6062
if (!$this->hasArgument($name)) {
6163
throw new \InvalidArgumentException(sprintf('Argument "%s" is not defined.', $name));
6264
}
@@ -65,6 +67,9 @@ public function getArgumentValue(string $name)
6567

6668
public function hasArgument(string $name): bool
6769
{
70+
if (!self::startsWith($name, '--')) {
71+
$name = "--$name";
72+
}
6873
return array_key_exists($name, $this->arguments);
6974
}
7075

Diff for: tests/Command/FooCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getOptions(): array
3131
public function getArguments(): array
3232
{
3333
return [
34-
new CommandArgument('input', false, null, 'The input file for the foo operation')
34+
new CommandArgument('input', false, 'none', 'The input file for the foo operation')
3535
];
3636
}
3737

Diff for: tests/CommandTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ public function testExecute(): void
9393

9494
$this->assertEquals(6, $lines);
9595
}
96-
}
96+
}

Diff for: tests/InputTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testGetOptionValue()
7171

7272
public function testGetArgumentValue()
7373
{
74-
$input = new Input('test', [], ['argument' => 'value']);
74+
$input = new Input('test', [], ['--argument' => 'value']);
7575
$this->assertEquals('value', $input->getArgumentValue('argument'));
7676
$this->expectException(\InvalidArgumentException::class, function () use ($input) {
7777
$input->getArgumentValue('invalid');
@@ -80,8 +80,8 @@ public function testGetArgumentValue()
8080

8181
public function testHasArgument()
8282
{
83-
$input = new Input('test', [], ['argument' => 'value']);
83+
$input = new Input('test', [], ['--argument' => 'value']);
8484
$this->assertTrue($input->hasArgument('argument'));
8585
$this->assertFalse($input->hasArgument('invalid'));
8686
}
87-
}
87+
}

0 commit comments

Comments
 (0)