Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial import of Console_CommandLine PEAR2 port

  • Loading branch information...
commit 18ea84b7b5cfe0a26f8afa896f1a628c2cb98f75 1 parent 5bf3f93
David Jean Louis izimobil authored

Showing 68 changed files with 5,501 additions and 0 deletions. Show diff stats Hide diff stats

  1. +3 0  CREDITS
  2. +12 0 README
  3. +1 0  RELEASE-0.1.0
  4. +153 0 data/xmlschema.rng
  5. +77 0 examples/ex1.php
  6. +49 0 examples/ex2.php
  7. +34 0 examples/ex2.xml
  8. +51 0 makepackage.php
  9. +136 0 package.xml
  10. +1,029 0 src/Console/CommandLine.php
  11. +135 0 src/Console/CommandLine/Action.php
  12. +79 0 src/Console/CommandLine/Action/Callback.php
  13. +85 0 src/Console/CommandLine/Action/Counter.php
  14. +59 0 src/Console/CommandLine/Action/Help.php
  15. +89 0 src/Console/CommandLine/Action/Password.php
  16. +66 0 src/Console/CommandLine/Action/StoreArray.php
  17. +62 0 src/Console/CommandLine/Action/StoreFalse.php
  18. +74 0 src/Console/CommandLine/Action/StoreFloat.php
  19. +74 0 src/Console/CommandLine/Action/StoreInt.php
  20. +61 0 src/Console/CommandLine/Action/StoreString.php
  21. +62 0 src/Console/CommandLine/Action/StoreTrue.php
  22. +59 0 src/Console/CommandLine/Action/Version.php
  23. +76 0 src/Console/CommandLine/Argument.php
  24. +46 0 src/Console/CommandLine/Command.php
  25. +119 0 src/Console/CommandLine/Element.php
  26. +78 0 src/Console/CommandLine/Exception.php
  27. +55 0 src/Console/CommandLine/MessageProvider.php
  28. +99 0 src/Console/CommandLine/MessageProvider/Default.php
  29. +334 0 src/Console/CommandLine/Option.php
  30. +66 0 src/Console/CommandLine/Outputter.php
  31. +74 0 src/Console/CommandLine/Outputter/Default.php
  32. +75 0 src/Console/CommandLine/Renderer.php
  33. +383 0 src/Console/CommandLine/Renderer/Default.php
  34. +76 0 src/Console/CommandLine/Result.php
  35. +257 0 src/Console/CommandLine/XmlParser.php
  36. +78 0 tests/AllTests.php
  37. +28 0 tests/console_commandline_accept.phpt
  38. +63 0 tests/console_commandline_addargument.phpt
  39. +34 0 tests/console_commandline_addcommand.phpt
  40. +119 0 tests/console_commandline_addoption.phpt
  41. +14 0 tests/console_commandline_addoption_errors_1.phpt
  42. +14 0 tests/console_commandline_addoption_errors_2.phpt
  43. +14 0 tests/console_commandline_addoption_errors_3.phpt
  44. +14 0 tests/console_commandline_addoption_errors_4.phpt
  45. +14 0 tests/console_commandline_addoption_errors_5.phpt
  46. +14 0 tests/console_commandline_addoption_errors_6.phpt
  47. +14 0 tests/console_commandline_addoption_errors_7.phpt
  48. +31 0 tests/console_commandline_fromxmlfile.phpt
  49. +19 0 tests/console_commandline_fromxmlfile_error.phpt
  50. +32 0 tests/console_commandline_fromxmlstring.phpt
  51. +17 0 tests/console_commandline_parse_1.phpt
  52. +36 0 tests/console_commandline_parse_10.phpt
  53. +31 0 tests/console_commandline_parse_11.phpt
  54. +27 0 tests/console_commandline_parse_12.phpt
  55. +21 0 tests/console_commandline_parse_13.phpt
  56. +21 0 tests/console_commandline_parse_14.phpt
  57. +23 0 tests/console_commandline_parse_15.phpt
  58. +21 0 tests/console_commandline_parse_16.phpt
  59. +37 0 tests/console_commandline_parse_2.phpt
  60. +58 0 tests/console_commandline_parse_3.phpt
  61. +23 0 tests/console_commandline_parse_4.phpt
  62. +23 0 tests/console_commandline_parse_5.phpt
  63. +23 0 tests/console_commandline_parse_6.phpt
  64. +67 0 tests/console_commandline_parse_7.phpt
  65. +66 0 tests/console_commandline_parse_8.phpt
  66. +55 0 tests/console_commandline_parse_9.phpt
  67. +44 0 tests/test.xml
  68. +218 0 tests/tests.inc.php
3  CREDITS
... ... @@ -0,0 +1,3 @@
  1 +; Maintainers of Console_CommandLine
  2 +David JEAN LOUIS [izi] <izi@php.net> (lead)
  3 +
12 README
... ... @@ -0,0 +1,12 @@
  1 +PEAR2_Console_CommandLine is a full featured package for managing command-line
  2 +options and arguments highly inspired from python optparse module, it allows
  3 +the developer to easily build complex command line interfaces.
  4 +
  5 +Main features:
  6 +* handles sub commands (ie. $ myscript.php -q subcommand -f file),
  7 +* can be completely built from an xml definition file,
  8 +* generate --help and --version options automatically,
  9 +* can be completely customized,
  10 +* builtin support for i18n,
  11 +* and much more...
  12 +
1  RELEASE-0.1.0
... ... @@ -0,0 +1 @@
  1 +Initial port of PEAR Console_CommandLine to PEAR2.
153 data/xmlschema.rng
... ... @@ -0,0 +1,153 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +
  3 +<!--
  4 + This is the RNG file for validating PEAR2_Console_CommandLine xml definitions.
  5 +
  6 + Author : David JEAN LOUIS
  7 + Licence : MIT License
  8 + Version : SVN: $Id$
  9 +-->
  10 +
  11 +<grammar xmlns="http://relaxng.org/ns/structure/1.0"
  12 + datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  13 +
  14 + <!-- structure -->
  15 + <start>
  16 + <ref name="ref_command"/>
  17 + </start>
  18 +
  19 + <!-- Command node -->
  20 + <define name="ref_command">
  21 + <element name="command">
  22 + <interleave>
  23 + <optional>
  24 + <element name="name">
  25 + <text/>
  26 + </element>
  27 + </optional>
  28 + <optional>
  29 + <element name="description">
  30 + <text/>
  31 + </element>
  32 + </optional>
  33 + <optional>
  34 + <element name="version">
  35 + <text/>
  36 + </element>
  37 + </optional>
  38 + <optional>
  39 + <element name="add_help_option">
  40 + <ref name="ref_bool_choices"/>
  41 + </element>
  42 + </optional>
  43 + <optional>
  44 + <element name="add_version_option">
  45 + <ref name="ref_bool_choices"/>
  46 + </element>
  47 + </optional>
  48 + <optional>
  49 + <element name="force_posix">
  50 + <ref name="ref_bool_choices"/>
  51 + </element>
  52 + </optional>
  53 + <zeroOrMore>
  54 + <ref name="ref_option"/>
  55 + </zeroOrMore>
  56 + <zeroOrMore>
  57 + <ref name="ref_argument"/>
  58 + </zeroOrMore>
  59 + <zeroOrMore>
  60 + <ref name="ref_command"/>
  61 + </zeroOrMore>
  62 + </interleave>
  63 + </element>
  64 + </define>
  65 +
  66 + <!-- options and arguments common elements -->
  67 +
  68 + <define name="ref_option_argument_common">
  69 + <optional>
  70 + <element name="description">
  71 + <text/>
  72 + </element>
  73 + </optional>
  74 + <optional>
  75 + <element name="help_name">
  76 + <text/>
  77 + </element>
  78 + </optional>
  79 + <optional>
  80 + <element name="default">
  81 + <text/>
  82 + </element>
  83 + </optional>
  84 + </define>
  85 +
  86 + <!-- Option node -->
  87 + <define name="ref_option">
  88 + <element name="option">
  89 + <attribute name="name">
  90 + <data type="string"/>
  91 + </attribute>
  92 + <interleave>
  93 + <optional>
  94 + <element name="short_name">
  95 + <text/>
  96 + </element>
  97 + </optional>
  98 + <optional>
  99 + <element name="long_name">
  100 + <text/>
  101 + </element>
  102 + </optional>
  103 + <ref name="ref_option_argument_common"/>
  104 + <optional>
  105 + <element name="action">
  106 + <text/>
  107 + </element>
  108 + </optional>
  109 + <optional>
  110 + <element name="choices">
  111 + <zeroOrMore>
  112 + <element name="choice">
  113 + <text/>
  114 + </element>
  115 + </zeroOrMore>
  116 + </element>
  117 + </optional>
  118 + </interleave>
  119 + </element>
  120 + </define>
  121 +
  122 + <!-- Argument node -->
  123 + <define name="ref_argument">
  124 + <element name="argument">
  125 + <attribute name="name">
  126 + <data type="string"/>
  127 + </attribute>
  128 + <interleave>
  129 + <ref name="ref_option_argument_common"/>
  130 + <optional>
  131 + <element name="multiple">
  132 + <ref name="ref_bool_choices"/>
  133 + </element>
  134 + </optional>
  135 + </interleave>
  136 + </element>
  137 + </define>
  138 +
  139 + <!-- boolean choices -->
  140 + <define name="ref_bool_choices">
  141 + <choice>
  142 + <value>true</value>
  143 + <value>1</value>
  144 + <value>on</value>
  145 + <value>yes</value>
  146 + <value>false</value>
  147 + <value>0</value>
  148 + <value>off</value>
  149 + <value>no</value>
  150 + </choice>
  151 + </define>
  152 +
  153 +</grammar>
77 examples/ex1.php
... ... @@ -0,0 +1,77 @@
  1 +<?php
  2 +
  3 +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4 +
  5 +/**
  6 + * This file is part of the PEAR2_Console_CommandLine package.
  7 + *
  8 + * A simple example demonstrating the basic features of the PEAR2_Console_CommandLine
  9 + * package.
  10 + * In this example we create a program that simply zip a set of files.
  11 + *
  12 + * PHP version 5
  13 + *
  14 + * LICENSE: This source file is subject to the MIT license that is available
  15 + * through the world-wide-web at the following URI:
  16 + * http://opensource.org/licenses/mit-license.php
  17 + *
  18 + * @category Console
  19 + * @package PEAR2_Console_CommandLine
  20 + * @author David JEAN LOUIS <izimobil@gmail.com>
  21 + * @copyright 2007 David JEAN LOUIS
  22 + * @license http://opensource.org/licenses/mit-license.php MIT License
  23 + * @version SVN: $Id$
  24 + * @link http://pear.php.net/package/Console_CommandLine
  25 + * @since File available since release 0.1.0
  26 + */
  27 +
  28 +// uncomment this when package won't be in the SandBox anymore
  29 +// $basedir = __DIR__ . '/..';
  30 +$basedir = __DIR__ . '/../..';
  31 +
  32 +// Include PEAR2 autoload
  33 +require_once $basedir . '/autoload.php';
  34 +
  35 +// create the parser
  36 +$parser = new PEAR2_Console_CommandLine(array(
  37 + 'description' => 'zip given files using the php zip module.',
  38 + 'version' => '1.0.0'
  39 +));
  40 +
  41 +// add an option to make the program verbose
  42 +$parser->addOption('verbose', array(
  43 + 'short_name' => '-v',
  44 + 'long_name' => '--verbose',
  45 + 'action' => 'StoreTrue',
  46 + 'description' => 'turn on verbose output'
  47 +));
  48 +
  49 +// add an option to delete original files after zipping
  50 +$parser->addOption('delete', array(
  51 + 'short_name' => '-d',
  52 + 'long_name' => '--delete',
  53 + 'action' => 'StoreString',
  54 + 'description' => 'delete original files after zip operation',
  55 + 'choices' => array('foo', 'bar')
  56 +));
  57 +
  58 +// add the files argument, the user can specify one or several files
  59 +$parser->addArgument('files', array(
  60 + 'multiple' => true,
  61 + 'description' => 'list of files to zip separated by spaces'
  62 +));
  63 +
  64 +// add the zip file name argument
  65 +$parser->addArgument('zipfile', array('description' => 'zip file name'));
  66 +
  67 +// run the parser
  68 +try {
  69 + $result = $parser->parse();
  70 + // write your program here...
  71 + print_r($result->options);
  72 + print_r($result->args);
  73 +} catch (Exception $exc) {
  74 + $parser->displayError($exc->getMessage());
  75 +}
  76 +
  77 +?>
49 examples/ex2.php
... ... @@ -0,0 +1,49 @@
  1 +<?php
  2 +
  3 +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4 +
  5 +/**
  6 + * This file is part of the PEAR2_Console_CommandLine package.
  7 + *
  8 + * This example demonstrate the use of xml definitions files with
  9 + * PEAR2_Console_CommandLine, the result is the same as for the ex1.php file.
  10 + *
  11 + * PHP version 5
  12 + *
  13 + * LICENSE: This source file is subject to the MIT license that is available
  14 + * through the world-wide-web at the following URI:
  15 + * http://opensource.org/licenses/mit-license.php
  16 + *
  17 + * @category Console
  18 + * @package PEAR2_Console_CommandLine
  19 + * @author David JEAN LOUIS <izimobil@gmail.com>
  20 + * @copyright 2007 David JEAN LOUIS
  21 + * @license http://opensource.org/licenses/mit-license.php MIT License
  22 + * @version SVN: $Id$
  23 + * @link http://pear.php.net/package/Console_CommandLine
  24 + * @since File available since release 0.1.0
  25 + */
  26 +
  27 +// uncomment this when package won't be in the SandBox anymore
  28 +// $basedir = __DIR__ . '/..';
  29 +$basedir = __DIR__ . '/../..';
  30 +
  31 +// Include PEAR2 autoload
  32 +require_once $basedir . '/autoload.php';
  33 +
  34 +// create the parser from xml file
  35 +$xmlfile = __DIR__ . DIRECTORY_SEPARATOR . 'ex2.xml';
  36 +$parser = PEAR2_Console_CommandLine::fromXmlFile($xmlfile);
  37 +
  38 +
  39 +// run the parser
  40 +try {
  41 + $result = $parser->parse();
  42 + // write your program here...
  43 + print_r($result->options);
  44 + print_r($result->args);
  45 +} catch (Exception $exc) {
  46 + $parser->displayError($exc->getMessage());
  47 +}
  48 +
  49 +?>
34 examples/ex2.xml
... ... @@ -0,0 +1,34 @@
  1 +<?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2 +<command>
  3 + <description>zip given files using the php zip module.</description>
  4 + <version>1.0.0</version>
  5 + <option name="choice">
  6 + <short_name>-c</short_name>
  7 + <long_name>--choice</long_name>
  8 + <description>choice option</description>
  9 + <action>StoreString</action>
  10 + <choices>
  11 + <choice>ham</choice>
  12 + <choice>spam</choice>
  13 + </choices>
  14 + </option>
  15 + <option name="verbose">
  16 + <short_name>-v</short_name>
  17 + <long_name>--verbose</long_name>
  18 + <description>turn on verbose output</description>
  19 + <action>StoreTrue</action>
  20 + </option>
  21 + <option name="delete">
  22 + <short_name>-d</short_name>
  23 + <long_name>--delete</long_name>
  24 + <description>delete original files after zip operation</description>
  25 + <action>StoreTrue</action>
  26 + </option>
  27 + <argument name="files">
  28 + <description>a list of files to zip together</description>
  29 + <multiple>true</multiple>
  30 + </argument>
  31 + <argument name="zipfile">
  32 + <description>path to the zip file to generate</description>
  33 + </argument>
  34 +</command>
51 makepackage.php
... ... @@ -0,0 +1,51 @@
  1 +<?php
  2 +
  3 +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4 +
  5 +/**
  6 + * This file is part of the PEAR2_Console_CommandLine package.
  7 + *
  8 + * A full featured package for managing command-line options and arguments
  9 + * hightly inspired from python optparse module, it allows the developper to
  10 + * easily build complex command line interfaces.
  11 + *
  12 + * PHP version 5
  13 + *
  14 + * LICENSE: This source file is subject to the MIT license that is available
  15 + * through the world-wide-web at the following URI:
  16 + * http://opensource.org/licenses/mit-license.php
  17 + *
  18 + * @category Console
  19 + * @package PEAR2_Console_CommandLine
  20 + * @author David JEAN LOUIS <izimobil@gmail.com>
  21 + * @copyright 2007 David JEAN LOUIS
  22 + * @license http://opensource.org/licenses/mit-license.php MIT License
  23 + * @version SVN: $Id$
  24 + * @link http://pear.php.net/package/Console_CommandLine
  25 + * @since Class available since release 0.1.0
  26 + */
  27 +
  28 +// ensure that errors will be printed
  29 +error_reporting(E_ALL);
  30 +ini_set('display_errors',true);
  31 +
  32 +// uncomment this when package won't be in the SandBox anymore
  33 +// $basedir = __DIR__ . '/..';
  34 +$basedir = __DIR__ . '/../..';
  35 +
  36 +require_once $basedir . '/autoload.php';
  37 +
  38 +new PEAR2_Pyrus_Developer_PackageFile_PEAR2SVN(
  39 + __DIR__,
  40 + 'PEAR2_Console_CommandLine'
  41 +);
  42 +
  43 +$package = new PEAR2_Pyrus_Package('package.xml');
  44 +$outfile = $package->name . '-' . $package->version['release'];
  45 +$creator = new PEAR2_Pyrus_Package_Creator(
  46 + new PEAR2_Pyrus_Developer_Creator_Phar($outfile . '.tgz', false, Phar::TAR, Phar::GZ),
  47 + $basedir . '/Exception/src',
  48 + $basedir . '/Autoload/src',
  49 + $basedir . '/MultiErrors/src'
  50 +);
  51 +$creator->render($package);
136 package.xml
... ... @@ -0,0 +1,136 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<package version="2.1" xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd">
  3 + <name>PEAR2_Console_CommandLine</name>
  4 + <channel>pear2.php.net</channel>
  5 + <summary>PEAR2_Console_CommandLine is a full featured package for managing command-line
  6 +</summary>
  7 + <description>options and arguments highly inspired from python optparse module, it allows
  8 +the developer to easily build complex command line interfaces.
  9 +
  10 +Main features:
  11 +* handles sub commands (ie. $ myscript.php -q subcommand -f file),
  12 +* can be completely built from an xml definition file,
  13 +* generate --help and --version options automatically,
  14 +* can be completely customized,
  15 +* builtin support for i18n,
  16 +* and much more...
  17 +
  18 +</description>
  19 + <lead>
  20 + <name>David JEAN LOUIS</name>
  21 + <user>izi</user>
  22 + <email>izi@php.net</email>
  23 + <active>yes</active>
  24 + </lead>
  25 + <date>2008-06-08</date>
  26 + <time>21:20:56</time>
  27 + <version>
  28 + <release>0.1.0</release>
  29 + <api>0.1.0</api>
  30 + </version>
  31 + <stability>
  32 + <release>devel</release>
  33 + <api>alpha</api>
  34 + </stability>
  35 + <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
  36 + <notes>Initial port of PEAR Console_CommandLine to PEAR2.
  37 +</notes>
  38 + <contents>
  39 + <dir name="/">
  40 + <dir name="data" baseinstalldir="/">
  41 + <file role="data" name="xmlschema.rng"/>
  42 + </dir>
  43 + <dir name="examples">
  44 + <file role="doc" name="ex1.php"/>
  45 + <file role="doc" name="ex2.php"/>
  46 + <file role="doc" name="ex2.xml"/>
  47 + </dir>
  48 + <dir name="src" baseinstalldir="PEAR2">
  49 + <dir name="Console">
  50 + <dir name="CommandLine">
  51 + <dir name="Action">
  52 + <file role="php" name="Callback.php"/>
  53 + <file role="php" name="Counter.php"/>
  54 + <file role="php" name="Help.php"/>
  55 + <file role="php" name="Password.php"/>
  56 + <file role="php" name="StoreArray.php"/>
  57 + <file role="php" name="StoreFalse.php"/>
  58 + <file role="php" name="StoreFloat.php"/>
  59 + <file role="php" name="StoreInt.php"/>
  60 + <file role="php" name="StoreString.php"/>
  61 + <file role="php" name="StoreTrue.php"/>
  62 + <file role="php" name="Version.php"/>
  63 + </dir>
  64 + <dir name="MessageProvider">
  65 + <file role="php" name="Default.php"/>
  66 + </dir>
  67 + <dir name="Outputter">
  68 + <file role="php" name="Default.php"/>
  69 + </dir>
  70 + <dir name="Renderer">
  71 + <file role="php" name="Default.php"/>
  72 + </dir>
  73 + <file role="php" name="Action.php"/>
  74 + <file role="php" name="Argument.php"/>
  75 + <file role="php" name="Command.php"/>
  76 + <file role="php" name="Element.php"/>
  77 + <file role="php" name="Exception.php"/>
  78 + <file role="php" name="MessageProvider.php"/>
  79 + <file role="php" name="Option.php"/>
  80 + <file role="php" name="Outputter.php"/>
  81 + <file role="php" name="Renderer.php"/>
  82 + <file role="php" name="Result.php"/>
  83 + <file role="php" name="XmlParser.php"/>
  84 + </dir>
  85 + <file role="php" name="CommandLine.php"/>
  86 + </dir>
  87 + </dir>
  88 + <dir name="tests" baseinstalldir="/">
  89 + <file role="test" name="AllTests.php"/>
  90 + <file role="test" name="console_commandline_accept.phpt"/>
  91 + <file role="test" name="console_commandline_addargument.phpt"/>
  92 + <file role="test" name="console_commandline_addcommand.phpt"/>
  93 + <file role="test" name="console_commandline_addoption.phpt"/>
  94 + <file role="test" name="console_commandline_addoption_errors_1.phpt"/>
  95 + <file role="test" name="console_commandline_addoption_errors_2.phpt"/>
  96 + <file role="test" name="console_commandline_addoption_errors_3.phpt"/>
  97 + <file role="test" name="console_commandline_addoption_errors_4.phpt"/>
  98 + <file role="test" name="console_commandline_addoption_errors_5.phpt"/>
  99 + <file role="test" name="console_commandline_addoption_errors_6.phpt"/>
  100 + <file role="test" name="console_commandline_addoption_errors_7.phpt"/>
  101 + <file role="test" name="console_commandline_fromxmlfile.phpt"/>
  102 + <file role="test" name="console_commandline_fromxmlfile_error.phpt"/>
  103 + <file role="test" name="console_commandline_fromxmlstring.phpt"/>
  104 + <file role="test" name="console_commandline_parse_1.phpt"/>
  105 + <file role="test" name="console_commandline_parse_2.phpt"/>
  106 + <file role="test" name="console_commandline_parse_3.phpt"/>
  107 + <file role="test" name="console_commandline_parse_4.phpt"/>
  108 + <file role="test" name="console_commandline_parse_5.phpt"/>
  109 + <file role="test" name="console_commandline_parse_6.phpt"/>
  110 + <file role="test" name="console_commandline_parse_7.phpt"/>
  111 + <file role="test" name="console_commandline_parse_8.phpt"/>
  112 + <file role="test" name="console_commandline_parse_9.phpt"/>
  113 + <file role="test" name="console_commandline_parse_10.phpt"/>
  114 + <file role="test" name="console_commandline_parse_11.phpt"/>
  115 + <file role="test" name="console_commandline_parse_12.phpt"/>
  116 + <file role="test" name="console_commandline_parse_13.phpt"/>
  117 + <file role="test" name="console_commandline_parse_14.phpt"/>
  118 + <file role="test" name="console_commandline_parse_15.phpt"/>
  119 + <file role="test" name="console_commandline_parse_16.phpt"/>
  120 + <file role="test" name="test.xml"/>
  121 + <file role="test" name="tests.inc.php"/>
  122 + </dir>
  123 + </dir>
  124 + </contents>
  125 + <dependencies>
  126 + <required>
  127 + <php>
  128 + <min>5.2.0</min>
  129 + </php>
  130 + <pearinstaller>
  131 + <min>2.0.0a1</min>
  132 + </pearinstaller>
  133 + </required>
  134 + </dependencies>
  135 + <phprelease/>
  136 +</package>
1,029 src/Console/CommandLine.php
... ... @@ -0,0 +1,1029 @@
  1 +<?php
  2 +
  3 +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4 +
  5 +/**
  6 + * This file is part of the PEAR2_Console_CommandLine package.
  7 + *
  8 + * A full featured package for managing command-line options and arguments
  9 + * hightly inspired from python optparse module, it allows the developper to
  10 + * easily build complex command line interfaces.
  11 + *
  12 + * PHP version 5
  13 + *
  14 + * LICENSE: This source file is subject to the MIT license that is available
  15 + * through the world-wide-web at the following URI:
  16 + * http://opensource.org/licenses/mit-license.php
  17 + *
  18 + * @category Console
  19 + * @package PEAR2_Console_CommandLine
  20 + * @author David JEAN LOUIS <izimobil@gmail.com>
  21 + * @copyright 2007 David JEAN LOUIS
  22 + * @license http://opensource.org/licenses/mit-license.php MIT License
  23 + * @version SVN: $Id$
  24 + * @link http://pear.php.net/package/Console_CommandLine
  25 + * @since Class available since release 0.1.0
  26 + */
  27 +
  28 +/**
  29 + * Main class for parsing command line options and arguments.
  30 + *
  31 + * There are three ways to create parsers with this class:
  32 + * <code>
  33 + * // direct usage
  34 + * $parser = new PEAR2_Console_CommandLine();
  35 + *
  36 + * // with an xml definition file
  37 + * $parser = PEAR2_Console_CommandLine::fromXmlFile('path/to/file.xml');
  38 + *
  39 + * // with an xml definition string
  40 + * $validXmlString = '..your xml string...';
  41 + * $parser = PEAR2_Console_CommandLine::fromXmlString($validXmlString);
  42 + * </code>
  43 + *
  44 + * @category Console
  45 + * @package PEAR2_Console_CommandLine
  46 + * @author David JEAN LOUIS <izimobil@gmail.com>
  47 + * @copyright 2007 David JEAN LOUIS
  48 + * @license http://opensource.org/licenses/mit-license.php MIT License
  49 + * @version Release: @package_version@
  50 + * @link http://pear.php.net/package/Console_CommandLine
  51 + * @since File available since release 0.1.0
  52 + * @example docs/examples/ex1.php
  53 + * @example docs/examples/ex2.php
  54 + */
  55 +class PEAR2_Console_CommandLine
  56 +{
  57 + // Public properties {{{
  58 +
  59 + /**
  60 + * Error messages.
  61 + *
  62 + * @var array $errors
  63 + * @access public
  64 + */
  65 + public static $errors = array(
  66 + 'option_bad_name' => 'option name must be a valid php variable name (got: {$name})',
  67 + 'argument_bad_name' => 'argument name must be a valid php variable name (got: {$name})',
  68 + 'option_long_and_short_name_missing' => 'you must provide at least an option short name or long name for option "{$name}"',
  69 + 'option_bad_short_name' => 'option "{$name}" short name must be a dash followed by a letter (got: "{$short_name}")',
  70 + 'option_bad_long_name' => 'option "{$name}" long name must be 2 dashes followed by a word (got: "{$long_name}")',
  71 + 'option_unregistered_action' => 'unregistered action "{$action}" for option "{$name}".',
  72 + 'option_bad_action' => 'invalid action for option "{$name}".',
  73 + 'option_invalid_callback' => 'you must provide a valid callback for option "{$name}"',
  74 + 'action_class_does_not_exists' => 'action "{$name}" class "{$class}" not found, make sure that your class is available before calling PEAR2_Console_CommandLine::registerAction()',
  75 + 'invalid_xml_file' => 'XML definition file "{$file}" does not exists or is not readable',
  76 + 'invalid_rng_file' => 'RNG file "{$file}" does not exists or is not readable'
  77 + );
  78 +
  79 + /**
  80 + * The name of the program, if not given it defaults to argv[0].
  81 + *
  82 + * @var string $name
  83 + * @access public
  84 + */
  85 + public $name;
  86 +
  87 + /**
  88 + * A description text that will be displayed in the help message.
  89 + *
  90 + * @var string $description
  91 + * @access public
  92 + */
  93 + public $description = '';
  94 +
  95 + /**
  96 + * A string that represents the version of the program, if this property is
  97 + * not empty and property add_version_option is not set to false, the
  98 + * command line parser will add a --version option, that will display the
  99 + * property content.
  100 + *
  101 + * @var string $version
  102 + * @access public
  103 + */
  104 + public $version = '';
  105 +
  106 + /**
  107 + * Boolean that determine if the command line parser should add the help
  108 + * (-h, --help) option automatically.
  109 + *
  110 + * @var bool $add_help_option
  111 + * @access public
  112 + */
  113 + public $add_help_option = true;
  114 +
  115 + /**
  116 + * Boolean that determine if the command line parser should add the version
  117 + * (-v, --version) option automatically.
  118 + * Note that the version option is also generated only if the version
  119 + * property is not empty, it's up to you to provide a version string of
  120 + * course.
  121 + *
  122 + * @var bool $add_version_option
  123 + * @access public
  124 + */
  125 + public $add_version_option = true;
  126 +
  127 + /**
  128 + * The command line parser renderer instance.
  129 + *
  130 + * @var object that implements PEAR2_Console_CommandLine_Renderer interface
  131 + * @access protected
  132 + */
  133 + public $renderer = false;
  134 +
  135 + /**
  136 + * The command line parser outputter instance.
  137 + *
  138 + * @var object that implements PEAR2_Console_CommandLine::Outputter interface
  139 + * @access protected
  140 + */
  141 + public $outputter = false;
  142 +
  143 + /**
  144 + * The command line message provider instance.
  145 + *
  146 + * @var object an instance of PEAR2_Console_CommandLine::Message
  147 + * @access protected
  148 + */
  149 + public $message_provider = false;
  150 +
  151 + /**
  152 + * Boolean that tells the parser to be POSIX compliant, POSIX demands the
  153 + * following behavior: the first non-option stops option processing.
  154 + *
  155 + * @var bool $force_posix
  156 + * @access public
  157 + */
  158 + public $force_posix = false;
  159 +
  160 + /**
  161 + * An array of PEAR2_Console_CommandLine_Option objects.
  162 + *
  163 + * @var array $options
  164 + * @access public
  165 + */
  166 + public $options = array();
  167 +
  168 + /**
  169 + * An array of PEAR2_Console_CommandLine_Argument objects.
  170 + *
  171 + * @var array $args
  172 + * @access public
  173 + */
  174 + public $args = array();
  175 +
  176 + /**
  177 + * An array of PEAR2_Console_CommandLine_Command objects (sub commands).
  178 + *
  179 + * @var array $commands
  180 + * @access public
  181 + */
  182 + public $commands = array();
  183 +
  184 + /**
  185 + * Parent, only relevant in Command objects but left here for interface
  186 + * convenience.
  187 + *
  188 + * @var object PEAR2_Console_CommandLine
  189 + * @access public
  190 + */
  191 + public $parent = false;
  192 +
  193 + /**
  194 + * Array of valid actions for an option, this array will also store user
  195 + * registered actions.
  196 + * The array format is:
  197 + * <pre>
  198 + * array(
  199 + * <ActionName:string> => array(<ActionClass:string>, <builtin:bool>)
  200 + * )
  201 + *
  202 + * @var array $actions
  203 + * @static
  204 + * @access public
  205 + */
  206 + public static $actions = array(
  207 + 'StoreTrue' => array('PEAR2_Console_CommandLine_Action_StoreTrue', true),
  208 + 'StoreFalse' => array('PEAR2_Console_CommandLine_Action_StoreFalse', true),
  209 + 'StoreString' => array('PEAR2_Console_CommandLine_Action_StoreString', true),
  210 + 'StoreInt' => array('PEAR2_Console_CommandLine_Action_StoreInt', true),
  211 + 'StoreFloat' => array('PEAR2_Console_CommandLine_Action_StoreFloat', true),
  212 + 'StoreArray' => array('PEAR2_Console_CommandLine_Action_StoreArray', true),
  213 + 'Callback' => array('PEAR2_Console_CommandLine_Action_Callback', true),
  214 + 'Counter' => array('PEAR2_Console_CommandLine_Action_Counter', true),
  215 + 'Help' => array('PEAR2_Console_CommandLine_Action_Help', true),
  216 + 'Version' => array('PEAR2_Console_CommandLine_Action_Version', true),
  217 + 'Password' => array('PEAR2_Console_CommandLine_Action_Password', true)
  218 + );
  219 +
  220 + /**
  221 + * Array of options that must be dispatched at the end.
  222 + *
  223 + * @var array $_dispatchLater
  224 + * @access private
  225 + */
  226 + private $_dispatchLater = array();
  227 +
  228 + // }}}
  229 + // __construct() {{{
  230 +
  231 + /**
  232 + * Constructor.
  233 + * Example:
  234 + *
  235 + * <code>
  236 + * $parser = new PEAR2_Console_CommandLine(array(
  237 + * 'name' => 'yourprogram', // defaults to argv[0]
  238 + * 'description' => 'Description of your program',
  239 + * 'version' => '0.0.1', // your program version
  240 + * 'add_help_option' => true, // or false to disable --version option
  241 + * 'add_version_option' => true, // or false to disable --help option
  242 + * 'force_posix' => false // or true to force posix compliance
  243 + * ));
  244 + * </code>
  245 + *
  246 + * @param array $params an optional array of parameters
  247 + *
  248 + * @access public
  249 + */
  250 + public function __construct(array $params=array())
  251 + {
  252 + if (isset($params['name'])) {
  253 + $this->name = $params['name'];
  254 + } else {
  255 + $this->name = $_SERVER['argv'][0];
  256 + }
  257 + if (isset($params['description'])) {
  258 + $this->description = $params['description'];
  259 + }
  260 + if (isset($params['version'])) {
  261 + $this->version = $params['version'];
  262 + }
  263 + if (isset($params['add_version_option'])) {
  264 + $this->add_version_option = $params['add_version_option'];
  265 + }
  266 + if (isset($params['add_help_option'])) {
  267 + $this->add_help_option = $params['add_help_option'];
  268 + }
  269 + if (isset($params['force_posix'])) {
  270 + $this->force_posix = $params['force_posix'];
  271 + } else if (getenv('POSIXLY_CORRECT')) {
  272 + $this->force_posix = true;
  273 + }
  274 + // set default instances
  275 + $this->renderer = new PEAR2_Console_CommandLine_Renderer_Default($this);
  276 + $this->outputter = new PEAR2_Console_CommandLine_Outputter_Default();
  277 + $this->message_provider = new PEAR2_Console_CommandLine_MessageProvider_Default();
  278 + }
  279 +
  280 + // }}}
  281 + // accept() {{{
  282 +
  283 + /**
  284 + * Method to allow PEAR2_Console_CommandLine to accept either:
  285 + * + a custom renderer,
  286 + * + a custom outputter,
  287 + * + or a custom message provider
  288 + *
  289 + * @param mixed $instance the custom instance
  290 + *
  291 + * @access public
  292 + * @return void
  293 + * @throws PEAR2_Console_CommandLine_Exception if wrong argument passed
  294 + */
  295 + public function accept($instance)
  296 + {
  297 + if ($instance instanceof PEAR2_Console_CommandLine_Renderer) {
  298 + if (property_exists($instance, 'parser') && !$instance->parser) {
  299 + $instance->parser = $this;
  300 + }
  301 + $this->renderer = $instance;
  302 + } else if ($instance instanceof PEAR2_Console_CommandLine_Outputter) {
  303 + $this->outputter = $instance;
  304 + } else if ($instance instanceof PEAR2_Console_CommandLine_MessageProvider) {
  305 + $this->message_provider = $instance;
  306 + } else {
  307 + throw PEAR2_Console_CommandLine_Exception::build(
  308 + 'INVALID_CUSTOM_INSTANCE',
  309 + array(),
  310 + $this
  311 + );
  312 + }
  313 + }
  314 +
  315 + // }}}
  316 + // fromXmlFile() {{{
  317 +
  318 + /**
  319 + * Return a command line parser instance built from an xml file.
  320 + *
  321 + * Example:
  322 + * <code>
  323 + * require_once 'Console/CommandLine.php';
  324 + * $parser = PEAR2_Console_CommandLine::fromXmlFile('path/to/file.xml');
  325 + * $result = $parser->parse();
  326 + * </code>
  327 + *
  328 + * @param string $file path to the xml file
  329 + *
  330 + * @return object a PEAR2_Console_CommandLine instance
  331 + * @access public
  332 + * @static
  333 + */
  334 + public static function fromXmlFile($file)
  335 + {
  336 + return PEAR2_Console_CommandLine_XmlParser::parse($file);
  337 + }
  338 +
  339 + // }}}
  340 + // fromXmlString() {{{
  341 +
  342 + /**
  343 + * Return a command line parser instance built from an xml string.
  344 + *
  345 + * Example:
  346 + * <code>
  347 + * require_once 'Console/CommandLine.php';
  348 + * $xmldata = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
  349 + * <command>
  350 + * <description>Compress files</description>
  351 + * <option name="quiet">
  352 + * <short_name>-q</short_name>
  353 + * <long_name>--quiet</long_name>
  354 + * <description>be quiet when run</description>
  355 + * <action>StoreTrue/action>
  356 + * </option>
  357 + * <argument name="files">
  358 + * <description>a list of files</description>
  359 + * <multiple>true</multiple>
  360 + * </argument>
  361 + * </command>';
  362 + * $parser = PEAR2_Console_CommandLine::fromXmlString($xmldata);
  363 + * $result = $parser->parse();
  364 + * </code>
  365 + *
  366 + * @param string $string the xml data
  367 + *
  368 + * @return object a PEAR2_Console_CommandLine instance
  369 + * @access public
  370 + * @static
  371 + */
  372 + public static function fromXmlString($string)
  373 + {
  374 + return PEAR2_Console_CommandLine_XmlParser::parseString($string);
  375 + }
  376 +
  377 + // }}}
  378 + // addArgument() {{{
  379 +
  380 + /**
  381 + * Add an argument with the given $name to the command line parser.
  382 + *
  383 + * Example:
  384 + * <code>
  385 + * $parser = new PEAR2_Console_CommandLine();
  386 + * // add an array argument
  387 + * $parser->addArgument('input_files', array('multiple'=>true));
  388 + * // add a simple argument
  389 + * $parser->addArgument('output_file');
  390 + * $result = $parser->parse();
  391 + * print_r($result->args['input_files']);
  392 + * print_r($result->args['output_file']);
  393 + * // will print:
  394 + * // array('file1', 'file2')
  395 + * // 'file3'
  396 + * // if the command line was:
  397 + * // myscript.php file1 file2 file3
  398 + * </code>
  399 + *
  400 + * In a terminal, the help will be displayed like this:
  401 + * <code>
  402 + * $ myscript.php install -h
  403 + * Usage: myscript.php <input_files...> <output_file>
  404 + * </code>
  405 + *
  406 + * @param mixed $name a string containing the argument name or an
  407 + * instance of PEAR2_Console_CommandLine_Argument
  408 + * @param array $params an array containing the argument attributes
  409 + *
  410 + * @return object PEAR2_Console_CommandLine_Argument
  411 + * @access public
  412 + * @see PEAR2_Console_CommandLine_Command
  413 + */
  414 + public function addArgument($name, $params=array())
  415 + {
  416 + if ($name instanceof PEAR2_Console_CommandLine_Argument) {
  417 + $argument = $name;
  418 + } else {
  419 + $argument = new PEAR2_Console_CommandLine_Argument($name, $params);
  420 + }
  421 + $argument->validate();
  422 + $this->args[$argument->name] = $argument;
  423 + return $argument;
  424 + }
  425 +
  426 + // }}}
  427 + // addCommand() {{{
  428 +
  429 + /**
  430 + * Add a sub-command to the command line parser.
  431 + *
  432 + * Add a command with the given $name to the parser and return the
  433 + * PEAR2_Console_CommandLine_Command instance, you can then populate the command
  434 + * with options, configure it, etc... like you would do for the main parser
  435 + * because the class PEAR2_Console_CommandLine_Command inherits from
  436 + * PEAR2_Console_CommandLine.
  437 + *
  438 + * An example:
  439 + * <code>
  440 + * $parser = new PEAR2_Console_CommandLine();
  441 + * $install_cmd = $parser->addCommand('install');
  442 + * $install_cmd->addOption(
  443 + * 'verbose',
  444 + * array(
  445 + * 'short_name' => '-v',
  446 + * 'long_name' => '--verbose',
  447 + * 'description' => 'be noisy when installing stuff',
  448 + * 'action' => 'StoreTrue'
  449 + * )
  450 + * );
  451 + * $parser->parse();
  452 + * </code>
  453 + * Then in a terminal:
  454 + * <code>
  455 + * $ myscript.php install -h
  456 + * Usage: myscript.php install [options]
  457 + *
  458 + * Options:
  459 + * -h, --help display this help message and exit
  460 + * -v, --verbose be noisy when installing stuff
  461 + *
  462 + * $ myscript.php install --verbose
  463 + * Installing whatever...
  464 + * $
  465 + * </code>
  466 + *
  467 + * @param mixed $name a string containing the command name or an
  468 + * instance of PEAR2_Console_CommandLine_Command
  469 + * @param array $params an array containing the command attributes
  470 + *
  471 + * @return object PEAR2_Console_CommandLine_Command
  472 + * @access public
  473 + * @see PEAR2_Console_CommandLine_Command
  474 + */
  475 + public function addCommand($name, $params=array())
  476 + {
  477 + if ($name instanceof PEAR2_Console_CommandLine_Command) {
  478 + $command = $name;
  479 + } else {
  480 + $params['name'] = $name;
  481 + $command = new PEAR2_Console_CommandLine_Command($params);
  482 + }
  483 + $command->parent = $this;
  484 + $this->commands[$command->name] = $command;
  485 + return $command;
  486 + }
  487 +
  488 + // }}}
  489 + // addOption() {{{
  490 +
  491 + /**
  492 + * Add an option to the command line parser.
  493 + *
  494 + * Add an option with the name (variable name) $optname and set its
  495 + * attributes with the array $params, then return the
  496 + * PEAR2_Console_CommandLine_Option instance created.
  497 + * The method accepts another form: you can directly pass a
  498 + * PEAR2_Console_CommandLine_Option object as the sole argument, this allows to
  499 + * contruct the option separately, in order to reuse an option in
  500 + * different command line parsers or commands for example.
  501 + *
  502 + * Example:
  503 + * <code>
  504 + * $parser = new PEAR2_Console_CommandLine();
  505 + * $parser->addOption('path', array(
  506 + * 'short_name' => '-p', // a short name
  507 + * 'long_name' => '--path', // a long name
  508 + * 'description' => 'path to the dir', // a description msg
  509 + * 'action' => 'StoreString',
  510 + * 'default' => '/tmp' // a default value
  511 + * ));
  512 + * $parser->parse();
  513 + * </code>
  514 + *
  515 + * In a terminal, the help will be displayed like this:
  516 + * <code>
  517 + * $ myscript.php --help
  518 + * Usage: myscript.php [options]
  519 + *
  520 + * Options:
  521 + * -h, --help display this help message and exit
  522 + * -p, --path path to the dir
  523 + *
  524 + * </code>
  525 + *
  526 + * Various methods to specify an option, these 3 commands are equivalent:
  527 + * <code>
  528 + * $ myscript.php --path=some/path
  529 + * $ myscript.php -p some/path
  530 + * $ myscript.php -psome/path
  531 + * </code>
  532 + *
  533 + * @param mixed $name a string containing the option name or an
  534 + * instance of PEAR2_Console_CommandLine_Option
  535 + * @param array $params an array containing the option attributes
  536 + *
  537 + * @return object PEAR2_Console_CommandLine_Option
  538 + * @access public
  539 + * @see PEAR2_Console_CommandLine_Option
  540 + */
  541 + public function addOption($name, $params=array())
  542 + {
  543 + if ($name instanceof PEAR2_Console_CommandLine_Option) {
  544 + $opt = $name;
  545 + } else {
  546 + $opt = new PEAR2_Console_CommandLine_Option($name, $params);
  547 + }
  548 + $opt->validate();
  549 + $this->options[$opt->name] = $opt;
  550 + return $opt;
  551 + }
  552 +
  553 + // }}}
  554 + // displayError() {{{
  555 +
  556 + /**
  557 + * Display an error to the user and exit with $exitCode.
  558 + *
  559 + * @param string $error the error message
  560 + * @param int $exitCode the exit code number
  561 + *
  562 + * @return void
  563 + * @access public
  564 + */
  565 + public function displayError($error, $exitCode = 1)
  566 + {
  567 + $this->outputter->stderr($this->renderer->error($error));
  568 + exit($exitCode);
  569 + }
  570 +
  571 + // }}}
  572 + // displayUsage() {{{
  573 +
  574 + /**
  575 + * Display the usage help message to the user and exit with $exitCode
  576 + *
  577 + * @param int $exitCode the exit code number
  578 + *
  579 + * @return void
  580 + * @access public
  581 + */
  582 + public function displayUsage($exitCode = 1)
  583 + {
  584 + $this->outputter->stderr($this->renderer->usage());
  585 + exit($exitCode);
  586 + }
  587 +
  588 + // }}}
  589 + // displayVersion() {{{
  590 +
  591 + /**
  592 + * Display the program version to the user
  593 + *
  594 + * @return void
  595 + * @access public
  596 + */
  597 + public function displayVersion()
  598 + {
  599 + $this->outputter->stdout($this->renderer->version());
  600 + exit(0);
  601 + }
  602 +
  603 + // }}}
  604 + // findOption() {{{
  605 +
  606 + /**
  607 + * Find the option that matches the given short_name (ex: -v), long_name
  608 + * (ex: --verbose) or name (ex: verbose).
  609 + *
  610 + * @param string $str the option identifier
  611 + *
  612 + * @return mixed a PEAR2_Console_CommandLine_Option instance or false
  613 + * @access public
  614 + */
  615 + public function findOption($str)
  616 + {