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


// Definition of the options to check
$getopts = new 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
-i: DebugLevel, --
-o: filename, --output: filename
  Output File