| ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot |
Managing command line optionsManaging command line options –
how to add options to the parser and use them.
Some backgroundOptions are used to provide extra information to tune or customize the execution of a program. In case it wasn't clear, options are usually optional. A program should be able to run just fine with no options whatsoever. Pick a random program from the Unix or GNU toolsets. Can it run without any options at all and still make sense? The main exceptions are find, tar, and dd--all of which are mutant oddballs that have been rightly criticized for their non-standard syntax and confusing interfaces. Lots of people want their programs to have "required options". Think about it. If it's required, then it's not optional! If there is a piece of information that your program absolutely requires in order to run successfully, that's what arguments are for. As an example of good command-line interface design, consider the humble cp utility, for copying files. It doesn't make much sense to try to copy files without supplying a destination and at least one source. Hence, cp fails if you run it with no arguments. However, it has a flexible, useful syntax that does not require any options at all: $ cp SOURCE DEST $ cp SOURCE ... DEST-DIR
You can get pretty far with just that. Most cp implementations provide a bunch of options to tweak exactly how the files are copied: you can preserve mode and modification time, avoid following symlinks, ask before clobbering existing files, etc. But none of this distracts from the core mission of cp, which is to copy either one file to another, or several files to another directory. Adding options with Console_CommandLineTo add options to your parser, just create the parser as explained in the previous section and use the Console_CommandLine::addOption() method. The Console_CommandLine::addOption() method takes two arguments:
Adding commandline options
<?php
Now if the user type:
$ <yourprogram> -vo rtl
or (equivalent):
$ <yourprogram> --verbose --orientation=rtl
The output of the above script will be:
Array ( [verbose] => 1 [orientation] => rtl [help] => [version] => )
Options actionsActions tell the parser how to handle option values, among other things they tell the parser if the option expects a value or not and how to store this value in the result object. StoreTrueThis action tells the parser to store the value true in the result object if the option is present in the command line, for example:
$ <yourprogram> -v
will store TRUE in $result->options['verbose'], assuming the option was defined like this:
<?php
StoreFalseThis action tells the parser to store the value false in the result object if the option is present in the command line, for example:
$ <yourprogram> -q
will store FALSE in $result->options['verbose'], assuming the option was defined like this:
<?php
StoreStringThis action tells the parser that the option expects a value and to store this value as a string in the result object, for example:
$ <yourprogram> -o out.txt
will store the string "out.txt" in $result->options['outfile'], assuming the option was defined like this:
<?php
StoreIntThis action tells the parser that the option expects a value and to store this value as an integer in the result object, for example:
$ <yourprogram> --width=500
will store the integer 500 in $result->options['width'], assuming the option was defined like this:
<?php
StoreFloatThis action tells the parser that the option expects a value and to store this value as a float in the result object, for example:
$ <yourprogram> -l=0.3
will store the float 0.3 in $result->options['level'], assuming the option was defined like this:
<?php
CounterThis action tells the parser to increment the value in the result object each time it encounters the option in the command line, for example:
$ <yourprogram> -vvvv
or the equivalent:
$ <yourprogram> -v -v -v --verbose
will store the integer 4 in $result->options['verbose_level'], assuming the option was defined like this:
<?php
HelpThis action tells the parser to display the help message if it encounters the option in the command line, most of the time you won't need this since it is handled by Console_CommandLine internally. VersionThis action tells the parser to display the program version if it encounters the option in the command line, as for Help action, chances are that you won't need this since it is handled by Console_CommandLine internally. PasswordThis action allows the user to either type the password on the commandline or to be prompted for it (will not echo on unix systems), some examples:
<?php
$ <yourprogram> -ps3cret
will store the string "s3ecret" in $result->options['password'] whereas:
$ <yourprogram> -p
will "prompt" the user for entering his/her password without echoing it, and will store "s3ecret" in $result->options['password'] CallbackThis action allows to specify a PHP callback to handle user input. The callback must be a php callable and must accept five arguments:
Your callback function must return the modified (or not modified) value (the first argument). All these arguments should give you enough flexibility to build complex callback actions. Here is a simple example:
<?php
Now if the user type:
$ <yourprogram> -e foobar
The output of the above script will be:
7f12da3b1c126d7a47745b09dc0040c92cee1700
Lista special action that simply output an array as a list.
<?php
$ <yourprogram> --list-colors
will display the list of colors separated by commas. |
各種マニュアル:
PHPマニュアル |
PEARマニュアル |
Smarty(英語)マニュアル |
PHP-GTKマニュアル |
「Managing command line options」をGoogle検索
|