Examples

Examples – various code examples

Basic example

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

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>

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());
}
?>

Sub-commands example

Sub-commands example

<?php
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';

// create the parser
$parser = new Console_CommandLine(array(
    
'description' => 'A great program that can foo and bar !',
    
'version'     => '1.0.0'
));

// add a global option to make the program verbose
$parser->addOption('verbose', array(
    
'short_name'  => '-v',
    
'long_name'   => '--verbose',
    
'action'      => 'StoreTrue',
    
'description' => 'turn on verbose output'
));

// add the foo subcommand
$foo_cmd $parser->addCommand('foo', array(
    
'description' => 'output the given string with a foo prefix'
));
$foo_cmd->addOption('reverse', array(
    
'short_name'  => '-r',
    
'long_name'   => '--reverse',
    
'action'      => 'StoreTrue',
    
'description' => 'reverse the given string before echoing it'
));
$foo_cmd->addArgument('text', array(
    
'description' => 'the text to output'
));

// add the bar subcommand
$bar_cmd $parser->addCommand('bar', array(
    
'description' => 'output the given string with a bar prefix'
));
$bar_cmd->addOption('reverse', array(
    
'short_name'  => '-r',
    
'long_name'   => '--reverse',
    
'action'      => 'StoreTrue',
    
'description' => 'reverse the given string before echoing it'
));
$bar_cmd->addArgument('text', array(
    
'description' => 'the text to output'
));

// run the parser
try {
    
$result $parser->parse();
    if (
$result->command_name) {
        
$st $result->command->options['reverse'
            ? 
strrev($result->command->args['text'])
            : 
$result->command->args['text'];
        if (
$result->command_name == 'foo') { 
            echo 
"Foo says: $st\n";
        } else if (
$result->command_name == 'bar') {
            echo 
"Bar says: $st\n";
        }
    }
} catch (
Exception $exc) {
    
$parser->displayError($exc->getMessage());
}

?>

Sub-commands example using an XML definition file

Sub-commands example (XML file)

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<command>
    <description>A great program that can foo and bar !</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>
    <command>
        <name>foo</name>
        <description>output the given string with a foo prefix</description>
        <option name="reverse">
            <short_name>-r</short_name>
            <long_name>--reverse</long_name>
            <description>reverse the string before echoing it</description>
            <action>StoreTrue</action>
        </option>
        <argument name="text">
            <description>the text to output</description>
        </argument>
    </command>
    <command>
        <name>bar</name>
        <description>output the given string with a bar prefix</description>
        <option name="reverse">
            <short_name>-r</short_name>
            <long_name>--reverse</long_name>
            <description>reverse the string before echoing it</description>
            <action>StoreTrue</action>
        </option>
        <argument name="text">
            <description>the text to output</description>
        </argument>
    </command>
</command>

Sub-commands example (PHP file)

<?php
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';

// create the parser
$xmlfile dirname(__FILE__) . DIRECTORY_SEPARATOR 'ex4.xml';
$parser  Console_CommandLine::fromXmlFile($xmlfile);

// run the parser
try {
    
$result $parser->parse();
    if (
$result->command_name) {
        
$st $result->command->options['reverse'
            ? 
strrev($result->command->args['text'])
            : 
$result->command->args['text'];
        if (
$result->command_name == 'foo') { 
            echo 
"Foo says: $st\n";
        } else if (
$result->command_name == 'bar') {
            echo 
"Bar says: $st\n";
        }
    }
} catch (
Exception $exc) {
    
$parser->displayError($exc->getMessage());
}

?>
How to use the same script from your browser (Previous) Console_Getopt (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.