| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
|
|
3 |
// +----------------------------------------------------------------------+
|
|
|
4 |
// | PHP Version 4 |
|
|
|
5 |
// +----------------------------------------------------------------------+
|
|
|
6 |
// | Copyright (c) 2004 The PHP Group |
|
|
|
7 |
// +----------------------------------------------------------------------+
|
|
|
8 |
// | This source file is subject to version 3.0 of the PHP license, |
|
|
|
9 |
// | that is bundled with this package in the file LICENSE, and is |
|
|
|
10 |
// | available through the world-wide-web at the following url: |
|
|
|
11 |
// | http://www.php.net/license/3_0.txt. |
|
|
|
12 |
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
13 |
// | obtain it through the world-wide-web, please send a note to |
|
|
|
14 |
// | license@php.net so we can mail you a copy immediately. |
|
|
|
15 |
// +----------------------------------------------------------------------+
|
|
|
16 |
// | Author: Scott Mattocks <scottmattocks@php.net> |
|
|
|
17 |
// +----------------------------------------------------------------------+
|
|
|
18 |
//
|
|
|
19 |
// $Id: example2.php 222944 2006-11-07 13:53:37Z scottmattocks $
|
|
|
20 |
/**
|
|
|
21 |
* Example usage script for Console_Getargs.
|
|
|
22 |
*
|
|
|
23 |
* Console_Getargs is a class to help define and parse commandline
|
|
|
24 |
* options. The goal is to easily identify arguments a user may pass
|
|
|
25 |
* to an application run from the commandline. By specifying option
|
|
|
26 |
* names and rules, the applicatioin programmer can be sure that a
|
|
|
27 |
* user has given the application the needed information before any
|
|
|
28 |
* processing begins.
|
|
|
29 |
*
|
|
|
30 |
* This file attempts to give the reader an understanding of the
|
|
|
31 |
* usage and power of Console_Getargs through an example. At the
|
|
|
32 |
* bottom of this file you will see several commented commands
|
|
|
33 |
* for running this script. While this script aims at being as
|
|
|
34 |
* thorough as possible, it is by no means a complete set of the
|
|
|
35 |
* possible combinations that could be used with Console_Getargs.
|
|
|
36 |
*
|
|
|
37 |
* @author Bertrand Mansion <bmansion@mamasam.com>
|
|
|
38 |
* @author Scott Mattocks <scottmattocks@php.net>
|
|
|
39 |
* @copyright 2004
|
|
|
40 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
41 |
* @version @VER@
|
|
|
42 |
* @package Console_Getargs
|
|
|
43 |
*/
|
|
|
44 |
error_reporting(E_ALL);
|
|
|
45 |
/**
|
|
|
46 |
* Dummy class for example.
|
|
|
47 |
*
|
|
|
48 |
* This class returns the number of lines in a given file. It can
|
|
|
49 |
* optionally give you the first X lines, plus "highlight" certain
|
|
|
50 |
* words by surrounding them in '*'. When told to, this class can
|
|
|
51 |
* get very chatty.
|
|
|
52 |
*
|
|
|
53 |
* Please note that this class is not intended to be overly useful
|
|
|
54 |
* or efficient. It is meant simply to demonstrate the capabilities
|
|
|
55 |
* of Console_Getargs. It may be neccessary to change some paths
|
|
|
56 |
* depending on how you have installed Console_Getargs and the setup
|
|
|
57 |
* of your system.
|
|
|
58 |
*
|
|
|
59 |
* Run the following commands to see how to use Console_Getargs:
|
|
|
60 |
*
|
|
|
61 |
* $ php -q example2.php -h
|
|
|
62 |
* $ php -q example2.php
|
|
|
63 |
* $ php -q example2.php -v 1
|
|
|
64 |
* $ php -q example2.php -v
|
|
|
65 |
* $ php -q example2.php -dv
|
|
|
66 |
* $ php -q example2.php -dvs 10
|
|
|
67 |
* $ php -q example2.php -vs 10 -f if
|
|
|
68 |
* $ php -q example2.php -vs 10 -hi if
|
|
|
69 |
* $ php -q example2.php -vs 10 -f if file
|
|
|
70 |
* $ php -q example2.php -s 10 ../Getargs.php
|
|
|
71 |
* $ php -q example2.php -s 10 -f if file --parameters=../Getargs.php
|
|
|
72 |
*
|
|
|
73 |
* Also try creating your own array of arguments and passing it to the
|
|
|
74 |
* example instance on construction. You array should be numerically
|
|
|
75 |
* indexed. Here are a few examples:
|
|
|
76 |
* array('-d', '-vs', '10')
|
|
|
77 |
* array('--debug', '--verbose', '../Getargs.php');
|
|
|
78 |
*/
|
|
|
79 |
class Example {
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Show debug output.
|
|
|
83 |
* @var boolean
|
|
|
84 |
*/
|
|
|
85 |
var $debug;
|
|
|
86 |
/**
|
|
|
87 |
* The complete path to the file to count lines in.
|
|
|
88 |
* @var string
|
|
|
89 |
*/
|
|
|
90 |
var $file;
|
|
|
91 |
/**
|
|
|
92 |
* How many lines to show.
|
|
|
93 |
* @var integer
|
|
|
94 |
*/
|
|
|
95 |
var $showLines;
|
|
|
96 |
/**
|
|
|
97 |
* The level of chattyness.
|
|
|
98 |
* @var integer
|
|
|
99 |
*/
|
|
|
100 |
var $verbose;
|
|
|
101 |
/**
|
|
|
102 |
* The words to find and highlight.
|
|
|
103 |
* @var array
|
|
|
104 |
*/
|
|
|
105 |
var $findWords = array();
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Constructor.
|
|
|
109 |
*
|
|
|
110 |
* This will create an instance of the example object. If
|
|
|
111 |
* you pass an array of arguments on construction, that
|
|
|
112 |
* array will be used instead of the default agument list.
|
|
|
113 |
*
|
|
|
114 |
* @access public
|
|
|
115 |
* @param array $argArray The optional argument list.
|
|
|
116 |
* @return void
|
|
|
117 |
*/
|
|
|
118 |
function Example($argArray = NULL)
|
|
|
119 |
{
|
|
|
120 |
// Get the config array
|
|
|
121 |
require_once 'Console/Getargs.php';
|
|
|
122 |
$config = $this->getConfigArray();
|
|
|
123 |
|
|
|
124 |
// Get the arguments.
|
|
|
125 |
if (is_array($argArray)) {
|
|
|
126 |
// Use given arg list not $_SERVER['argv'].
|
|
|
127 |
$args =& Console_Getargs::factory($config, $argArray);
|
|
|
128 |
} else {
|
|
|
129 |
// Use $_SERVER['argv'].
|
|
|
130 |
$args =& Console_Getargs::factory($config);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// Check for errors.
|
|
|
134 |
if (PEAR::isError($args)) {
|
|
|
135 |
if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) {
|
|
|
136 |
// User put illegal values on the command line.
|
|
|
137 |
echo Console_Getargs::getHelp($config, NULL, $args->getMessage(), 78, 4)."\n";
|
|
|
138 |
} else if ($args->getCode() === CONSOLE_GETARGS_HELP) {
|
|
|
139 |
// User needs help.
|
|
|
140 |
echo Console_Getargs::getHelp($config, NULL, NULL, 78, 4)."\n";
|
|
|
141 |
}
|
|
|
142 |
exit;
|
|
|
143 |
} else {
|
|
|
144 |
// Assign the member vars.
|
|
|
145 |
$this->debug = $args->getValue('debug');
|
|
|
146 |
$this->file = $args->getValue(CONSOLE_GETARGS_PARAMS);
|
|
|
147 |
$this->showLines = $args->getValue('showlines');
|
|
|
148 |
$this->verbose = $args->getValue('verbose');
|
|
|
149 |
$this->findWords = $args->getValue('find');
|
|
|
150 |
|
|
|
151 |
// Make sure the file is readable.
|
|
|
152 |
if (!@is_readable($this->file)) {
|
|
|
153 |
$msg = $this->file . ' is not readable.';
|
|
|
154 |
echo Console_Getargs::getHelp($config, NULL, $msg) . "\n";
|
|
|
155 |
exit;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Return the config array.
|
|
|
162 |
*
|
|
|
163 |
* The config array is the set of rules for command line
|
|
|
164 |
* arguments. For more details please read the comments
|
|
|
165 |
* in Getargs.php
|
|
|
166 |
*
|
|
|
167 |
* @static
|
|
|
168 |
* @access public
|
|
|
169 |
* @param none
|
|
|
170 |
* @return &array
|
|
|
171 |
*/
|
|
|
172 |
function &getConfigArray()
|
|
|
173 |
{
|
|
|
174 |
$configArray = array();
|
|
|
175 |
|
|
|
176 |
// Allow the user to show debug output.
|
|
|
177 |
$configArray['debug'] = array('short' => 'db',
|
|
|
178 |
'max' => 0,
|
|
|
179 |
'desc' => 'Show debug output.'
|
|
|
180 |
);
|
|
|
181 |
// Make the program chatty.
|
|
|
182 |
$configArray['verbose'] = array('short' => 'v',
|
|
|
183 |
'min' => 0,
|
|
|
184 |
'max' => 1,
|
|
|
185 |
'desc' => 'Set the verbose level.',
|
|
|
186 |
'default' => 2
|
|
|
187 |
);
|
|
|
188 |
// How many lines should be shown.
|
|
|
189 |
$configArray['showlines'] = array('short' => 's',
|
|
|
190 |
'min' => 1,
|
|
|
191 |
'max' => 1,
|
|
|
192 |
'desc' => 'How many lines of the file should be shown.',
|
|
|
193 |
);
|
|
|
194 |
// What file should be used.
|
|
|
195 |
$configArray[CONSOLE_GETARGS_PARAMS] = array('min' => 1,
|
|
|
196 |
'max' => 1,
|
|
|
197 |
'desc' => 'The file to count lines from.',
|
|
|
198 |
'default' => basename(__FILE__)
|
|
|
199 |
);
|
|
|
200 |
// Show the help message.
|
|
|
201 |
// (Not really needed unless you want help to show up in the
|
|
|
202 |
// list of options in the help menu.)
|
|
|
203 |
$configArray['help'] = array('short' => 'h',
|
|
|
204 |
'max' => 0,
|
|
|
205 |
'desc' => 'Show this help.'
|
|
|
206 |
);
|
|
|
207 |
// Search for and highlight a word.
|
|
|
208 |
$configArray['find|highlight'] = array('short' => 'f|hi',
|
|
|
209 |
'max' => -1,
|
|
|
210 |
'min' => 0,
|
|
|
211 |
'desc' => 'Find words within the lines displayed. Words found will be changed from "word" to "*word*".'
|
|
|
212 |
);
|
|
|
213 |
return $configArray;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
/**
|
|
|
217 |
* The method for displaying the results.
|
|
|
218 |
*
|
|
|
219 |
* This will output atleast the number of lines. Depending on the
|
|
|
220 |
* options passed on the command line, more info may be shown.
|
|
|
221 |
*
|
|
|
222 |
* @access public
|
|
|
223 |
* @param none
|
|
|
224 |
* @return void
|
|
|
225 |
*/
|
|
|
226 |
function display()
|
|
|
227 |
{
|
|
|
228 |
// How chatty should the program be.
|
|
|
229 |
if ($this->verbose > 1) {
|
|
|
230 |
echo "Welcome to the Console_Getargs example2 script!\n";
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
if ($this->verbose > 0) {
|
|
|
234 |
echo basename($this->file) . ' has ';
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
// Spit out the number of lines.
|
|
|
238 |
echo $this->countLines();
|
|
|
239 |
|
|
|
240 |
if ($this->verbose > 0) {
|
|
|
241 |
echo ' lines.';
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
echo "\n";
|
|
|
245 |
|
|
|
246 |
// If the user wants to highlight ceratin words do it.
|
|
|
247 |
if (count($this->findWords)) {
|
|
|
248 |
// Chattyness.
|
|
|
249 |
if ($this->verbose) {
|
|
|
250 |
echo "Searching for:\t";
|
|
|
251 |
settype($this->findWords, 'array');
|
|
|
252 |
foreach ($this->findWords as $word) {
|
|
|
253 |
echo $word . ', ';
|
|
|
254 |
}
|
|
|
255 |
echo "\n";
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
// Spit out the requested number of lines.
|
|
|
259 |
echo $this->find($this->getFirstXLines());
|
|
|
260 |
} else {
|
|
|
261 |
// Just output the lines.
|
|
|
262 |
echo $this->getFirstXLines();
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Return the number of lines in the file.
|
|
|
268 |
*
|
|
|
269 |
* @access public
|
|
|
270 |
* @param none
|
|
|
271 |
* @return integer
|
|
|
272 |
*/
|
|
|
273 |
function countLines()
|
|
|
274 |
{
|
|
|
275 |
return count(file($this->file));
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Return the requested number of lines.
|
|
|
280 |
*
|
|
|
281 |
* Depending on the arguments passed on the command line, some
|
|
|
282 |
* words may be surrounded with '*'.
|
|
|
283 |
*
|
|
|
284 |
* @access public
|
|
|
285 |
* @param none
|
|
|
286 |
* @return string
|
|
|
287 |
*/
|
|
|
288 |
function getFirstXLines()
|
|
|
289 |
{
|
|
|
290 |
// Show debug output if requested.
|
|
|
291 |
if ($this->debug) {
|
|
|
292 |
echo "Getting all lines from file\t";
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
// Get the lines from the file.
|
|
|
296 |
$lines = file($this->file);
|
|
|
297 |
|
|
|
298 |
if ($this->debug) {
|
|
|
299 |
echo "OK\n";
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
$retString = '';
|
|
|
303 |
|
|
|
304 |
// Collect the lines.
|
|
|
305 |
if (count($lines) && !empty($this->showLines)) {
|
|
|
306 |
for ($i = 0; $i < $this->showLines; ++$i) {
|
|
|
307 |
if ($this->debug) {
|
|
|
308 |
echo $i . "\t" . $lines[$i] . "\n";
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
$retString.= $lines[$i];
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
return $retString;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Highlight the requested words.
|
|
|
320 |
*
|
|
|
321 |
* If the user wants some words highlighted, surround them in
|
|
|
322 |
* '*'.
|
|
|
323 |
*
|
|
|
324 |
* @access public
|
|
|
325 |
* @param string $text The text to find words in.
|
|
|
326 |
* @return string
|
|
|
327 |
*/
|
|
|
328 |
function find($text)
|
|
|
329 |
{
|
|
|
330 |
$highlighted = $text;
|
|
|
331 |
|
|
|
332 |
settype($this->findWords, 'array');
|
|
|
333 |
|
|
|
334 |
// Replace each word.
|
|
|
335 |
foreach ($this->findWords as $word) {
|
|
|
336 |
$highlighted = str_replace($word, '*' . $word . '*', $highlighted);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
return $highlighted;
|
|
|
340 |
}
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
// Create a new instance of the Example.
|
|
|
344 |
$ex = new Example();
|
|
|
345 |
|
|
|
346 |
// Show the results.
|
|
|
347 |
$ex->display();
|
|
|
348 |
/*
|
|
|
349 |
* Local variables:
|
|
|
350 |
* tab-width: 4
|
|
|
351 |
* c-basic-offset: 4
|
|
|
352 |
* End:
|
|
|
353 |
*/
|
|
|
354 |
?>
|