/** * Gets formatted help for this parser object. * Generates help text based on the description, options, arguments, subcommands and epilog * in the parser. * * @param string $subcommand If present and a valid subcommand that has a linked parser. * That subcommands help will be shown instead. * @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($subcommand = null, $format = 'text', $width = 72) { if (isset($this->_subcommands[$subcommand]) && $this->_subcommands[$subcommand]->parser() instanceof self) { $subparser = $this->_subcommands[$subcommand]->parser(); $subparser->command($this->command() . ' ' . $subparser->command()); return $subparser->help(null, $format, $width); } $formatter = new HelpFormatter($this); if ($format === 'text' || $format === true) { return $formatter->text($width); } elseif ($format === 'xml') { return $formatter->xml(); } }
/** * Test that a long set of arguments doesn't make useless output. * * @return void */ public function testHelpWithLotsOfArguments() { $parser = new ConsoleOptionParser('mycommand', false); $parser->addArgument('test', array('help' => 'A test option.'))->addArgument('test2', array('help' => 'A test option.'))->addArgument('test3', array('help' => 'A test option.'))->addArgument('test4', array('help' => 'A test option.'))->addArgument('test5', array('help' => 'A test option.'))->addArgument('test6', array('help' => 'A test option.'))->addArgument('test7', array('help' => 'A test option.'))->addArgument('model', array('help' => 'The model to make.', 'required' => true))->addArgument('other_longer', array('help' => 'Another argument.')); $formatter = new HelpFormatter($parser); $result = $formatter->text(); $expected = 'cake mycommand [-h] [arguments]'; $this->assertContains($expected, $result); }
/** * test getting help with defined options. * * @return void */ public function testHelpWithOptionsAndArguments() { $parser = new ConsoleOptionParser('mycommand', false); $parser->addOption('test', array('help' => 'A test option.'))->addArgument('model', array('help' => 'The model to make.', 'required' => true))->addArgument('other_longer', array('help' => 'Another argument.')); $formatter = new HelpFormatter($parser); $result = $formatter->text(); $expected = <<<TEXT <info>Usage:</info> cake mycommand [-h] [--test] <model> [<other_longer>] <info>Options:</info> --help, -h Display this help. --test A test option. <info>Arguments:</info> model The model to make. other_longer Another argument. <comment>(optional)</comment> TEXT; $this->assertEquals($expected, $result, 'Help does not match'); }