cvs: peardoc / global.ent /en/package/console console-commandline.xml /en/package/console/console-commandline creating-the-parser.xml examples.xml
extending.xml intro.xml populating-the-parser.xml

From: Date: Mon, 19 May 2008 12:33:40 +0000
Subject: cvs: peardoc / global.ent /en/package/console console-commandline.xml /en/package/console/console-commandline creating-the-parser.xml examples.xml
extending.xml intro.xml populating-the-parser.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-10782@lists.php.net to get a copy of this message
izi Mon May 19 12:33:40 2008 UTC Added files: /peardoc/en/package/console/console-commandline creating-the-parser.xml examples.xml extending.xml intro.xml populating-the-parser.xml Modified files: /peardoc global.ent /peardoc/en/package/console console-commandline.xml Log: initial import of Console_CommandLine documentation in peardoc

http://cvs.php.net/viewvc.cgi/peardoc/global.ent?r1=1.104&r2=1.105&diff_format=u Index: peardoc/global.ent diff -u peardoc/global.ent:1.104 peardoc/global.ent:1.105 --- peardoc/global.ent:1.104 Sat May 10 12:33:36 2008 +++ peardoc/global.ent Mon May 19 12:33:40 2008 @@ -1,6 +1,6 @@ <!-- -*- SGML -*- - $Id: global.ent,v 1.104 2008/05/10 12:33:36 firman Exp $ + $Id: global.ent,v 1.105 2008/05/19 12:33:40 izi Exp $ Contains global "macros" for all the XML documents. @@ -217,9 +217,6 @@ <!ENTITY url.iso.entities "http://jeroen.a-eskwadraat.nl/iso-entities.zip"> <!ENTITY url.docbook.appa "http://www.oasis-open.org/docbook/documentation/reference/html/appa.html"> -<!-- Console --> -<!ENTITY url.console_commandline.docs "http://code.google.com/p/console-commandline/wiki/UserManual"> - <!-- For CVS section --> <!ENTITY url.cvs "http://ximbiot.com/cvs/wiki/"> @@ -272,6 +269,7 @@ <!-- For links to the API documentation generated by phpdocumentor --> <!ENTITY phpdocbase.html-page2 "http://pear.php.net/package/HTML_Page2/docs/latest/HTML_Page2/HTML_Page2.html#method"> <!ENTITY phpdocbase.console-progressbar "http://pear.php.net/package/Console_ProgressBar/docs/latest/Console_ProgressBar/Console_ProgressBar.html#method"> +<!ENTITY phpdocbase.console-commandline "http://pear.php.net/package/Console_CommandLine/docs/latest/Console_CommandLine/Console_CommandLine.html#method"> <!ENTITY phpdocbase.games-chess "http://pear.php.net/package/Games_Chess/docs/latest/Games_Chess/"> <!ENTITY phpdocbase.datetime-date "http://pear.php.net/package/Date/docs/latest/Date/Date.html"> <!ENTITY phpdocbase.datetime-date.method "http://pear.php.net/package/Date/docs/latest/Date/Date.html#class_methods"> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline.xml?r1=1.1&r2=1.2&diff_format=u Index: peardoc/en/package/console/console-commandline.xml diff -u peardoc/en/package/console/console-commandline.xml:1.1 peardoc/en/package/console/console-commandline.xml:1.2 --- peardoc/en/package/console/console-commandline.xml:1.1 Tue Apr 15 06:54:10 2008 +++ peardoc/en/package/console/console-commandline.xml Mon May 19 12:33:40 2008 @@ -1,13 +1,20 @@ -<!-- $Revision: 1.1 $ --> +<!-- $Revision: 1.2 $ --> <sect1 id="package.console.console-commandline"> - <title>Console_Commandline</title> + <title>Console_CommandLine</title> <para> - The documentation for Console_Commandline can be found - on an <ulink url="&url.console_commandline.docs;">external page</ulink>. + <classname>Console_CommandLine</classname> is a full featured package for + managing command line options and arguments highly inspired from python + optparse module, it allows the developer to easily build complex command + line interfaces. </para> - </sect1> + &package.console.console-commandline.intro; + &package.console.console-commandline.creating-the-parser; + &package.console.console-commandline.populating-the-parser; + &package.console.console-commandline.extending; + &package.console.console-commandline.examples; +</sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/creating-the-parser.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/creating-the-parser.xml +++ peardoc/en/package/console/console-commandline/creating-the-parser.xml <!-- $Revision: 1.1 $ --> <refentry id="package.console.console-commandline.creating-the-parser"> <refnamediv> <refname>Creating the parser</refname> <refpurpose> how to create a parser using <acronym>PHP</acronym> code or an <acronym>XML</acronym> definition file </refpurpose> </refnamediv> <refsect1 id="package.console.console-commandline.creating-the-parser.intro"> <title><classname>Console_CommandLine</classname> constructor</title> <para><classname>Console_CommandLine</classname> constructor 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). </para> <para> <table> <title>Available parameters</title> <tgroup cols="2"> <tbody> <row> <entry>name</entry> <entry>(string, optional), the name of your program, if not given it will default to <varname>$argv[0]</varname></entry> </row> <row> <entry>description</entry> <entry>(string optional), the description of your program: this should explain what your program is supposed to do</entry> </row> <row> <entry>version</entry> <entry>(string or numeric, optional), the program version number. If not given, the --version option will not be available.</entry> </row> <row> <entry>add_help_option</entry> <entry>(boolean, optional, default &true;), if set to &false;, tells the parser to not generate automatically the <quote>help</quote> option</entry> </row> <row> <entry>add_version_option</entry> <entry>(boolean, optional, default &true;), if set to &false;, tells the parser to not generate automatically the <quote>version</quote> option</entry> </row> <row> <entry>force_posix</entry> <entry>(boolean, optional, default &false;), if set to &true;, tells the parser to force <acronym>POSIX</acronym> compliance (please see the gettext manual for more information)</entry> </row> </tbody> </tgroup> </table> </para> </refsect1> <refsect1 id="package.console.console-commandline.creating-the-parser.php"> <title>Using <acronym>PHP</acronym> code</title> <para>The examples below demonstrate how to instanciate <classname>Console_CommandLine</classname> and build a parser using <acronym>PHP</acronym> code. </para> <para> <example> <title>The simplest way</title> <programlisting role="php"> <![CDATA[ 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 )); ]]> </programlisting> </example> </para> <para> <example> <title>Alternative method</title> <programlisting role="php"> <![CDATA[ require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); $parser->description = 'A useful description for your program.'; $parser->version = '0.0.1'; // the version of your program ]]> </programlisting> </example> </para> </refsect1> <refsect1 id="package.console.console-commandline.creating-the-parser.xml"> <title>Using an <acronym>XML</acronym> definition file</title> <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. <example> <title>The <acronym>XML</acronym> file</title> <programlisting role="xml"> <![CDATA[ <?xml version="1.0" encoding="utf-8" standalone="yes"?> <command> <description>A useful description for your program.</description> <version>0.0.1</version> </command> ]]> </programlisting> </example> </para> <para> <example> <title>The <acronym>PHP</acronym> file</title> <programlisting> <![CDATA[ require_once 'Console/CommandLine.php'; $parser = Console_CommandLine::fromXmlFile('example.xml'); ]]> </programlisting> </example> </para> <para> Note: Using and <acronym>XML</acronym> string you would have called <ulink url="&phpdocbase.console-commandline;fromXmlString"> <function>Console_CommandLine::fromXmlString</function></ulink> instead of <ulink url="&phpdocbase.console-commandline;fromXmlFile"> <function>Console_CommandLine::fromXmlFile</function></ulink> of course. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/examples.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/examples.xml +++ peardoc/en/package/console/console-commandline/examples.xml <!-- $Revision: 1.1 $ --> <refentry id="package.console.console-commandline.examples"> <refnamediv> <refname>Examples</refname> <refpurpose>various code examples</refpurpose> </refnamediv> <refsect1 id="package.console.console-commandline.examples.ex1"> <title>Basic example</title> <para> <example> <title>Basic example</title> <programlisting role="php"> <![CDATA[ // Include the Console_CommandLine package. require_once 'Console/CommandLine.php'; // create the parser $parser = new Console_CommandLine(array( 'description' => 'zip given files using the php zip module.', 'version' => '1.0.0' )); // add an option to make the program verbose $parser->addOption( 'verbose', array( 'short_name' => '-v', 'long_name' => '--verbose', 'action' => 'StoreTrue', 'description' => 'turn on verbose output' ) ); // add an option to delete original files after zipping $parser->addOption( 'delete', array( 'short_name' => '-d', 'long_name' => '--delete', 'action' => 'StoreTrue', 'description' => 'delete original files after zip operation' ) ); // add the files argument, the user can specify one or several files $parser->addArgument( 'files', array( 'multiple' => true, 'description' => 'list of files to zip separated by spaces' ) ); // add the zip file name argument $parser->addArgument('zipfile', array('description' => 'zip file name')); // run the parser try { $result = $parser->parse(); // write your program here... print_r($result->options); print_r($result->args); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ]]> </programlisting> </example> </para> </refsect1> <refsect1 id="package.console.console-commandline.examples.ex2"> <title> Basic example using an <acronym>XML</acronym> definition file </title> <para> <example> <title>Basic example (<acronym>XML</acronym> file)</title> <programlisting role="xml"> <![CDATA[ <?xml version="1.0" encoding="utf-8" standalone="yes"?> <command> <description>zip given files using the php zip module.</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> <option name="delete"> <short_name>-d</short_name> <long_name>--delete</long_name> <description>delete original files after zip operation</description> <action>StoreTrue</action> </option> <argument name="files"> <description>a list of files to zip together</description> <multiple>true</multiple> </argument> <argument name="zipfile"> <description>path to the zip file to generate</description> </argument> </command> ]]> </programlisting> </example> </para> <para> <example> <title>Basic example (<acronym>PHP</acronym> file)</title> <programlisting role="php"> <![CDATA[ // Include the Console_CommandLine package. require_once 'Console/CommandLine.php'; // create the parser from xml file $xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex2.xml'; $parser = Console_CommandLine::fromXmlFile($xmlfile); // run the parser try { $result = $parser->parse(); // todo: your program here ;) print_r($result->options); print_r($result->args); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ]]> </programlisting> </example> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/extending.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/extending.xml +++ peardoc/en/package/console/console-commandline/extending.xml <!-- $Revision: 1.1 $ --> <refentry id="package.console.console-commandline.extending"> <refnamediv> <refname>Extending Console_CommandLine</refname> <refpurpose>how to customize this package to achieve specific tasks</refpurpose> </refnamediv> <refsect1> <title>Adding custom actions</title> <para>TODO: write this section</para> </refsect1> <refsect1> <title>Using a custom renderer</title> <para>TODO: write this section</para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/intro.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/intro.xml +++ peardoc/en/package/console/console-commandline/intro.xml <!-- $Revision: 1.1 $ --> <refentry id="package.console.console-commandline.intro"> <refnamediv> <refname>Introduction</refname> <refpurpose>introduction to the <classname>Console_CommandLine</classname> package</refpurpose> </refnamediv> <refsect1> <title>Introduction</title> <para> <classname>Console_CommandLine</classname> 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. </para> <para> DISCLAIMER: since Console_CommandLine was highly inspired from the python optparse module and thus is very similar to it, some parts of this document were shamelessly copied from optparse manual. </para> <para> <example> <title>A simple example:</title> <programlisting role="php"> <![CDATA[ require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); $parser->description = 'A fantastic command line program that does nothing.'; $parser->version = '1.5.0'; $parser->addOption('filename', array( 'short_name' => '-f', 'long_name' => '--file', 'description' => 'write report to FILE', 'help_name' => 'FILE', 'action' => 'StoreString' )); $parser->addOption('quiet', array( 'short_name' => '-q', 'long_name' => '--quiet', 'description' => "don't print status messages to stdout", 'action' => 'StoreTrue' )); try { $result = $parser->parse(); // do something with the result object print_r($result->options); print_r($result->args); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); } ]]> </programlisting> </example> </para> <para> With the above lines of code, users of the script can now use the program like this: <programlisting role="text"> <![CDATA[ $ <yourscript> --file=outfile -q ]]> </programlisting> </para> <para> As it parses the command line, Console_CommandLine sets attributes of the result object returned by parse() method based on user-supplied command-line values. When 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;. </para> <para> Console_CommandLine supports both long and short options, allows short options to be merged together, and allows options to be associated with their arguments in a variety of ways. </para> <para> The following lines are all equivalent for the above example: <programlisting role="text"> <![CDATA[ $ <yourscript> -f outfile --quiet $ <yourscript> --quiet --file outfile $ <yourscript> -q -foutfile $ <yourscript> -qfoutfile ]]> </programlisting> </para> <para> Additionally, to get help, users can do this: <programlisting role="text"> <![CDATA[ $ <yourscript> -h $ <yourscript> --help ]]> </programlisting> </para> <para> These commands will print: <programlisting role="text"> <![CDATA[ A fantastic command line program that does nothing. Usage: tmp.php [options] Options: -f FILE, --file=FILE write report to FILE -q, --quiet don't print status messages to stdout -h, --help show this help message and exit --version show the program version and exit ]]> </programlisting> </para> <para> Console_CommandLine also manage the program version automatically: <programlisting role="text"> <![CDATA[ $ <yourscript> --version ]]> </programlisting> </para> <para> The above command will print: <programlisting role="text"> <![CDATA[ $ <yourscript> version 1.5.0. ]]> </programlisting> where the value of yourscript is determined at runtime (normally from <varname>$argv[0]</varname>, except if you specified explicitely a name for your parser). </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/populating-the-parser.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-commandline/populating-the-parser.xml +++ peardoc/en/package/console/console-commandline/populating-the-parser.xml <!-- $Revision: 1.1 $ --> <refentry id="package.console.console-commandline.populating-the-parser"> <refnamediv> <refname>Populating the parser</refname> <refpurpose> how to add options, arguments and subcommands to the parser </refpurpose> </refnamediv> <refsect1 id="package.console.console-commandline.populating-the-parser.options"> <title>Adding options</title> <refsect2 id="package.console.console-commandline.populating-the-parser.options.background"> <title>Some background</title> <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> </refsect2> <refsect2 id="package.console.console-commandline.populating-the-parser.options.adding"> <title>Adding options with <classname>Console_CommandLine</classname></title> <para> To add options to your parser, just create the parser as explained in the previous section and use the <ulink url="&phpdocbase.console-commandline;addOption"> <function>Console_CommandLine::addOption</function></ulink> method. </para> <para> The <ulink url="&phpdocbase.console-commandline;addOption"> <function>Console_CommandLine::addOption</function></ulink> 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->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="3"> <thead> <row> <entry>name</entry> <entry>description</entry> <entry>example</entry> </row> </thead> <tbody> <row> <entry>short_name</entry> <entry>(string, required if no long_name given), the option short name</entry> <entry>-o</entry> </row> <row> <entry>long_name</entry> <entry>(string, required if no short_name given), the option long name</entry> <entry>--orientation</entry> </row> <row> <entry>description</entry> <entry>(string, optional, but recommended for the help message), a description for the option</entry> <entry>orientation of the page: ltr (default) or rtl</entry> </row> <row> <entry>action</entry> <entry>(string, optional, default is <quote>StoreString</quote>), the option action (see next section for details)</entry> <entry>StoreString</entry> </row> <row> <entry>default</entry> <entry>(mixed, optional), the option default value</entry> <entry>ltr</entry> </row> <row> <entry>choices</entry> <entry>(array, optional, only relevant for options that expect argument(s)), an array of possible values for the option</entry> <entry>array('ltr', 'rtl')</entry> </row> <row> <entry>help_name</entry> <entry>(string, optional, if not given it will default to the option name) 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> <title>Adding commandline options</title> <programlisting role="php"> <![CDATA[ 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> <![CDATA[ $ <yourprogram> -vo rtl ]]> </programlisting> </para> <para>or (equivalent):</para> <para> <programlisting> <![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> </refsect2> <refsect2 id="package.console.console-commandline.populating-the-parser.options.actions"> <title>Options actions</title> <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> <refsect3> <title>StoreTrue</title> <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->options['verbose']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('verbose', array('short_name'=>'-v', 'action'=>'StoreTrue')); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>StoreFalse</title> <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->options['verbose']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('verbose', array('short_name'=>'-q', 'action'=>'StoreFalse')); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>StoreString</title> <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->options['outfile']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('outfile', array('short_name'=>'-o', 'action'=>'StoreString')); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>StoreInt</title> <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->options['width']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('width', array('long_name'=>'--width', 'action'=>'StoreInt')); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>StoreFloat</title> <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->options['level']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('level', array('short_name'=>'-l', 'action'=>'StoreFloat')); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>Counter</title> <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 float the integer 4 in <varname>$result->options['verbose_level']</varname>, assuming the option was defined like this: </para> <para> <programlisting role="php"> <![CDATA[ $parser->addOption('verbose_level', array( 'short_name' => '-v', 'long_name' => '--verbose', 'action' => 'Counter' )); $result = $parser->parse(); ]]> </programlisting> </para> </refsect3> <refsect3> <title>Help</title> <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 Console_CommandLine internally.</para> </refsect3> <refsect3> <title>Version</title> <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 Console_CommandLine internally.</para> </refsect3> <refsect3> <title>Password</title> <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[ $parser->addOption('level', array('short_name'=>'-l', 'action'=>'StoreFloat')); $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->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->options['password']</varname> </para> </refsect3> <refsect3> <title>Callback</title> <para>This action allows to specify a <acronym>PHP</acronym> callback to handle user input.</para> <para>TODO: develop this section</para> </refsect3> </refsect2> </refsect1> <refsect1 id="package.console.console-commandline.populating-the-parser.arguments"> <title>Adding arguments</title> <refsect2 id="package.console.console-commandline.populating-the-parser.arguments.background"> <title>Some background</title> <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> </refsect2> <refsect2 id="package.console.console-commandline.populating-the-parser.arguments.adding"> <title>Adding arguments with <classname>Console_CommandLine</classname></title> <para> To add arguments to your parser, just create the parser as explained in the previous section and use the <ulink url="&phpdocbase.console-commandline;addArgument"> <function>Console_CommandLine::addArgument</function></ulink> method. </para> <para> The <ulink url="&phpdocbase.console-commandline;addArgument"> <function>Console_CommandLine::addArgument</function></ulink> 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->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="3"> <thead> <row> <entry>name</entry> <entry>description</entry> <entry>example</entry> </row> </thead> <tbody> <row> <entry>description</entry> <entry>(string, optional, but recommended for the help message), a description for the argument</entry> <entry>entry list of input files separated by spaces</entry> </row> <row> <entry>multiple</entry> <entry>(boolean, optional, default to &false;), tells the parser that the argument expects multiples values</entry> <entry>&true;</entry> </row> <row> <entry>help_name</entry> <entry>(string, optional, if not given it will default to the argument name), the name to display in the argument help line</entry> <entry>files</entry> </row> </tbody> </tgroup> </table> </para> <para> <example> <title>Adding commandline arguments</title> <programlisting role="php"> <![CDATA[ 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> <![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> </refsect2> </refsect1> <refsect1 id="package.console.console-commandline.populating-the-parser.subcommands"> <title>Adding sub-commands</title> <refsect2 id="package.console.console-commandline.populating-the-parser.subcommands.background"> <title>What are sub-commands ?</title> <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> </refsect2> <refsect2 id="package.console.console-commandline.populating-the-parser.subcommands.adding"> <title>Adding sub-commands with <classname>Console_CommandLine</classname></title> <para>TODO: develop this section</para> </refsect2> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.pear.doc (#10782) next »