如何隐藏或删除默认可用的控制台命令? [英] How to hide or delete the defaults available console commands?

查看:18
本文介绍了如何隐藏或删除默认可用的控制台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 symfony new my_project_name 创建了一个新的 Symfony 4 项目.

I created a new Symfony 4 project via symfony new my_project_name.

目前当我执行./bin/console时,输出显示

Currently when I execute ./bin/console, the output shows

我将创建一些自定义控制台命令,并且在执行 ./bin/console

I will create some custom console commands and I want show only my custom commands when I do ./bin/console

也许我应该从头开始创建一个自定义的可执行控制台",但我不知道该怎么做.

Maybe I should create a custom executable 'console' from scratch, but I don't know how do that.

推荐答案

您正在创建一个完整的 Symfony 应用程序,因此包含的包提供的所有命令都可用.

You are creating a complete Symfony application, so all the commands provided by the included packages are available.

与其从一个框架开始并试图削减您不想要的部分,您需要从更远的地方开始,以拥有一个真正的准系统项目.

Instead of starting from a framework and trying to trim down the parts you do not want, you need to start from further down to have a really barebones project.

首先不要使用symfony命令,没必要.普通的老作曲家会做到这一点.

First, do not use symfony command, no need. Plain old composer will do the trick.

目录中,执行:

composer require symfony/console

这将导入控制台项目所需的唯一依赖项,并为您的自动加载器执行基本引导.

This will import the only dependency needed for a console project, and do the basic bootstrapping for your autoloader.

composer.json 中,添加以下内容:

In composer.json, add the following:

"autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }

命令类

您需要一个或多个命令才能实际添加到您的应用程序中.让我们从一个完全成熟的问候应用程序开始.在您的项目中创建文件 src/Greet.php:

declare(strict_types=1);

namespace App;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Greet extends Symfony\Component\Console\Command\Command
{
    protected function configure()
    {
        $this->addArgument('person', InputArgument::OPTIONAL, 'Name of the Entity being greeted', 'World');
        $this->addOption('greeting', 'g', InputOption::VALUE_OPTIONAL, 'How to greet the entity', 'Hello');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $greeting = $input->getOption('greeting');
        $entity   = $input->getArgument('person');

        $output->writeln("<info>$greeting $entity!</info>");

    }
}

这是一个将打印Hello World!"的基本命令.如果在没有任何选项或参数的情况下执行,将接受一个参数使用而不是World",以及一个选项来设置问候语而不是Hello".

This is a basic command that will print "Hello World!" if executed without any option or argument, will accept one argument use instead of "World", and one option to set the greeting instead of "Hello".

在项目的根目录上,创建一个文件 app.php.

On the project's root, create a file app.php.

require __DIR__ . '/vendor/autoload.php';
$app = new Symfony\Component\Console\Application('Hello World App', '1.0.0');
$app->add((new App\Greet('greet')));
$app->run();

这是一个很短的脚本,所以让我们一行一行地进行

This is be a very short script, so let's go line by line

  1. 需要自动加载器.此脚本是应用程序的入口点,因此我们需要执行自动加载器.
  2. 实例化应用程序.这将创建 Symfony 控制台应用程序的新实例,为应用程序设置名称和版本.这将用于应用程序的帮助"打印输出.
  3. 添加命令.默认情况下,应用程序根本没有命令.我们需要添加我们刚刚创建的命令.add() 需要一个 Command 实例.我们实例化刚刚创建的命令,并将其设置为名称为greet".
  4. 运行应用程序
  1. Require the autoloader. This script is the entry point for the application, so we need to execute the autoloader.
  2. Instantiate the application. This creates a new instance of the Symfony Console application, sets a name and version for the app. This will be used in the "help" printouts for the app.
  3. Add the command. By default, the application has no commands at all. We'll need to add the command we have just created. add() expects a Command instance. We instantiate the command we just created, and we set it to be called by the name "greet".
  4. Run the application

生成自动加载器.

执行 composer dump-autoload 以便为您的应用程序生成自动加载器.

Generate autoloader.

Execute composer dump-autoload so the autoloader for your application is generated.

如果你现在执行 php app.php 你会得到:

If you now execute php app.php you'll get:

Hello World App 1.0.0

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  greet
  help   Displays help for a command
  list   Lists commands

请注意,唯一可用的命令是 greet.您可以像这样使用:

Note that the only available command is greet. Which you can use like this:

# php app.php greet
Hello World!
# php app.php greet Alice
Hello Alice!
# php app.php greet Bob -g "Good morning"
Good morning Bob!
# php app.php help greet
Usage:
  greet [options] [--] [<person>]

Arguments:
  person                     Name of the Entity being greeted [default: "World"]

Options:
  -g, --greeting[=GREETING]  How to greet the entity [default: "Hello"]
[... default generic options omitted]

这篇关于如何隐藏或删除默认可用的控制台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆