Examples -- various code examples
Basic example
Exemple 38-1. Basic example
<?php
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';
// create the parser
$parser = new Console_CommandLine(array(
'description' => 'zip given files using the php zip module.',
'version' => '1.0.0'
));
// add an option to make the program verbose
$parser->addOption(
'verbose',
array(
'short_name' => '-v',
'long_name' => '--verbose',
'action' => 'StoreTrue',
'description' => 'turn on verbose output'
)
);
// add an option to delete original files after zipping
$parser->addOption(
'delete',
array(
'short_name' => '-d',
'long_name' => '--delete',
'action' => 'StoreTrue',
'description' => 'delete original files after zip operation'
)
);
// add the files argument, the user can specify one or several files
$parser->addArgument(
'files',
array(
'multiple' => true,
'description' => 'list of files to zip separated by spaces'
)
);
// add the zip file name argument
$parser->addArgument('zipfile', array('description' => 'zip file name'));
// run the parser
try {
$result = $parser->parse();
// write your program here...
print_r($result->options);
print_r($result->args);
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}
?>
|
|
Basic example using an XML definition file
Exemple 38-2. Basic example (XML file) <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<command>
<description>zip given files using the php zip module.</description>
<version>1.0.0</version>
<option name="verbose">
<short_name>-v</short_name>
<long_name>--verbose</long_name>
<description>turn on verbose output</description>
<action>StoreTrue</action>
</option>
<option name="delete">
<short_name>-d</short_name>
<long_name>--delete</long_name>
<description>delete original files after zip operation</description>
<action>StoreTrue</action>
</option>
<argument name="files">
<description>a list of files to zip together</description>
<multiple>true</multiple>
</argument>
<argument name="zipfile">
<description>path to the zip file to generate</description>
</argument>
</command> |
|
Exemple 38-3. Basic example (PHP file)
<?php
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';
// create the parser from xml file
$xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex2.xml';
$parser = Console_CommandLine::fromXmlFile($xmlfile);
// run the parser
try {
$result = $parser->parse();
// todo: your program here ;)
print_r($result->options);
print_r($result->args);
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}
?>
|
|