How to run a custom command create in composer.json with options?

Viewed 63

I'd like to use the Codeception bin file in project A from project B, so in project B's composer.json file I have added this script:

"codecept": "../projectA/vendor/bin/codecept"

With this, I can run a command from project B such as:

composer codecept bootstrap 

However, if I add an option that can be used with the bootstrap command, such as:

composer codecept bootstrap --empty 

Composer throws with the following error:

[Symfony\Component\Console\Exception\RuntimeException]  
The "--empty" option does not exist. 

This is my first time playing around with custom commands, so I'm not sure what I'm missing.

2 Answers

Okay, I found it, have to use an extra -- before passing the option, like this:

composer codecept bootstrap -- --empty 

The error you see is from Symfony Console that is a dependency of Composer.

The underlying problem is that the command-line argument parser in Symfony Console is flawed (read: limited).

You can work-around that in many cases - but not for all - by using the -- argument delimiter and adding the commands arguments behind. E.g.:

composer codecept bootstrap -- --empty 

However this does only work for command-line options that do not exist in Symfony Console as global arguments. E.g. this won't work for --help, it will show Composers' help text. Or --version, or --quiet and so on and so forth, e.g.:

composer exec codecept-the-foo-baloo bootstrap -- --version
Composer version ...

The command/package must not even be installed. As written this is flawed and introduces such and similar kind of limitation(s).

Related