0
|
1 <?php
|
|
2
|
|
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
4
|
|
5 /**
|
|
6 * This file is part of the PEAR Console_CommandLine package.
|
|
7 *
|
|
8 * A simple example demonstrating the use of subcommands.
|
|
9 *
|
|
10 * PHP version 5
|
|
11 *
|
|
12 * LICENSE: This source file is subject to the MIT license that is available
|
|
13 * through the world-wide-web at the following URI:
|
|
14 * http://opensource.org/licenses/mit-license.php
|
|
15 *
|
|
16 * @category Console
|
|
17 * @package Console_CommandLine
|
|
18 * @author David JEAN LOUIS <izimobil@gmail.com>
|
|
19 * @copyright 2007 David JEAN LOUIS
|
|
20 * @license http://opensource.org/licenses/mit-license.php MIT License
|
|
21 * @version CVS: $Id$
|
|
22 * @link http://pear.php.net/package/Console_CommandLine
|
|
23 * @since File available since release 0.1.0
|
|
24 */
|
|
25
|
|
26 // Include the Console_CommandLine package.
|
|
27 require_once 'Console/CommandLine.php';
|
|
28
|
|
29 // create the parser
|
|
30 $parser = new Console_CommandLine(array(
|
|
31 'description' => 'A great program that can foo and bar !',
|
|
32 'version' => '1.0.0'
|
|
33 ));
|
|
34
|
|
35 // add a global option to make the program verbose
|
|
36 $parser->addOption('verbose', array(
|
|
37 'short_name' => '-v',
|
|
38 'long_name' => '--verbose',
|
|
39 'action' => 'StoreTrue',
|
|
40 'description' => 'turn on verbose output'
|
|
41 ));
|
|
42
|
|
43 // add the foo subcommand
|
|
44 $foo_cmd = $parser->addCommand('foo', array(
|
|
45 'description' => 'output the given string with a foo prefix'
|
|
46 ));
|
|
47 $foo_cmd->addOption('reverse', array(
|
|
48 'short_name' => '-r',
|
|
49 'long_name' => '--reverse',
|
|
50 'action' => 'StoreTrue',
|
|
51 'description' => 'reverse the given string before echoing it'
|
|
52 ));
|
|
53 $foo_cmd->addArgument('text', array(
|
|
54 'description' => 'the text to output'
|
|
55 ));
|
|
56
|
|
57 // add the bar subcommand with a "baz" alias
|
|
58 $bar_cmd = $parser->addCommand('bar', array(
|
|
59 'description' => 'output the given string with a bar prefix',
|
|
60 'aliases' => array('baz'),
|
|
61 ));
|
|
62 $bar_cmd->addOption('reverse', array(
|
|
63 'short_name' => '-r',
|
|
64 'long_name' => '--reverse',
|
|
65 'action' => 'StoreTrue',
|
|
66 'description' => 'reverse the given string before echoing it'
|
|
67 ));
|
|
68 $bar_cmd->addArgument('text', array(
|
|
69 'description' => 'the text to output'
|
|
70 ));
|
|
71
|
|
72 // run the parser
|
|
73 try {
|
|
74 $result = $parser->parse();
|
|
75 if ($result->command_name) {
|
|
76 $st = $result->command->options['reverse']
|
|
77 ? strrev($result->command->args['text'])
|
|
78 : $result->command->args['text'];
|
|
79 if ($result->command_name == 'foo') {
|
|
80 echo "Foo says: $st\n";
|
|
81 } else if ($result->command_name == 'bar') {
|
|
82 echo "Bar says: $st\n";
|
|
83 }
|
|
84 }
|
|
85 } catch (Exception $exc) {
|
|
86 $parser->displayError($exc->getMessage());
|
|
87 }
|
|
88
|
|
89 ?>
|