cvs: peardoc /en/package/console/console-commandline extending.xml
| From: | David JEAN LOUIS | Date: | Mon, 08 Dec 2008 09:52:50 +0000 |
| Subject: | cvs: peardoc /en/package/console/console-commandline extending.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-11308@lists.php.net to get a copy of this message | ||
izi Mon Dec 8 09:52:50 2008 UTC
Modified files:
/peardoc/en/package/console/console-commandline extending.xml
Log:
added doc for custom actions
http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-commandline/extending.xml?r1=1.5&r2=1.6&diff_format=u
Index: peardoc/en/package/console/console-commandline/extending.xml
diff -u peardoc/en/package/console/console-commandline/extending.xml:1.5
peardoc/en/package/console/console-commandline/extending.xml:1.6
--- peardoc/en/package/console/console-commandline/extending.xml:1.5 Sat Dec 6 15:01:01 2008
+++ peardoc/en/package/console/console-commandline/extending.xml Mon Dec 8 09:52:50 2008
@@ -10,11 +10,74 @@
<refpurpose>how to customize this package to achieve specific tasks</refpurpose>
</refnamediv>
<refsection><info><title>Adding custom actions</title></info>
-
- <para>TODO: write this section</para>
+ <para>
+ <phd:pearapi phd:package="Console_CommandLine"
phd:linkend="Console_CommandLine"/>
+ allows you to register custom actions when you need it.
+ Basically, you just create a action class that inherits from
+ <phd:pearapi phd:package="Console_CommandLine"
phd:linkend="Console_CommandLine_Action"/>
+ and you call the
+ <phd:pearapi phd:package="Console_CommandLine"
phd:linkend="Console_CommandLine::registerAction"/>
+ method to register it.
+ </para>
+ <para>
+ Let's take a simple example, suppose we want to create a "range" action that
+ would allow the user to type:
+ <programlisting role="text">
+ <![CDATA[
+$ <yourprogram> -r 1,5
+ ]]>
+ </programlisting>
+ And in our
+ <phd:pearapi phd:package="Console_CommandLine"
phd:linkend="Console_CommandLine_Result"/>
+ instance we would have:
+ <programlisting role="text">
+ <![CDATA[
+// $result->options['range']: array('min' => 1, 'max' => 5)
+ ]]>
+ </programlisting>
+ Here's how we would do:
+ <programlisting role="php">
+ <![CDATA[
+<?php
+
+require_once 'Console/CommandLine.php';
+require_once 'Console/CommandLine/Action.php';
+
+class ActionRange extends Console_CommandLine_Action
+{
+ public function execute($value=false, $params=array())
+ {
+ $range = explode(',', str_replace(' ', '', $value));
+ if (count($range) != 2) {
+ throw new Exception(sprintf(
+ 'Option "%s" must be 2 integers separated by a comma',
+ $this->option->name
+ ));
+ }
+ $this->setResult(array('min' => $range[0], 'max' =>
$range[1]));
+ }
+}
+
+// then we can register our action
+Console_CommandLine::registerAction('Range', 'ActionRange');
+
+// and now our action is available !
+$parser = new Console_CommandLine();
+$parser->addOption('range', array(
+ 'short_name' => '-r',
+ 'long_name' => '--range',
+ 'action' => 'Range', // note our custom action
+ 'description' => 'A range of two integers separated by a comma'
+));
+// etc...
+
+?>
+ ]]>
+ </programlisting>
+ </para>
</refsection>
+
<refsection><info><title>Using a custom renderer</title></info>
-
<para>TODO: write this section</para>
</refsection>
</refentry>