Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
jakzal commentedDec 10, 2015
"Unique command" doesn't imply that there's only one command. How about calling it a "single command"? |
lyrixx commentedDec 10, 2015
Indeed ;) I will update my PR. |
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 configuresetUniqueCommand('foobar') and there is nofoobar 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.
lyrixx commentedDec 11, 2015
PR updated ;) |
javiereguiluz commentedDec 11, 2015
I like this proposal. However, I don't like these two lines from the above example: ->getApplication() ->setSingleCommand('echo') Would this other code work? (newApplication()) ->register('echo') ->addArgument('foo', InputArgument::OPTIONAL,'The directory','foo') ->addOption('bar',null, InputOption::VALUE_REQUIRED,'Foobar','bar') ->setCode(function(InputInterface$input,OutputInterface$output) {// ... }) ->run(); |
lyrixx commentedDec 11, 2015
@javiereguiluz I think we can implement that, but it's a bit too magic for me. And we don't want magic in symfony. |
lyrixx commentedDec 11, 2015
What about: |
nicolas-grekas commentedDec 11, 2015
I like it :) |
javiereguiluz commentedDec 11, 2015
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 ( |
lyrixx commentedDec 11, 2015
Then |
javiereguiluz commentedDec 11, 2015
Yes. Would this work? (newApplication()) ->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 |
lyrixx commentedDec 11, 2015
This is a good idea, but a BC break (ref) |
nicolas-grekas commentedDec 11, 2015
|
jvasseur commentedDec 11, 2015
Why not (newSingleCommandApplication()) ->addArgument('foo', InputArgument::OPTIONAL,'The directory','foo') ->addOption('bar',null, InputOption::VALUE_REQUIRED,'Foobar','bar') ->setCode(function(InputInterface$input,OutputInterface$output) {// ... }) ->run(); ? |
stof commentedDec 11, 2015
@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 |
jakzal commentedDec 11, 2015
lyrixx commentedDec 11, 2015
Yes, but whit black magic :-) |
| publicfunctionsetSingleCommand($commandName) | ||
| { | ||
| if (null !==$this->singleCommand) { | ||
| thrownew \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.
| * | ||
| * @param string $commandName The Command name | ||
| */ | ||
| publicfunctionsetDefaultCommand($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.
| * | ||
| * @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
fabpot commentedJun 15, 2016
👍 |
| * @param bool $singleCommand Set to true if there is only one command is this Application | ||
| */ | ||
| publicfunctionsetDefaultCommand($commandName) | ||
| publicfunctionsetDefaultCommand($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 to02914e6Comparefabpot commentedJun 15, 2016
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 | MITHello;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<?phprequire __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.phpstartfoobar``````>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument"startfirst argumentbar``````>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option"startfirst argumentfirst option``````>(3)[{..}eg/dev/github.com/symfony/symfony](console-one-app) php test.php "first argument" --bar="first option" --helpUsage: 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
chalasr commentedJul 7, 2016
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 toHi,I've updated "Single command application" tutorial, regarding the recent pull request merged (symfony/symfony#16906).MickaëlCommits-------5535c88 Updated single command How to
Uh oh!
There was an error while loading.Please reload this page.
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: