Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added defaultCommand and getDefaultCommand methods #90

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,37 @@ public function add(Command $command, string $alias = '', bool $default = false)
return $this;
}

/**
* Set the default command.
*
* @param string $commandName The name of the default command
*
* @return self The application
*
* @throws InvalidArgumentException If the specified command name does not exist
*/
public function defaultCommand(string $commandName): self
{
if (!isset($this->commands[$commandName])) {
throw new InvalidArgumentException(sprintf('Command "%s" does not exist', $commandName));
}

$this->default = $commandName;

return $this;
}

/**
* Get the default command.
*
* @return string|null The name of the default command, or null if not set
*/
public function getDefaultCommand(): ?string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be always a default command i think as it is set in constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just use it as a getter to get the default command cauz i need it in test

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no i mean about the nullable string in return type. can be just string i guess. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah maybe but if command is not default it's should return null right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't default set in construct already? __default__

{
return $this->default;
}


/**
* Groups commands set within the callable.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,21 @@ protected function newApp(string $name, string $version = '')

return $app->io(new Interactor(static::$in, static::$ou));
}

public function testDefaultCommand()
{
$app = $this->newApp("test");

// Add some sample commands to the application
$app->command('command1');
$app->command('command2');

// Test setting a valid default command
$app->defaultCommand('command1');
$this->assertEquals('command1', $app->getDefaultCommand());

// Test setting an invalid default command
$this->expectException(InvalidArgumentException::class);
$app->defaultCommand('invalid_command');
}
}