[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/splitbrain/php-cli/src/ -> Options.php (summary)

(no description)

File Size: 470 lines (15 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Options:: (12 methods):
  __construct()
  setHelp()
  registerArgument()
  registerCommand()
  registerOption()
  checkArguments()
  parseOptions()
  getOpt()
  getCmd()
  getArgs()
  help()
  readPHPArgv()


Class: Options  - X-Ref

Class Options

Parses command line options passed to the CLI script. Allows CLI scripts to easily register all accepted options and
commands and even generates a help text from this setup.

__construct(Colors $colors = null)   X-Ref
Constructor

param: Colors $colors optional configured color object

setHelp($help)   X-Ref
Sets the help text for the tool itself

param: string $help

registerArgument($arg, $help, $required = true, $command = '')   X-Ref
Register the names of arguments for help generation and number checking

This has to be called in the order arguments are expected

param: string $arg argument name (just for help)
param: string $help help text
param: bool $required is this a required argument
param: string $command if theses apply to a sub command only

registerCommand($command, $help)   X-Ref
This registers a sub command

Sub commands have their own options and use their own function (not main()).

param: string $command
param: string $help

registerOption($long, $help, $short = null, $needsarg = false, $command = '')   X-Ref
Register an option for option parsing and help generation

param: string $long multi character option (specified with --)
param: string $help help text for this option
param: string|null $short one character option (specified with -)
param: bool|string $needsarg does this option require an argument? give it a name here
param: string $command what command does this option apply to

checkArguments()   X-Ref
Checks the actual number of arguments against the required number

Throws an exception if arguments are missing.

This is run from CLI automatically and usually does not need to be called directly


parseOptions()   X-Ref
Parses the given arguments for known options and command

The given $args array should NOT contain the executed file as first item anymore! The $args
array is stripped from any options and possible command. All found otions can be accessed via the
getOpt() function

Note that command options will overwrite any global options with the same name

This is run from CLI automatically and usually does not need to be called directly


getOpt($option = null, $default = false)   X-Ref
Get the value of the given option

Please note that all options are accessed by their long option names regardless of how they were
specified on commandline.

Can only be used after parseOptions() has been run

return: bool|string|string[]
param: mixed $option
param: bool|string $default what to return if the option was not set

getCmd()   X-Ref
Return the found command if any

return: string

getArgs()   X-Ref
Get all the arguments passed to the script

This will not contain any recognized options or the script name itself

return: array

help()   X-Ref
Builds a help screen from the available options. You may want to call it from -h or on error

return: string

readPHPArgv()   X-Ref
Safely read the $argv PHP array across different PHP configurations.
Will take care on register_globals and register_argc_argv ini directives

return: array the $argv PHP array or PEAR error if not registered