Tag Archives: PEAR

PEAR::Console_CommandLine

With minor bugfixes the stable version 1.1.1 of PEAR::Console_CommandLine was recently released. The package provides full featured command line options and arguments parser and can easily be included in your application. The parser can be built by passing an array of options to the constructor or by setting the public properties of an instantiated parser object.

require_once 'Console/CommandLine.php';

$parser = new Console_CommandLine();
$parser -> description = 'Dump an OWL (Document Management System) 
Database into a corresponding file system';
$parser -> version = '0.9.1';

$parser -> addOption('simulation', array(
    'short_name'  => '-s',
    'long_name'   => '--simulation',
    'description' => 'simulate dump, do not write into filesystem',
    'action'      => 'StoreTrue'
));

$parser -> addOption('verbose', array(
    'short_name'  => '-v',
    'long_name'   => '--verbose',
    'description' => "print status messages to stdout",
    'action'      => 'StoreTrue'
));

The command line:

$>php owl2fs.php --help

Dump an OWL (Document Management System) 
Database into a corresponding file system

Usage:
  owl2fs.php [options]

Options:
  -s, --simulation  simulate dump, do not write into filesystem
  -v, --verbose     print status messages to stdout
  -h, --help        show this help message and exit
  --version         show the program version and exit

Using the fromXmlFile() or fromXmlString() methods, options can be passed in the XML format.

The ‘action’ key in the options section allows you to pass several datatypes and can as well prompt for a password. User input can be passed to a callback function, e.g. for validation.

To give a clear structure to your interface, subcommands can easily be created using the addCommand() method:

$subcommand = $parser -> addCommand('extract', array(
   'description' => 'extract content from pdf files'
));

$subcommand -> addOption('images', array(
   'short_name'  => '-i',
   'long_name'   => '--image',
   'action'      => 'StoreTrue',
   'description' => 'extract images from pdf files'
));

$subcommand -> addOption('text', array(
   'short_name'  => '-t',
   'long_name'   => '--text',
   'action'      => 'StoreTrue',
   'description' => 'extract text from pdf files'
));

The command line:

$>php owl2fs.php --help

Dump an OWL (Document Management System) Database 
into a corresponding file system

Usage:
  testfile5.php [options]
  testfile5.php [options] <command> [options]

Options:
  -s, --simulation  simulate dump, do not write into filesystem
  -v, --verbose     print status messages to stdout
  -h, --help        show this help message and exit
  --version         show the program version and exit

Commands:
  extract  extract content from pdf files

To get information about the subcommand, enter:

$>php owl2fs.php extract --help

extract content from pdf files

Usage:
  owls2fs.php [options] extract [options]

Options:
  -i, --image  extract images from pdf files
  -t, --text   extract text from pdf files
  -h, --help   show this help message and exit

Console_CommandLine can as well be used in web environments as it accesses the HTTP $_GET and $_POST variables. So using e.g.

http://myApp.php?name=value

, ‘value’ will be passed to an option ‘name’ defined with

$parser->addOption(
'name', array(...))

A valuable documentation with many examples can be found on the project docs page