cvs: peardoc /en/package/console console-commandline.xml /en/package/console/console-commandline arguments.xml creating-the-parser.xml examples.xml
extending.xml intro.xml options.xml populating-the-parser.xml sub-commands.xml

From: Date: Sat, 06 Dec 2008 15:01:01 +0000
Subject: cvs: peardoc /en/package/console console-commandline.xml /en/package/console/console-commandline arguments.xml creating-the-parser.xml examples.xml
extending.xml intro.xml options.xml populating-the-parser.xml sub-commands.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-11307@lists.php.net to get a copy of this message
izi Sat Dec 6 15:01:01 2008 UTC Added files: /peardoc/en/package/console/console-commandline arguments.xml options.xml sub-commands.xml Removed files: /peardoc/en/package/console/console-commandline populating-the-parser.xml Modified files: /peardoc/en/package/console console-commandline.xml /peardoc/en/package/console/console-commandline creating-the-parser.xml examples.xml extending.xml intro.xml Log: * updated doc with new features, * wrote sub-commands doc, * splitted "populating-the-parser" in three separated sections (options, arguments and sub-commands), * some cosmetic changes.

http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline.xml?r1=1.9&r2=1.10&diff_format=u Index: peardoc/en/package/console/console-commandline.xml diff -u peardoc/en/package/console/console-commandline.xml:1.9 peardoc/en/package/console/console-commandline.xml:1.10 --- peardoc/en/package/console/console-commandline.xml:1.9 Thu Oct 9 22:35:22 2008 +++ peardoc/en/package/console/console-commandline.xml Sat Dec 6 15:01:01 2008 @@ -1,14 +1,20 @@ <?xml version="1.0" encoding="utf-8"?> -<book xmlns="http://docbook.org/ns/docbook" version="lillet" xml:id="package.console.console-commandline"> +<book + xmlns="http://docbook.org/ns/docbook" + xmlns:phd="http://www.php.net/ns/phd" + version="lillet" + xml:id="package.console.console-commandline" +> <info> <title>Console_CommandLine</title> <abstract> <para> - <classname>Console_CommandLine</classname> is a full featured package for - managing command line options and arguments highly inspired from the + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> + is a full featured package for managing command line options and arguments + highly inspired from the <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&url.python.doc.modindex.optparse;">python - optparse module</link>, it allows the developer to easily build - complex command line interfaces. + optparse module</link>, it allows the developer to easily build complex + command line interfaces. </para> </abstract> </info> @@ -16,7 +22,9 @@ <chapter> &package.console.console-commandline.intro; &package.console.console-commandline.creating-the-parser; - &package.console.console-commandline.populating-the-parser; + &package.console.console-commandline.options; + &package.console.console-commandline.arguments; + &package.console.console-commandline.sub-commands; &package.console.console-commandline.extending; &package.console.console-commandline.examples; </chapter> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/creating-the-parser.xml?r1=1.9&r2=1.10&diff_format=u Index: peardoc/en/package/console/console-commandline/creating-the-parser.xml diff -u peardoc/en/package/console/console-commandline/creating-the-parser.xml:1.9 peardoc/en/package/console/console-commandline/creating-the-parser.xml:1.10 --- peardoc/en/package/console/console-commandline/creating-the-parser.xml:1.9 Wed Nov 12 22:07:09 2008 +++ peardoc/en/package/console/console-commandline/creating-the-parser.xml Sat Dec 6 15:01:01 2008 @@ -2,7 +2,8 @@ <refentry xmlns="http://docbook.org/ns/docbook" xmlns:phd="http://www.php.net/ns/phd" - version="lillet" xml:id="package.console.console-commandline.creating-the-parser" + version="lillet" + xml:id="package.console.console-commandline.creating-the-parser" > <refnamediv> <refname>Creating the parser</refname> @@ -13,8 +14,9 @@ </refnamediv> <refsection xml:id="package.console.console-commandline.creating-the-parser.intro"><info><title><classname>Console_CommandLine</classname> constructor</title></info> - <para><classname>Console_CommandLine</classname> constructor takes an - optional array of parameters explained in the table below. + <para> + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::__construct"/> + takes an optional array of parameters explained in the table below. Note that if you are using an <acronym>XML</acronym> definition file, you can pass these parameters in it (see <acronym>XML</acronym> example for details). @@ -82,8 +84,8 @@ <refsection xml:id="package.console.console-commandline.creating-the-parser.php"><info><title>Using <acronym>PHP</acronym> code</title></info> <para>The examples below demonstrate how to instanciate - <classname>Console_CommandLine</classname> and build a parser using - <acronym>PHP</acronym> code. + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> + and build a parser using <acronym>PHP</acronym> code. </para> <para> <example><info><title>The simplest way</title></info> @@ -120,10 +122,10 @@ <refsection xml:id="package.console.console-commandline.creating-the-parser.xml"><info><title>Using an <acronym>XML</acronym> definition file</title></info> <para>The examples below demonstrate how to instanciate - <classname>Console_CommandLine</classname> and build a parser using an - <acronym>XML</acronym> definition file, this can be very useful if you - have a big program or if you need to reuse your user interface settings - for a web frontend for example. + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> + and build a parser using an <acronym>XML</acronym> definition file, this + can be very useful if you have a big program or if you need to reuse your + user interface settings for a web frontend for example. <example><info><title>The <acronym>XML</acronym> file</title></info> <programlisting role="xml"> @@ -154,9 +156,9 @@ <note> <para> Using an <acronym>XML</acronym> string you would have called - <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::fromXmlString">fromXmlString</phd:pearapi> + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::fromXmlString"/> instead of - <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::fromXmlFile">fromXmlFile</phd:pearapi> + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::fromXmlFile"/> of course. </para> </note> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/examples.xml?r1=1.6&r2=1.7&diff_format=u Index: peardoc/en/package/console/console-commandline/examples.xml diff -u peardoc/en/package/console/console-commandline/examples.xml:1.6 peardoc/en/package/console/console-commandline/examples.xml:1.7 --- peardoc/en/package/console/console-commandline/examples.xml:1.6 Sat Dec 6 12:20:24 2008 +++ peardoc/en/package/console/console-commandline/examples.xml Sat Dec 6 15:01:01 2008 @@ -1,5 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> -<refentry xmlns="http://docbook.org/ns/docbook" version="lillet" xml:id="package.console.console-commandline.examples"> +<refentry + xmlns="http://docbook.org/ns/docbook" + xmlns:phd="http://www.php.net/ns/phd" + version="lillet" + xml:id="package.console.console-commandline.examples" +> <refnamediv> <refname>Examples</refname> <refpurpose>various code examples</refpurpose> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/extending.xml?r1=1.4&r2=1.5&diff_format=u Index: peardoc/en/package/console/console-commandline/extending.xml diff -u peardoc/en/package/console/console-commandline/extending.xml:1.4 peardoc/en/package/console/console-commandline/extending.xml:1.5 --- peardoc/en/package/console/console-commandline/extending.xml:1.4 Thu Oct 9 15:16:21 2008 +++ peardoc/en/package/console/console-commandline/extending.xml Sat Dec 6 15:01:01 2008 @@ -1,5 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> -<refentry xmlns="http://docbook.org/ns/docbook" version="lillet" xml:id="package.console.console-commandline.extending"> +<refentry + xmlns="http://docbook.org/ns/docbook" + xmlns:phd="http://www.php.net/ns/phd" + version="lillet" + xml:id="package.console.console-commandline.extending" +> <refnamediv> <refname>Extending Console_CommandLine</refname> <refpurpose>how to customize this package to achieve specific tasks</refpurpose> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/intro.xml?r1=1.7&r2=1.8&diff_format=u Index: peardoc/en/package/console/console-commandline/intro.xml diff -u peardoc/en/package/console/console-commandline/intro.xml:1.7 peardoc/en/package/console/console-commandline/intro.xml:1.8 --- peardoc/en/package/console/console-commandline/intro.xml:1.7 Thu Oct 9 15:16:21 2008 +++ peardoc/en/package/console/console-commandline/intro.xml Sat Dec 6 15:01:01 2008 @@ -1,5 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> -<refentry xmlns="http://docbook.org/ns/docbook" version="lillet" xml:id="package.console.console-commandline.intro"> +<refentry + xmlns="http://docbook.org/ns/docbook" + xmlns:phd="http://www.php.net/ns/phd" + version="lillet" + xml:id="package.console.console-commandline.intro" +> <refnamediv> <refname>Introduction</refname> <refpurpose>introduction to the <classname>Console_CommandLine</classname> @@ -8,16 +13,17 @@ <refsection><info><title>Introduction</title></info> <para> - <classname>Console_CommandLine</classname> is a convenient, flexible, and - powerful library for parsing command-line options and arguments. + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> + is a convenient, flexible, and powerful library for parsing command-line + options and arguments. </para> <para> It uses adeclarative style of command-line parsing: you create an instance of Console_CommandLine, populate it with options, arguments and even sub-commands and parse the command line. - <classname>Console_CommandLine</classname> allows users to specify options - in the conventional GNU/POSIX syntax, and additionally generates usage and - help messages for you. + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> + allows users to specify options in the conventional GNU/POSIX syntax, and + additionally generates usage and help messages for you. </para> <para> DISCLAIMER: since Console_CommandLine was highly inspired from the @@ -73,9 +79,12 @@ </para> <para> As it parses the command line, Console_CommandLine sets attributes of the - result object returned by <function>parse()</function> method based on - user-supplied command-line values. - When <function>parse()</function> returns from parsing this command line, + result object returned by + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::parse"/> + method based on user-supplied command-line values. + When + <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::parse"/> + returns from parsing this command line, <varname>$result-&gt;options['filename']</varname> will be <quote>outfile</quote> and <varname>$result-&gt;options['quiet']</varname> will be &true;. http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/arguments.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/arguments.xml +++ peardoc/en/package/console/console-commandline/arguments.xml <?xml version="1.0" encoding="utf-8"?> <refentry xmlns="http://docbook.org/ns/docbook" xmlns:phd="http://www.php.net/ns/phd" version="lillet" xml:id="package.console.console-commandline.arguments" > <refnamediv> <refname>Managing command line arguments</refname> <refpurpose> how to add arguments to the parser and use them </refpurpose> </refnamediv> <refsection xml:id="package.console.console-commandline.arguments.background"><info><title>Some background</title></info> <para> Arguments are for those pieces of information that your program absolutely, positively requires to run. </para> <para> A good user interface should have as few absolute requirements as possible. If your program requires 17 distinct pieces of information in order to run successfully, it doesn't much matter how you get that information from the user, most people will give up and walk away before they successfully run the program. This applies whether the user interface is a command-line, a configuration file, or a GUI: if you make that many demands on your users, most of them will simply give up. </para> <para> In short, try to minimize the amount of information that users are absolutely required to supply and use sensible defaults whenever possible. Of course, you also want to make your programs reasonably flexible. That's what options are for. </para> </refsection> <refsection xml:id="package.console.console-commandline.arguments.adding"><info><title>Adding arguments with <classname>Console_CommandLine</classname></title></info> <para> To add arguments to your parser, just create the parser as explained in the previous section and use the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::addArgument"/> method. </para> <para> The <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::addArgument"/> method takes two arguments: <itemizedlist> <listitem> <simpara>the argument name: a string that will be used to access the argument in the result object. For example if you name your argument foo, you will access to the result object like this: <varname>$result-&gt;args['foo']</varname>;</simpara> </listitem> <listitem> <simpara>the arguments parameters: an array of various informations as explained below.</simpara> </listitem> </itemizedlist> </para> <para> <table><title>Available arguments parameters</title> <tgroup cols="5"> <thead> <row> <entry>name</entry> <entry>type</entry> <entry>required</entry> <entry>description</entry> <entry>example</entry> </row> </thead> <tbody> <row> <entry>description</entry> <entry>string</entry> <entry>no, but recommended for the help message</entry> <entry>a description for the argument</entry> <entry>entry list of input files separated by spaces</entry> </row> <row> <entry>multiple</entry> <entry>boolean</entry> <entry>no, default to &false;</entry> <entry>tells the parser that the argument expects multiples values</entry> <entry>&true;</entry> </row> <row> <entry>optional</entry> <entry>boolean</entry> <entry>no, default to &false;</entry> <entry>tells the parser that the argument is optional</entry> <entry>&true;</entry> </row> <row> <entry>help_name</entry> <entry>string</entry> <entry>no, if not given it will default to the argument name</entry> <entry>the name to display in the argument help line</entry> <entry>files</entry> </row> </tbody> </tgroup> </table> </para> <para> <example><info><title>Adding commandline arguments</title></info> <programlisting role="php"> <![CDATA[ <?php require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); // add an array argument $parser->addArgument('input_files', array('multiple'=>true)); // add a simple argument $parser->addArgument('output_file'); try { $result = $parser->parse(); print_r($result->args); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ?> ]]> </programlisting> </example> </para> <para>If the user type:</para> <para> <programlisting role="text"> <![CDATA[ $ <yourprogram> file1 file2 file3 ]]> </programlisting> </para> <para>The output of the above script will be:</para> <para> <programlisting role="text"> <![CDATA[ Array ( [input_files] => Array ( [0] => file1 [1] => file2 ) [output_file] => file3 ) ]]> </programlisting> </para> </refsection> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/options.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/options.xml +++ peardoc/en/package/console/console-commandline/options.xml <?xml version="1.0" encoding="utf-8"?> <refentry xmlns="http://docbook.org/ns/docbook" xmlns:phd="http://www.php.net/ns/phd" version="lillet" xml:id="package.console.console-commandline.options" > <refnamediv> <refname>Managing command line options</refname> <refpurpose> how to add options to the parser and use them. </refpurpose> </refnamediv> <refsection xml:id="package.console.console-commandline.options.background"><info><title>Some background</title></info> <para> Options are used to provide extra information to tune or customize the execution of a program. In case it wasn't clear, options are usually optional. A program should be able to run just fine with no options whatsoever. Pick a random program from the Unix or GNU toolsets. Can it run without any options at all and still make sense? The main exceptions are find, tar, and dd--all of which are mutant oddballs that have been rightly criticized for their non-standard syntax and confusing interfaces. </para> <para> Lots of people want their programs to have "required options". Think about it. If it's required, then it's not optional! If there is a piece of information that your program absolutely requires in order to run successfully, that's what arguments are for. </para> <para> As an example of good command-line interface design, consider the humble cp utility, for copying files. It doesn't make much sense to try to copy files without supplying a destination and at least one source. Hence, cp fails if you run it with no arguments. However, it has a flexible, useful syntax that does not require any options at all: <programlisting role="text"> <![CDATA[ $ cp SOURCE DEST $ cp SOURCE ... DEST-DIR ]]> </programlisting> </para> <para> You can get pretty far with just that. Most cp implementations provide a bunch of options to tweak exactly how the files are copied: you can preserve mode and modification time, avoid following symlinks, ask before clobbering existing files, etc. But none of this distracts from the core mission of cp, which is to copy either one file to another, or several files to another directory. </para> </refsection> <refsection xml:id="package.console.console-commandline.options.adding"><info><title>Adding options with <classname>Console_CommandLine</classname></title></info> <para> To add options to your parser, just create the parser as explained in the previous section and use the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::addOption"/> method. </para> <para> The <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::addOption"/> method takes two arguments: <itemizedlist> <listitem> <simpara>the option name: a string that will be used to access the option in the result object. For example if you name your option foo, you will access to the result object like this: <varname>$result-&gt;options['foo']</varname>;</simpara> </listitem> <listitem> <simpara>the options parameters: an array of various informations, as explained below.</simpara> </listitem> </itemizedlist> <table><title>Available option parameters</title> <tgroup cols="5"> <thead> <row> <entry>name</entry> <entry>type</entry> <entry>required</entry> <entry>description</entry> <entry>example</entry> </row> </thead> <tbody> <row> <entry>short_name</entry> <entry>string</entry> <entry>yes, if no long_name given</entry> <entry>the option short name</entry> <entry>-o</entry> </row> <row> <entry>long_name</entry> <entry>string</entry> <entry>yes, if no short_name given</entry> <entry>the option long name</entry> <entry>--orientation</entry> </row> <row> <entry>description</entry> <entry>string</entry> <entry>no, but recommended for the help message</entry> <entry>a description for the option</entry> <entry>orientation of the page: ltr (default) or rtl</entry> </row> <row> <entry>action</entry> <entry>string</entry> <entry>no, default to <quote>StoreString</quote></entry> <entry>the option action (see next section for details)</entry> <entry>StoreString</entry> </row> <row> <entry>default</entry> <entry>mixed</entry> <entry>no</entry> <entry>the option default value</entry> <entry>ltr</entry> </row> <row> <entry>choices</entry> <entry>array</entry> <entry>no, only relevant for options that expect argument(s)</entry> <entry>list of possible values for the option</entry> <entry>array('ltr', 'rtl')</entry> </row> <row> <entry>list</entry> <entry>array</entry> <entry>no, only relevant for options that use the "List" action</entry> <entry>list of values to display</entry> <entry>array('blue', 'green', 'yellow')</entry> </row> <row> <entry>add_list_option</entry> <entry>boolean</entry> <entry>no, default to &false;</entry> <entry>this property is only relevant when the choices property is set, if set to &true; the parser will generate an additional option to list the choices (eg. --list-foo).</entry> <entry>&true;</entry> </row> <row> <entry>help_name</entry> <entry>string</entry> <entry>no, if not given it will default to the option name</entry> <entry>the name to display in the option help line</entry> <entry>page_orientation (the help ligne will be: --orientation=page_orientation)</entry> </row> </tbody> </tgroup> </table> </para> <para> <example><info><title>Adding commandline options</title></info> <programlisting role="php"> <![CDATA[ <?php require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(array( 'description' => 'A useful description for your program.', 'version' => '0.0.1', // the version of your program )); // Adding a simple option that takes no argument and that tell our program to // turn on verbose output: $parser->addOption( 'verbose', array( 'short_name' => '-v', 'long_name' => '--verbose', 'description' => 'turn on verbose output', 'action' => 'StoreTrue' ) ); // Adding an option that will store a string $parser->addOption( 'orientation', array( 'short_name' => '-o', 'long_name' => '--orientation', 'description' => 'orientation of the page, "ltr" (default) or "rtl"', 'action' => 'StoreString', 'default' => 'ltr', 'help_name' => 'page_orientation' ) ); try { $result = $parser->parse(); print_r($result->options); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ?> ]]> </programlisting> </example> </para> <para>Now if the user type:</para> <para> <programlisting role="text"> <![CDATA[ $ <yourprogram> -vo rtl ]]> </programlisting> </para> <para>or (equivalent):</para> <para> <programlisting role="text"> <![CDATA[ $ <yourprogram> --verbose --orientation=rtl ]]> </programlisting> </para> <para>The output of the above script will be:</para> <para> <programlisting role="text"> <![CDATA[ Array ( [verbose] => 1 [orientation] => rtl [help] => [version] => ) ]]> </programlisting> </para> </refsection> <refsection xml:id="package.console.console-commandline.options.actions"><info><title>Options actions</title></info> <para> Actions tell the parser how to handle option values, among other things they tell the parser if the option expects a value or not and how to store this value in the result object. </para> <refsection><info><title>StoreTrue</title></info> <para>This action tells the parser to store the value true in the result object if the option is present in the command line, for example: </para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -v</programlisting> </para> <para> will store &true; in <varname>$result-&gt;options['verbose']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('verbose', array('short_name'=>'-v', 'action'=>'StoreTrue')); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>StoreFalse</title></info> <para>This action tells the parser to store the value false in the result object if the option is present in the command line, for example:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -q</programlisting> </para> <para> will store &false; in <varname>$result-&gt;options['verbose']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('verbose', array('short_name'=>'-q', 'action'=>'StoreFalse')); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>StoreString</title></info> <para>This action tells the parser that the option expects a value and to store this value as an integer in the result object, for example:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -o out.txt</programlisting> </para> <para> will store the string <quote>out.txt</quote> in <varname>$result-&gt;options['outfile']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('outfile', array('short_name'=>'-o', 'action'=>'StoreString')); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>StoreInt</title></info> <para>This action tells the parser that the option expects a value and to store this value as an integer in the result object, for example:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; --width=500</programlisting> </para> <para> will store the integer 500 in <varname>$result-&gt;options['width']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('width', array('long_name'=>'--width', 'action'=>'StoreInt')); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>StoreFloat</title></info> <para>This action tells the parser that the option expects a value and to store this value as a float in the result object, for example:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -l=0.3</programlisting> </para> <para> will store the float 0.3 in <varname>$result-&gt;options['level']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('level', array('short_name'=>'-l', 'action'=>'StoreFloat')); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>Counter</title></info> <para>This action tells the parser to increment the value in the result object each time it encounters the option in the command line, for example:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -vvvv</programlisting> </para> <para>or the equivalent:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -v -v -v --verbose</programlisting> </para> <para> will store the integer 4 in <varname>$result-&gt;options['verbose_level']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('verbose_level', array( 'short_name' => '-v', 'long_name' => '--verbose', 'action' => 'Counter' )); $result = $parser->parse(); ?> ]]> </programlisting> </para> </refsection> <refsection><info><title>Help</title></info> <para>This action tells the parser to display the help message if it encounters the option in the command line, most of the time you won't need this since it is handled by <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> internally. </para> </refsection> <refsection><info><title>Version</title></info> <para>This action tells the parser to display the program version if it encounters the option in the command line, as for Help action, chances are that you won't need this since it is handled by <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> internally. </para> </refsection> <refsection><info><title>Password</title></info> <para>This action allows the user to either type the password on the commandline or to be prompted for it (will not echo on unix systems), some examples:</para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('password', array('short_name'=>'-p', 'action'=>'Password')); $result = $parser->parse(); ?> ]]> </programlisting> </para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -ps3cret</programlisting> </para> <para> will store the string <quote>s3ecret</quote> in <varname>$result-&gt;options['password']</varname> </para> <para>whereas:</para> <para> <programlisting role="text">$ &lt;yourprogram&gt; -p</programlisting> </para> <para> will <quote>prompt</quote> the user for entering his/her password without echoing it, and will store <quote>s3ecret</quote> in <varname>$result-&gt;options['password']</varname> </para> </refsection> <refsection><info><title>Callback</title></info> <para>This action allows to specify a <acronym>PHP</acronym> callback to handle user input. The callback must be a php callable and must accept five arguments:</para> <itemizedlist> <listitem> <simpara>the value of the option;</simpara> </listitem> <listitem> <simpara>the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Option_Callback"/> instance</simpara> </listitem> <listitem> <simpara>the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Result"/> instance</simpara> </listitem> <listitem> <simpara>the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine"/> instance (the parser)</simpara> </listitem> <listitem> <simpara>an array of additional parameters that you specify when building your option.</simpara> </listitem> </itemizedlist> <para>Your callback function must return the modified (or not modified) value (the first argument).</para> <para>All these arguments should give you enough flexibility to build complex callback actions.</para> <para>Here is a simple example:</para> <para> <programlisting role="php"> <![CDATA[ <?php /** * A simple encryption callback. * */ function encryptCallback($value, $option, $result, $parser, $params=array()) { if (!isset($params['salt'])) { $params['salt'] = ''; } return sha1($params['salt'] . $value); } require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); $parser->addOption('encrypt', array( 'short_name' => '-e', 'long_name' => '--encrypt', 'action' => 'Callback', 'description' => 'encrypt the given value using sha1 + salt', 'callback' => 'encryptCallback', 'action_params' => array('salt' => 'x2897ZHKx7200j1__2') )); try { $result = $parser->parse(); echo $result->options['encrypt'] . "\n"; } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ?> ]]> </programlisting> </para> <para>Now if the user type:</para> <para> <programlisting role="text"> <![CDATA[ $ <yourprogram> -e foobar ]]> </programlisting> </para> <para>The output of the above script will be:</para> <para> <programlisting role="text">7f12da3b1c126d7a47745b09dc0040c92cee1700</programlisting> </para> </refsection> <refsection><info><title>List</title></info> <para>a special action that simply output an array as a list.</para> <para> <programlisting role="php"> <![CDATA[ <?php $parser->addOption('list_colors', array( 'short_name' => '-l', 'long_name' => '--list-colors', 'action' => 'List', 'list' => array('blue', 'green', 'yellow') )); $result = $parser->parse(); ?> ]]> </programlisting> </para> <para> <programlisting role="text">$ &lt;yourprogram&gt; --list-colors</programlisting> </para> <para>will display the list of colors separated by commas.</para> </refsection> </refsection> </refentry> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/sub-commands.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/sub-commands.xml +++ peardoc/en/package/console/console-commandline/sub-commands.xml <?xml version="1.0" encoding="utf-8"?> <refentry xmlns="http://docbook.org/ns/docbook" xmlns:phd="http://www.php.net/ns/phd" version="lillet" xml:id="package.console.console-commandline.sub-commands" > <refnamediv> <refname>Managing command line sub-commands</refname> <refpurpose> how to add sub-commands to the parser and use them </refpurpose> </refnamediv> <refsection xml:id="package.console.console-commandline.sub-commands.background"><info><title>What are sub-commands ?</title></info> <para> Some programs are very complex, you can't do anything about that but you need to provide the best user interface possible in order to have happy users. In some cases, sub-commands can be very useful, take the PEAR installer for example, it is much more clearer to separate the installer functionalities than to mix all functionalities in the same interface, that's why you have an interface like: </para> <programlisting role="text"> <![CDATA[ $ pear install <options> <pkgname> $ pear upgrade <options> <pkgname> and so on... ]]> </programlisting> </refsection> <refsection xml:id="package.console.console-commandline.sub-commands.adding"><info><title>Adding sub-commands with <classname>Console_CommandLine</classname></title></info> <para> Adding sub-commands is quite simple, basically you use the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine::addCommand"/> method that returns a <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Command"/> instance and then you build your command instance like you would do for the main parser (add options, arguments etc...). One thing to remember is that sub-commands are exactly the same as main parsers: they have the same properties and methods. </para> <para> Let's take a simple example to demonstrate the use of sub-commands, in the following code we will build a simple command line program that will have two sub-commands: "foo" and "bar": <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' )); ?> ]]> </programlisting> Of course, we could also build our parser with sub-commands using an xml file, the xml file would be: <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> </para> <para> So far, so good, now let's see how to use this parser. When a user type a sub-command in the command line, the result object will have two properties set: the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Result::$command_name"/> that will contain the name (as a string) of the sub-command typed by the user, and the <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Result::$command"/> that will contain a <phd:pearapi phd:package="Console_CommandLine" phd:linkend="Console_CommandLine_Result"/> instance, specific to the provided sub-command. For example with the above parser, we would do: <programlisting role="php"> <![CDATA[ <?php try { $result = $parser->parse(); // find which command was entered switch ($result->command_name) { case 'foo': // the user typed the foo command // options and arguments for this command are stored in the // $result->command instance: print_r($result->command); exit(0); case 'bar': // the user typed the bar command // options and arguments for this command are stored in the // $result->command instance: print_r($result->command); exit(0); default: // no command entered exit(0); } } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ?> ]]> </programlisting> </para> </refsection> </refentry>
« previous php.pear.doc (#11307) next »