From f908a562ec51f1a6ce441fe6c48b565e70ccd2ba Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Tue, 7 May 2024 15:10:33 +0100 Subject: [PATCH] Add generic Argument attribute --- src/Commands/Attributes/Argument.php | 4 +++- src/Commands/Concerns/Virtue.php | 10 ++++++++-- tests/Datasets/TestCommand.php | 3 +++ tests/Feature/CommandAttributesTest.php | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Commands/Attributes/Argument.php b/src/Commands/Attributes/Argument.php index ba878a4..909abe1 100644 --- a/src/Commands/Attributes/Argument.php +++ b/src/Commands/Attributes/Argument.php @@ -4,9 +4,11 @@ namespace WendellAdriel\Virtue\Commands\Attributes; +use Attribute; use Closure; -abstract class Argument +#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] +class Argument { /** * @param string|int|bool|array|float|null $default diff --git a/src/Commands/Concerns/Virtue.php b/src/Commands/Concerns/Virtue.php index b42b541..5bbb144 100644 --- a/src/Commands/Concerns/Virtue.php +++ b/src/Commands/Concerns/Virtue.php @@ -26,9 +26,15 @@ trait Virtue public function getArguments(): array { $arguments = []; - $requiredArguments = $this->buildArgumentsList(RequiredArgument::class); + $generalArguments = $this->buildArgumentsList(Argument::class); + + $requiredArguments = $this->buildArgumentsList(RequiredArgument::class) + ->merge($generalArguments->where('mode', InputArgument::REQUIRED)); + [$arrayArguments, $nonArrayArguments] = $requiredArguments->partition(fn (array $argument) => $argument['mode'] > InputArgument::REQUIRED); - $optionalArguments = $this->buildArgumentsList(OptionalArgument::class); + + $optionalArguments = $this->buildArgumentsList(OptionalArgument::class) + ->merge($generalArguments->filter(fn (array $argument) => $argument['mode'] === InputArgument::OPTIONAL || $argument['mode'] === InputArgument::IS_ARRAY)); foreach ($nonArrayArguments as $argument) { $arguments[] = $argument; diff --git a/tests/Datasets/TestCommand.php b/tests/Datasets/TestCommand.php index cb8c261..5c72c03 100644 --- a/tests/Datasets/TestCommand.php +++ b/tests/Datasets/TestCommand.php @@ -5,6 +5,7 @@ namespace Tests\Datasets; use Illuminate\Console\Command; +use WendellAdriel\Virtue\Commands\Attributes\Argument; use WendellAdriel\Virtue\Commands\Attributes\FlagOption; use WendellAdriel\Virtue\Commands\Attributes\OptionalArgument; use WendellAdriel\Virtue\Commands\Attributes\RequiredArgument; @@ -13,6 +14,8 @@ #[RequiredArgument(name: 'name')] #[OptionalArgument(name: 'age', default: 18)] +#[Argument(name: 'optional', required: false, description: 'Optional parameter')] +#[Argument(name: 'required')] #[FlagOption(name: 'negative', shortcut: 'm', negatable: true)] #[ValueOption(name: 'year', description: 'The year')] #[ValueOption(name: 'scores', array: true, default: [1, 2, 3])] diff --git a/tests/Feature/CommandAttributesTest.php b/tests/Feature/CommandAttributesTest.php index be0a784..cd342c6 100644 --- a/tests/Feature/CommandAttributesTest.php +++ b/tests/Feature/CommandAttributesTest.php @@ -10,8 +10,11 @@ $arguments = Collection::make($command->getArguments())->keyBy('name'); $options = Collection::make($command->getOptions())->keyBy('name'); - expect($arguments)->toHaveCount(2) + expect($arguments)->toHaveCount(4) ->and($arguments->get('name'))->toBeArray() + ->and($arguments->get('required'))->toBeArray() + ->and($arguments->get('optional'))->toBeArray() + ->and($arguments->get('optional')['description'])->toBe('Optional parameter') ->and($arguments->get('age'))->toBeArray() ->and($arguments->get('age')['default'])->toBe(18) ->and($options)->toHaveCount(3)