-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Better support for one command app #16906
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
Conversation
06485a9
to
acc2ab5
Compare
"Unique command" doesn't imply that there's only one command. How about calling it a "single command"? |
Indeed ;) I will update my PR. |
|
||
public function setUniqueCommand($commandName) | ||
{ | ||
if (null !== $this->uniqueCommand) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also validate that it is registered ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Triggering an exception if you configure setUniqueCommand('foobar')
and there is no foobar
command registered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not, but if we do that, we are forcing the order. I mean will not able anymore to first set-up the single command name, then register the command.
acc2ab5
to
b342502
Compare
PR updated ;) |
I like this proposal. However, I don't like these two lines from the above example: ->getApplication()
->setSingleCommand('echo') Would this other code work? (new Application())
->register('echo')
->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo')
->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar')
->setCode(function(InputInterface $input, OutputInterface $output) {
// ...
})
->run(); |
@javiereguiluz I think we can implement that, but it's a bit too magic for me. And we don't want magic in symfony. |
What about:
|
I like it :) |
I'm afraid that the new example doesn't follow the Symfony philosophy either. It complicates the learning curve because it adds a new method ( |
Then |
Yes. Would this work? (new Application())
->register('echo')
->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo')
->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar')
->setCode(function(InputInterface $input, OutputInterface $output) {
// ...
})
->run('echo'); // <-- pass the command to run |
This is a good idea, but a BC break (ref) |
|
Why not (new SingleCommandApplication())
->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo')
->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar')
->setCode(function(InputInterface $input, OutputInterface $output) {
// ...
})
->run(); ? |
@nicolas-grekas this might complicate the Application a lot though, as @jvasseur this is a bad idea, as your SingleCommandApplication does not have the API of the Application anymore, but of the Command |
Yes, but whit black magic :-) |
b342502
to
42a9111
Compare
public function setSingleCommand($commandName) | ||
{ | ||
if (null !== $this->singleCommand) { | ||
throw new \LogicException('A Single command is already defined.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this check is needed. It looks like a useless restriction to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks it's useful restriction, yet it makes this feature less flexible.
And on framework level, I think it should be that way.
Strict code should be in app/domain level
@@ -1,28 +1,26 @@ | |||
Usage: | |||
help [options] [--] [<command_name>] | |||
list [options] [--] [<namespace>] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the fixture, because now, the --help
display the help of the default command.
@@ -1044,6 +1052,18 @@ private function findAlternatives($name, $collection) | |||
public function setDefaultCommand($commandName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding a new argument here that says it is the default but also the only command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And so replace the method I added? It sounds good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I also added a test.
@@ -1039,11 +1047,21 @@ private function findAlternatives($name, $collection) | |||
/** | |||
* Sets the default Command name. | |||
* | |||
* @param string $commandName The Command name | |||
* @param string $commandName The Command name | |||
* @param bool $singleCommand Set to true if there is only one command is this Application |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: in this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
👍 |
*/ | ||
public function setDefaultCommand($commandName) | ||
public function setDefaultCommand($commandName, $singleCommand = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this boolean argument be called $isSingleCommand
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know.... Just tell me ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiereguiluz : so ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion it should be called $isSingleCommand
because $singleCommand
looks like it's asking for the name of that single command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
a7d0fa2
to
02914e6
Compare
Thank you @lyrixx. |
This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Better support for one command app | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9564 | License | MIT Hello; I write many CLI application, and "single command" in cli application is not so easy to write. This is why I propose this patch. IMHO, this PR could replaces #9609. See it in application: ```php #!/usr/bin/env php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; (new Application('echo', '1.0.0')) ->register('echo') ->addArgument('foo', InputArgument::OPTIONAL, 'The directory', 'foo') ->addOption('bar', null, InputOption::VALUE_REQUIRED, 'Foobar', 'bar') ->setCode(function(InputInterface $input, OutputInterface $output) { $output->writeln('start'); $output->writeln($input->getArgument('foo')); $output->writeln($input->getOption('bar')); }) ->getApplication() ->setSingleCommand('echo') ->run(); ``` Some usage: ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php start foo bar ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" start first argument bar ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option" start first argument first option ``` ``` >(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option" --help Usage: echo [options] [--] [<foo>] Arguments: foo The directory [default: "foo"] Options: --bar=BAR Foobar [default: "bar"] -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 ``` Commits ------- 4a9bb1d [Console] Better support for one command app
Does the documentation have been updated, or it is planned to? Otherwise it should. 👍 |
This PR was merged into the master branch. Discussion ---------- Updated single command How to Hi, I've updated "Single command application" tutorial, regarding the recent pull request merged (symfony/symfony#16906). Mickaël Commits ------- 5535c88 Updated single command How to
Hello;
I write many CLI application, and "single command" in cli application is not so easy to write.
This is why I propose this patch. IMHO, this PR could replaces #9609.
See it in application:
Some usage: