annotate vendor/pear/console_commandline/docs/examples/ex3.php @ 43:771f6803cc4b default tip

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