cakephp/cakephp · error · Cake\Console\Exception\ConsoleException

Invalid format. Output format can be text or xml.

Error message

Invalid format. Output format can be text or xml.

What it means

ConsoleOptionParser::help($subcommand, $format) only supports 'text' (default) and 'xml' output formats, delegating to HelpFormatter. Any other $format string falls through both checks and throws this ConsoleException.

Solutions

  1. Use only 'text' or 'xml' as the format argument.
  2. Normalize/whitelist the incoming format: strtolower($format) and default to 'text' for unknown values.
  3. For JSON or other output, get the XML/arrays via $formatter->xml() or $parser->options() and transform it yourself.
  4. Upgrade CakePHP if newer versions add formats; otherwise subclass HelpFormatter for custom rendering.

Example fix

// before
$help = $parser->help(null, 'json');

// after
$format = in_array(strtolower($format), ['text', 'xml'], true) ? strtolower($format) : 'text';
$help = $parser->help(null, $format);
Defensive patterns

Strategy: validation

Validate before calling

$allowed = ['text', 'xml'];
if (!in_array($format, $allowed, true)) {
    $format = 'text';
}

Type guard

function normalizeFormat(mixed $format): string {
    return is_string($format) && in_array(strtolower($format), ['text', 'xml'], true)
        ? strtolower($format) : 'text';
}

Try / catch

try {
    $help = $parser->help($subcommand, $format);
} catch (ConsoleException $e) {
    $help = $parser->help($subcommand, 'text');
}

Prevention

When it happens

Trigger: Calling $parser->help(null, $format) (directly or via HelpFormatter-generated help) with $format not equal to 'text' or 'xml', e.g. 'json', 'yaml', or a typo like 'XML' (case-sensitive comparison).

Common situations: Attempts to render CLI help as JSON for machine consumption; passing a user/config-supplied format string straight through; case mistakes like 'Text' or 'XML'.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/3fb1c4092f7becc0. Report an issue: GitHub.

Appendix: source

Thrown at src/Console/ConsoleOptionParser.php:686

     * in the parser.
     *
     * @param string $format Define the output format, can be text or XML
     * @param int $width The width to format user content to. Defaults to 72
     * @return string Generated help.
     */
    public function help(string $format = 'text', int $width = 72): string
    {
        $formatter = new HelpFormatter($this);
        $formatter->setAlias($this->rootName);

        if ($format === 'text') {
            return $formatter->text($width);
        }
        if ($format === 'xml') {
            return (string)$formatter->xml();
        }

        throw new ConsoleException('Invalid format. Output format can be text or xml.');
    }

    /**
     * Set the root name used in the HelpFormatter
     *
     * @param string $name The root command name
     * @return $this
     */
    public function setRootName(string $name)
    {
        $this->rootName = $name;

        return $this;
    }

    /**
     * Parse the value for a long option out of $this->_tokens. Will handle
     * options with an `=` in them.

View on GitHub (pinned to 1128eba9b0)