symfony: Console: Running default command causes invalid hasAnArrayArgument state

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.4.2

Got an exception

When I run php my-script.php. Seems like a bug to me because no matter how I order the arguments the problem still occurs.

Expected:

Not enough arguments (missing: “invoice-id, customer”).

Actual:

Cannot add a required argument after an optional one.

Caused by Symfony\Component\Console\Input\InputDefinition::addArgument.

<?php


namespace Mite\Console;


use Mite\Mite;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PdfCommand extends Command {
    protected static $defaultName = 'mite:pdf';

    protected function configure() {
        // $this->addOption( 'month', 'm', InputOption::VALUE_OPTIONAL, 'Month which shall be invoiced', date( 'm' ) );

        $this->addArgument( 'invoice-id', InputArgument::REQUIRED, 'ID of the invoice' );
        $this->addArgument( 'customer', InputArgument::REQUIRED, 'Name of the customer as stored in mite' );

        $this->addOption( 'month', 'm', InputOption::VALUE_OPTIONAL, 'Month which shall be invoiced', date( 'm' ) );
    }

    protected function execute( InputInterface $input, OutputInterface $output ) {

    }
}

app.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

$app = new \Symfony\Component\Console\Application( 'mite-pdf' );

$app->addCommands( [
	new \Mite\Console\PdfCommand(),
] );

$app->setDefaultCommand('mite:pdf');

$app->run();

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

I think what @sroze means is that the first passed argument would be considered as the command name, that would fail. Still have to dig into it but I think he is right, meaning that arguments just don’t work for a default command. I’ll have a look asap if nobody does.