-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console Component] Make creating single command app easier #9564
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
Comments
not sure if you saw the other discussions going on with this, but maybe this is different, but doubt it 👶 |
@cordoval I think this could be possible, as it is the proper way to do it. It is the way used in Behat for instance (the implementation is a bit different, but the idea is the same) The previous discussion was trying to make the command name optional while keeping the possibility to have multiple commands in the application, which was causing lots of issues. |
@cordoval you mean #3857 ? Yes, I saw it, but that one tries to change Application's behaviour regarding "no command given" cases, which is not the right way, as @stof mentioned. I also read #4104, but with that one you lose the features of an Application (error handling, help, ...). My proposal is to add another class (subclassing from Application to inherit all the good stuff) which expects you to define (only) one command (through the constructor), being the single publicly accessible command, for which a command argument would be silly and thus can be dropped. |
@soxofaan please open a PR for your proposal I see one improvement compared to your code: it should prevent adding new commands by throwing an exception in |
I thought about that, but it still might be interesting to add multiple commands which still can be called internally from the "main" command ( |
I started implementing at https://github.com/soxofaan/symfony/compare/ticket9564-console-single-command-app Not completely ready to make a PR:
but I'd want that it does not show I'm still figuring out if that is possible to to in a clean way |
Could you please open a |
sure: #9609 |
Added SingleCommandApplication to simplify creating and using Applications that only provide one Command.
Added SingleCommandApplication to simplify creating and using Applications that only provide one Command.
Added SingleCommandApplication to simplify creating and using Applications that only provide one Command.
Added SingleCommandApplication to simplify creating and using Applications that only provide one Command.
Added SingleCommandApplication to simplify creating and using Applications that only provide one Command.
Is this feature going to become part of Symfony? |
Check Symfony docs: Building a single Command Application It's nicely explained there |
Closing this issue because the related PR was closed in favor of the newer proposal explained in #16906. |
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
Hi,
(first attempt at contributing to symfony here)
I was reading on creating single command applications with Symfony console (because that's what I mostly need) and that felt a bit cumbersome/too much boilerplate.
Would there be interest in including something like the helper class below in the
Symfony\Component\Console
namespace, to make creating single command applications easier?Usage example (based on http://symfony.com/doc/current/components/console/introduction.html):
I omitted docblocks and such to keep it to the point.
I just wanted to collect some quick feedback on this idea before making this a real pull request.
thanks.
The text was updated successfully, but these errors were encountered: