Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Created July 9, 2022 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/6610796d44dcf554f1d7a2c8d82e8998 to your computer and use it in GitHub Desktop.
Save mtvbrianking/6610796d44dcf554f1d7a2c8d82e8998 to your computer and use it in GitHub Desktop.
Symfony Command Allow Extra Non Validated Options
  namespace Symfony\Component\Console\Input;
  
  use Symfony\Component\Console\Exception\RuntimeException;
  
  class ArgvInput extends Input
  {
      protected function parse()
      {
          $parseOptions = true;
          $this->parsed = $this->tokens;
  
+         if (in_array('--', $this->tokens)) { // if $this->allowsExtraArgs() && $this->hasExtraArgs()
+             $this->parsed = array_slice($this->tokens, 0, array_search('--', $this->tokens));
+ 
+             $extra = array_slice($this->tokens, array_search('--', $this->tokens) + 1);
+ 
+             $this->options['--command'] = implode(' ', $extra);
+         }
  
          while (null !== $token = array_shift($this->parsed)) {
              $parseOptions = $this->parseToken($token, $parseOptions);
          }
      }
  }
use Symfony\Component\Console\Command\Command;

class Demo extends Command
{
    protected function configure(): void
    {
        $this->setDefinition(
            new InputDefinition([
                // ...
                // new InputOption('--command', '-c', InputOption::VALUE_OPTIONAL, 'Raw Command'),
            ])
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
    	dd($input->getOption('--command')); // cmd arg --opt=ok
    }
}
php artisan xml:lint in.xml --noout -- cmd arg --opt=ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment