Overview

Namespaces

  • None
  • splitbrain
    • phpcli
      • tests

Classes

  • CLI
  • Colors
  • Options
  • PSR3CLI
  • TableFormatter

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class

Class Options

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.

Direct known subclasses

splitbrain\phpcli\tests\Options
Namespace: splitbrain\phpcli
License: MIT
Author: Andreas Gohr andi@splitbrain.org
Located at src/Options.php
Methods summary
public
# __construct( splitbrain\phpcli\Colors $colors = null )

Constructor

Constructor

Parameters

$colors
optional configured color object

Throws

splitbrain\phpcli\Exception
when arguments can't be read
public
# getBin( )

Gets the bin value

Gets the bin value

public
# setHelp( string $help )

Sets the help text for the tool itself

Sets the help text for the tool itself

Parameters

$help
public
# setCommandHelp( string $help )

Sets the help text for the tools commands itself

Sets the help text for the tools commands itself

Parameters

$help
public
# useCompactHelp( boolean $set = true )

Use a more compact help screen with less new lines

Use a more compact help screen with less new lines

Parameters

$set
public
# registerArgument( string $arg, string $help, boolean $required = true, string $command = '' )

Register the names of arguments for help generation and number checking

Register the names of arguments for help generation and number checking

This has to be called in the order arguments are expected

Parameters

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

Throws

splitbrain\phpcli\Exception
public
# registerCommand( string $command, string $help )

This registers a sub command

This registers a sub command

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

Parameters

$command
$help

Throws

splitbrain\phpcli\Exception
public
# registerOption( string $long, string $help, string|null $short = null, boolean|string $needsarg = false, string $command = '' )

Register an option for option parsing and help generation

Register an option for option parsing and help generation

Parameters

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

Throws

splitbrain\phpcli\Exception
public
# checkArguments( )

Checks the actual number of arguments against the required number

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

Throws

splitbrain\phpcli\Exception
public
# parseOptions( )

Parses the given arguments for known options and command

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

Throws

splitbrain\phpcli\Exception
public boolean|string|string[]
# getOpt( mixed $option = null, boolean|string $default = false )

Get the value of the given option

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

Parameters

$option
$default
what to return if the option was not set

Returns

boolean|string|string[]
public string
# getCmd( )

Return the found command if any

Return the found command if any

Returns

string
public array
# getArgs( )

Get all the arguments passed to the script

Get all the arguments passed to the script

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

Returns

array
public string
# help( )

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

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

Returns

string

Throws

splitbrain\phpcli\Exception
Properties summary
protected array $setup

keeps the list of options to parse

keeps the list of options to parse

#
protected array $options

store parsed options

store parsed options

# array()
protected string $command

current parsed command if any

current parsed command if any

# ''
protected array $args

passed non-option arguments

passed non-option arguments

# array()
protected string $bin

the executed script

the executed script

#
protected splitbrain\phpcli\Colors $colors

for colored help output

for colored help output

#
protected string $newline

newline used for spacing help texts

newline used for spacing help texts

# "\n"
API documentation generated by ApiGen