gvd
gvd

Reputation: 1592

How to output to terminal options and its description from Laravel Commands

From Laravel documentation you can create your own artisan commands and add parameters with descriptions

{--param= : description}

For example if user not enter a required parameter I want to show this parameter and it description like is defined in $signature property.

How can I do this?

Upvotes: 7

Views: 3631

Answers (2)

Max
Max

Reputation: 405

If you want to display the help text programatatically, you can use the base Symfony methods to achieve this easily from inside your Laravel command:

$help = new \Symfony\Component\Console\Command\HelpCommand\HelpCommand();
$help->setCommand($this);
$help->run($this->input, $this->output);

See this SO answer.

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

You can display all options available for a command with calling it with -h or --help option:

php artisan yourcommand -h

To add description to parameters use this syntax:

protected $signature = 'yourcommand {someargument : Description of option}';

If you want to do that programmatically, create a public method in the command class:

public function help()
{
    // Display all arguments with descriptions.
    foreach($this->getDefinition()->getArguments() as $name => $description) {
        echo $name . ' - ' . $description->getDescription();
    };

    // Display all options with descriptions.
    foreach($this->getDefinition()->getOptions() as $name => $description) {
        echo $name . ' - ' . $description->getDescription();
    };
}

Then call it from your code:

app('\App\Console\Commands\YourCommand')->help();

Upvotes: 9

Related Questions