cvs: peardoc /en/package/console/console-commandline examples.xml

From: Date: Sat, 06 Dec 2008 12:20:24 +0000
Subject: cvs: peardoc /en/package/console/console-commandline examples.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-11306@lists.php.net to get a copy of this message
izi Sat Dec 6 12:20:24 2008 UTC Modified files: /peardoc/en/package/console/console-commandline examples.xml Log: Added examples for sub-commands. http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/examples.xml?r1=1.5&r2=1.6&diff_format=u Index: peardoc/en/package/console/console-commandline/examples.xml diff -u peardoc/en/package/console/console-commandline/examples.xml:1.5 peardoc/en/package/console/console-commandline/examples.xml:1.6 --- peardoc/en/package/console/console-commandline/examples.xml:1.5 Thu Oct 9 15:16:21 2008 +++ peardoc/en/package/console/console-commandline/examples.xml Sat Dec 6 12:20:24 2008 @@ -136,4 +136,167 @@ </example> </para> </refsection> + + <refsection xml:id="package.console.console-commandline.examples.ex3"><info><title>Sub-commands example</title></info> + + <para> + <example><info><title>Sub-commands example</title></info> + + <programlisting role="php"> + <![CDATA[ +<?php +// Include the Console_CommandLine package. +require_once 'Console/CommandLine.php'; + +// create the parser +$parser = new Console_CommandLine(array( + 'description' => 'A great program that can foo and bar !', + 'version' => '1.0.0' +)); + +// add a global option to make the program verbose +$parser->addOption('verbose', array( + 'short_name' => '-v', + 'long_name' => '--verbose', + 'action' => 'StoreTrue', + 'description' => 'turn on verbose output' +)); + +// add the foo subcommand +$foo_cmd = $parser->addCommand('foo', array( + 'description' => 'output the given string with a foo prefix' +)); +$foo_cmd->addOption('reverse', array( + 'short_name' => '-r', + 'long_name' => '--reverse', + 'action' => 'StoreTrue', + 'description' => 'reverse the given string before echoing it' +)); +$foo_cmd->addArgument('text', array( + 'description' => 'the text to output' +)); + +// add the bar subcommand +$bar_cmd = $parser->addCommand('bar', array( + 'description' => 'output the given string with a bar prefix' +)); +$bar_cmd->addOption('reverse', array( + 'short_name' => '-r', + 'long_name' => '--reverse', + 'action' => 'StoreTrue', + 'description' => 'reverse the given string before echoing it' +)); +$bar_cmd->addArgument('text', array( + 'description' => 'the text to output' +)); + +// run the parser +try { + $result = $parser->parse(); + if ($result->command_name) { + $st = $result->command->options['reverse'] + ? strrev($result->command->args['text']) + : $result->command->args['text']; + if ($result->command_name == 'foo') { + echo "Foo says: $st\n"; + } else if ($result->command_name == 'bar') { + echo "Bar says: $st\n"; + } + } +} catch (Exception $exc) { + $parser->displayError($exc->getMessage()); +} + +?> + ]]> + </programlisting> + </example> + </para> + </refsection> + <refsection xml:id="package.console.console-commandline.examples.ex4"><info><title> + Sub-commands example using an <acronym>XML</acronym> definition file + </title></info> + + <para> + <example><info><title>Sub-commands example (<acronym>XML</acronym> file)</title></info> + + <programlisting role="xml"> + <![CDATA[ +<?xml version="1.0" encoding="utf-8" standalone="yes"?> +<command> + <description>A great program that can foo and bar !</description> + <version>1.0.0</version> + <option name="verbose"> + <short_name>-v</short_name> + <long_name>--verbose</long_name> + <description>turn on verbose output</description> + <action>StoreTrue</action> + </option> + <command> + <name>foo</name> + <description>output the given string with a foo prefix</description> + <option name="reverse"> + <short_name>-r</short_name> + <long_name>--reverse</long_name> + <description>reverse the string before echoing it</description> + <action>StoreTrue</action> + </option> + <argument name="text"> + <description>the text to output</description> + </argument> + </command> + <command> + <name>bar</name> + <description>output the given string with a bar prefix</description> + <option name="reverse"> + <short_name>-r</short_name> + <long_name>--reverse</long_name> + <description>reverse the string before echoing it</description> + <action>StoreTrue</action> + </option> + <argument name="text"> + <description>the text to output</description> + </argument> + </command> +</command> + ]]> + </programlisting> + </example> + </para> + <para> + <example><info><title>Sub-commands example (<acronym>PHP</acronym> file)</title></info> + + <programlisting role="php"> + <![CDATA[ +<?php +// Include the Console_CommandLine package. +require_once 'Console/CommandLine.php'; + +// create the parser +$xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex4.xml'; +$parser = Console_CommandLine::fromXmlFile($xmlfile); + +// run the parser +try { + $result = $parser->parse(); + if ($result->command_name) { + $st = $result->command->options['reverse'] + ? strrev($result->command->args['text']) + : $result->command->args['text']; + if ($result->command_name == 'foo') { + echo "Foo says: $st\n"; + } else if ($result->command_name == 'bar') { + echo "Bar says: $st\n"; + } + } +} catch (Exception $exc) { + $parser->displayError($exc->getMessage()); +} + +?> + ]]> + </programlisting> + </example> + </para> + </refsection> </refentry>

« previous php.pear.doc (#11306) next »