---
title: "Adding Commands from a Package"
description: "Add commands to Crest from a package, in the composer.json of the package."
version: "1.0"
---

> Documentation Index
> Fetch the complete documentation index at: https://crest.phalcon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding Commands from a Package

A package adds commands to Crest in its `composer.json`:

```json
"extra": {
    "crest": {
        "commands": {
            "demo:greet": "Vendor\\Demo\\GreetCommand"
        }
    }
}
```

Crest reads this block from each package that is installed with the Crest that runs, and from its root project. In a project, the global Crest passes the listing and the project commands to the project Crest (see [The global Crest and the project Crest](/1.0/how-crest-works#the-global-crest-and-the-project-crest)). Thus only the packages of the project count. Outside a project, the packages of the global Crest count. These commands cannot replace a command or an alias of Crest, and the root project replaces a package command with the same name. `crest make:command` writes a command and prints its block (see [Command reference](/1.0/commands)). A package command works in both flavors, and `make:command` gives the same output in both.

An unknown command gives a hint when Crest knows the package that provides its prefix:

```text
crest: unknown command 'migration:run'; provided by phalcon/migrations
```

There is no hint when a registered command has that prefix: the package is installed, and only the name is wrong.

A command extends `Crest\Console\Command\Command` and declares two methods:

```php
use Crest\Console\Command\Command;
use Crest\Console\Input;
use Crest\Console\Output;
use Crest\Console\Parsing\Definition;

final class GreetCommand extends Command
{
    public function define(): Definition
    {
        return Definition::for('demo:greet', 'Greet someone')
            ->argument('name', true, 'Who to greet')
            ->option('shout', 'Use capital letters');
    }

    public function handle(Input $input, Output $output): int
    {
        $output->line('Hello ' . $input->argumentString('name'));

        return 0;
    }
}
```

Crest makes the command with `new $class()`, so the constructor must work without arguments. Crest skips a class that is not a `Command`, or that `new $class()` cannot create. Each command also gets the global options (`--config`, `--directory`, `--trace`, `--help`, `--quiet`, `--no-interaction`; see [Global options](/1.0/commands#global-options)).

These types are the API for a package. Each type has `@api` in its docblock:

| Type | Use |
|---|---|
| `Crest\Console\Command\Command` | the base class of a command; `notes()` gives lines for the end of its help |
| `Crest\Console\Parsing\Definition` | `for()`, `argument()` and `option()` declare the arguments and options; `help()` and `example()` give the help text and the examples of `--help` |
| `Crest\Console\Parsing\Example` | an example of `getExamples()`: its `line` and its `comment` |
| `Crest\Console\Input` | the values of the call |
| `Crest\Console\Output` | `line()`, `success()`, `error()`, `write()`, `table()`, `ask()` and `choice()` |
| `Crest\Console\Exceptions\Exception` | throw it to stop with one clean error line |
| `Crest\Command\Project` | `Project::context($input)` gives the project of the call |
| `Crest\Project\ProjectContext` | `root()`, `path($key)` and `namespaceFor($key)` |

`Project::context()` finds the project as the other commands do (see [The project file](/1.0/how-crest-works#the-project-file)). Without `crest.php`, the command stops with the `crest init` hint.

A public element with `@internal` in its docblock is not part of the API, and it can change in any release. Tools that read the tag, for example an IDE or a rule of a static analyzer, can report its use from another package. Do not extend `Crest\Command\ProjectCommand`: it is the base of the commands of Crest, and it changes with them. Do not use `Crest\Project\Config`: `Project::context()` gives the project as a `ProjectContext`.

Source: https://crest.phalcon.io/1.0/package-commands/index.mdx
