Skip to content

Commit

Permalink
Add generic Argument attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 7, 2024
1 parent a9f999c commit f908a56
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Commands/Attributes/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>|float|null $default
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/Concerns/Virtue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tests/Datasets/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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])]
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/CommandAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f908a56

Please sign in to comment.