Back to the module list

Command line parameters

The options provided to the command permit to configure the software. They are really important, must be placed in any order, must support multiple times options

Usage

// Definition of the options to check
$getopts = new Domframework\Getopts ();
// The parameters to add method are :
// - The identifier of the options
// - The short options (one letter with/without semi-colon) to check
// - The long options without double dash, with/without semi-colon to check.
//   If string, use colon to separate the options
// - The description of the option to use in help
// - The description (or name) of the parameter
// - The same identifier can be called multiple time.
//   Set $multiple to set the maximum number of values
$getopts->add ("Help", "?h", array ("help","help2"), "Help of the software");
// Allow one -f with mandatory filename
$getopts->add ("ReadFile", "f:", "file:,joue::", "Read the file", "filename");
// Allow 2 -d maximum
$getopts->add ("Debug", "d", "debug", "Debug", "DebugLevel", 2);
// Allow 3 -o maximum with a mandatory filename
$getopts->add ("OutputFile", "o:", "output:", "Output File", "filename", 3);

// Read a value : return false if the value is not set
// Return an true if the value is set
// The help method return the help page
if ($getopts->get ("Help"))
  die ($getopts->help ());

// Read a value : return an empty array if debug is not set,
// Return an array with one "true" if -d is set one time
// Return an array with two "true" if -d is set two times
var_dump ($getopts->get ("Debug"));

Create the help message automatically

echo $getopts->help ();
-?, -h, --help, --help2
  Help of the software
-f: filename, --file: filename, --joue:: [filename]
  Read the file
-d, --debug
  Debug
-i: DebugLevel, --
  Debug
-o: filename, --output: filename
  Output File

The class definition

Class Domframework\Getopts

Namespace Domframework

Description

/**
 Manage the options provided on the command line
 Parameters can be :
 - Manage -d -dd -ddd as -d -d -d
 - Manage mandatory parameter to argument: -f file (-f: in getopt)
 - Manage optional parameter to argument: -b (-b:: in getopt)
 The long options start with two dashes, the short start with one dash and
 can only be one letter
 If the token is "--", the following value can start by dash

Properties

No property available

Methods

public function __construct ()
/**
 The constructor check the availability of the MB module

public function add ( $identifier, $short, $long, $description, $paramName="", $multiple=0)
/**
 Add a new option to check
 @param string $identifier The identifier of the options
 @param string $short The short options (one letter with/without
 semi-colon) to check
 @param array|string $long The long options without double dash,
 with/without semi-colon to check. If string, use colon to separate the
 options
 @param string $description The description of the option
 @param string $paramName The description (or name) of the parameter
 @param integer $multiple The same identifier can be called multiple time.
 Set $multiple to set the maximum number of values

public function get ( $identifier)
/**
 Get the value of the option if set in the command line. If simulate is
 defined, use it.
 Return false if the option is not set
 Return true if the option is set without parameter
 Return the content of the parameter if the option is set and parameter is
 defined
 Throw an exception if the identifier is not set
 @param string $identifier The identifier option to get

public function help ()
/**
 Get the Help message with all the descriptions and options

public function programName ()
/**
 Get the name of the program found in the command line

public function restOfLine ()
/**
 Get the value found in the rest of line (after the double dashes)

public function scan ()
/**
 Scan the command line and fill the parameters.
 Use the simulate line if provided or use the $argv if not
 Set the parameters property to an array
 @return $this;

public function simulate ( $simulate=null)
/**
 Set/Get the simulate value
 @param string|null $simulate The simulate to get/set