Skip to content

Commit a3c521c

Browse files
committed
Fixes #8
1 parent 7c33dd8 commit a3c521c

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/Concerns/UsesConsoleToolkit.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ protected function propertyToOption(OptionReflection $option): InputOption
208208
$option->hasRequiredValue() => $this->makeInputOption(
209209
$option,
210210
$option->isAutoAskEnabled() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_REQUIRED,
211-
$option->isAutoAskEnabled() ? '$__not_provided__$' : null
212211
),
213212

214213
$option->hasValue() => $this->makeInputOption(

src/Concerns/UsesInputValidation.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ protected function validate(Collection $collection): Collection
3535
$messages
3636
);
3737

38-
if (! $validator->fails()) {
38+
if (!$validator->fails()) {
3939
return $collection;
4040
}
4141

42-
$inputErrors = $collection->mapWithKeys(fn (InputReflection $reflection) => [
42+
$inputErrors = $collection->mapWithKeys(fn(InputReflection $reflection) => [
4343
$reflection->getName() => new InputErrorData(
4444
key: $reflection->getName(),
4545
choices: $choices[$reflection->getName()] ?? [],
@@ -56,7 +56,7 @@ protected function validate(Collection $collection): Collection
5656
*/
5757
protected function extractValidationData(Collection $collection): array
5858
{
59-
return $collection->reduce(fn (array $carry, InputReflection $reflection) => [
59+
return $collection->reduce(fn(array $carry, InputReflection $reflection) => [
6060
'values' => [...$carry['values'], ...$this->extractInputValues($reflection)],
6161
'rules' => [...$carry['rules'], ...$this->extractInputRules($reflection)],
6262
'messages' => [...$carry['messages'], ...$this->extractValidationMessages($reflection)],
@@ -71,12 +71,12 @@ protected function extractValidationData(Collection $collection): array
7171

7272
protected function extractValidationMessages(InputReflection $reflection): array
7373
{
74-
if (! $reflection->getValidationMessage()) {
74+
if (!$reflection->getValidationMessage()) {
7575
return [];
7676
}
7777

7878
return collect($reflection->getValidationMessage())
79-
->mapWithKeys(fn (string $value, string $key) => ["{$reflection->getName()}.{$key}" => $value])
79+
->mapWithKeys(fn(string $value, string $key) => ["{$reflection->getName()}.{$key}" => $value])
8080
->all();
8181
}
8282

@@ -98,7 +98,7 @@ protected function extractInputRules(
9898
): array {
9999
$rules = [];
100100

101-
if ($this->hasAutoAskEnabled($reflection) && ! $reflection->isArray()) {
101+
if ($this->hasAutoAskEnabled($reflection) && !$reflection->isArray()) {
102102
$rules[] = 'required';
103103
}
104104

@@ -129,7 +129,12 @@ protected function hasAutoAskEnabled(InputReflection $reflection): bool
129129
ConsoleInputType::Argument => $reflection->isAutoAskEnabled(),
130130
ConsoleInputType::Option => $reflection->isAutoAskEnabled()
131131
&& $reflection->hasRequiredValue()
132-
&& $this->option($reflection->getName()) !== '$__not_provided__$',
132+
&& array_key_exists($reflection->getName(), $this->getSpecifiedOptions()),
133133
};
134134
}
135+
136+
protected function getSpecifiedOptions(): array
137+
{
138+
return (new \ReflectionClass($this->input))->getProperty('options')->getValue($this->input);
139+
}
135140
}

tests/ConsoleInputAutoAskTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ public function handle()
6666
->assertSuccessful();
6767
});
6868

69+
it('only asks for options if there is an required value and the option is used', function () {
70+
$command = new class () extends Command {
71+
use UsesConsoleToolkit;
72+
73+
protected $name = 'ask:me';
74+
75+
#[Option]
76+
public string $required;
77+
78+
public function handle()
79+
{
80+
$this->line('Some Text');
81+
}
82+
};
83+
84+
Artisan::starting(fn (Artisan $artisan) => $artisan->add($command));
85+
86+
\Pest\Laravel\artisan('ask:me --required')
87+
->expectsQuestion('Please enter "required"', 'Swiss')
88+
->expectsOutput('Some Text')
89+
->assertSuccessful();
90+
});
91+
6992
it('can give choices if missing input', function () {
7093
$command = new class () extends Command {
7194
use UsesConsoleToolkit;

0 commit comments

Comments
 (0)