Console_GetoptPlus
[ class tree: Console_GetoptPlus ] [ index: Console_GetoptPlus ] [ all elements ]

Class: Console_GetoptPlus

Source Location: /Console_GetoptPlus-1.0.0RC1/GetoptPlus.php

Class Overview

Console_GetoptPlus_Getopt
   |
   --Console_GetoptPlus

Parsing of a command line based on the command description in an array.


Author(s):

Version:

  • Release:

Copyright:

  • 2008 Michel Corne

Methods


Inherited Variables

Inherited Methods

Class: Console_GetoptPlus_Getopt

Console_GetoptPlus_Getopt::createShorcuts()
Creates the option shorcut names
Console_GetoptPlus_Getopt::doGetopt()
Parses the command line
Console_GetoptPlus_Getopt::getopt()
Parses the command line
Console_GetoptPlus_Getopt::getopt2()
Parses the command line
Console_GetoptPlus_Getopt::isOption()
Checks if the argument is an option
Console_GetoptPlus_Getopt::parseLongOption()
Parses a long option
Console_GetoptPlus_Getopt::parseLongOptionsDef()
Parses the long option names and types
Console_GetoptPlus_Getopt::parseShortOption()
Parses a short option
Console_GetoptPlus_Getopt::parseShortOptionsDef()
Parses the short option names and types
Console_GetoptPlus_Getopt::process()
Parses the command line
Console_GetoptPlus_Getopt::readPHPArgv()
Reads the command arguments
Console_GetoptPlus_Getopt::verifyNoAmbiguity()
Verifies there is no ambiguity with option names

Class Details

[line 125]
Parsing of a command line based on the command description in an array.

See more examples in docs/examples.

Code Example:

  1.  require_once 'Console/GetoptPlus.php';
  2.  
  3.  try {
  4.     $config = array(
  5.       'header' => array('The command xyz is used to...',
  6.         'Note that the header and the usage are optional.'),
  7.       'usage' => array('--foo''--bar <arg> -c [arg]'),
  8.       'options' => array(
  9.         array('long' => 'foo''type' => 'noarg''desc' => array(
  10.           'An option without argument with only the long',
  11.           'name defined.')),
  12.         array('long' => 'bar''type' => 'mandatory''short' => 'b',
  13.           'desc' => array('arg',
  14.             'A mandatory option with both the long and',
  15.             'the short names defined.')),
  16.         array('short' => 'c''type' => 'optional',
  17.           'desc' => array('arg',
  18.             'An option with an optional argument with only',
  19.             'the short name defined.'))),
  20.         'parameters' => array('[param1] [param2]',
  21.           'Some additional parameters.'),
  22.         'footer' => array('Some additional information.',
  23.           'Note that the footer is optional.'),
  24.     );
  25.  
  26.  $options = Console_Getoptplus::getoptplus($config);
  27.  // some processing here...
  28.  print_r($options);
  29.  }
  30.     $error = array($e->getCode()$e->getMessage());
  31.     print_r($error);
  32.  }

Run:

 #xyz --help
 #xyz -h
 #xyz --foo -b car -c
 #xyz --foo -b car -c param1
 #xyz --foo -b car -cbus param1
 #xyz --foo -b car -c=bus param1
 #xyz --bar car param1 param2
 #xyz --bar car -- param1 param2
 #xyz --bar=car param1 param2



[ Top ]


Method Detail

checkOptionsConfig   [line 154]

array checkOptionsConfig( array $config)

Verifies the option settings are valid

Adds the "help" option if missing.

  • Return: the options configurations, the updated 'options' subarray, e.g. $config['options']
  • Access: public

Parameters:

array   $config   —  the command configuration, see the configuration definition/example in the class Doc Block

[ Top ]

createOptionsDef   [line 219]

array createOptionsDef( array $optionsConfig, string $defType, string $pattern)

Extracts the long or short option names and types

Validates the option name against the pattern. A short option name has a single letter. A long option name has one of more alphanumerical letters.

  • Return: the option names list, e.g. array("a" => "noarg", ...)
  • Access: public

Parameters:

array   $optionsConfig   —  the options configurations, see the configuration definition/example in the class Doc Block, e.g. $config['options']
string   $defType   —  the option name type: "short" or "long"
string   $pattern   —  the validation pattern

[ Top ]

getoptplus   [line 310]

array getoptplus( [array $config = array()], [string $convertName = ''], [boolean $returnAssoc = false], [string $ambiguity = ''], [boolean $exitHelp = true])

Parses the command line

See the configuration definition/example in the class Doc Block.

Example 1: returning an index array

  1.  array(
  2.     [0=> array(
  3.       [0=> array([0=> "--foo"[1=> null),
  4.       [1=> array([0=> "b"[1=> "car"),
  5.       [2=> array([0=> "c"[1=> null)),
  6.     [1=> array([0=> "param1"[1=> "param2")
  7.  );

Example 2: returning an associative array

  1.  array(
  2.     [0=> array("foo" => null"bar" => "car""c" => null),
  3.     [1=> array([0=> "param1"[1=> "param2")
  4.  );

  • Return: the parsed options, their arguments and parameters
  • Access: public

Parameters:

array   $config   —  the command configuration, see the configuration definition/example in the class Doc Block
string   $convertName   —  returns short option names if set to "long2short", long ones if set to "short2long", as in the command line by default
boolean   $returnAssoc   —  returns an associative array if true, an index array if false
string   $ambiguity   —  directive to handle option names ambiguity, e.g. "--foo" and "--foobar":
  • "loose": allowed if "--foo" does not accept an argument, this is the default behaviour
  • "strict": no ambiguity allowed
  • "shortcuts": implies "strict", the use of partial option names is allowed, e.g. "--f" or "--fo" instead of "--foo"
boolean   $exitHelp   —  if "--help" is one of the options:
  • true: displays the command usage and exits
  • false: returns the command usage as:
    • an index array, e.g. array([0] => array([0] => array("h", "Usage:...")))
    • an associative, e.g. array([0] => array("h" => "Usage:..."))

[ Top ]

parseLongOptionsDef   [line 330]

array parseLongOptionsDef( array $optionsConfig)

Parses the long option names and types

Verifies the option names have one of more alphanumerical characters.

  • Return: the option names list, e.g. array("foo" => "noarg", ...)
  • Access: public

Overrides Console_GetoptPlus_Getopt::parseLongOptionsDef() (Parses the long option names and types)

Parameters:

array   $optionsConfig   —  the options configurations, see the configuration definition/example in the class Doc Block, e.g. $config['options']

[ Top ]

parseShortOptionsDef   [line 346]

array parseShortOptionsDef( array $optionsConfig)

Parses the short option names and types

Verifies the option names have one alphanumerical character.

  • Return: the option names list, e.g. array("f" => "noarg", ...)
  • Access: public

Overrides Console_GetoptPlus_Getopt::parseShortOptionsDef() (Parses the short option names and types)

Parameters:

array   $optionsConfig   —  the options configurations, see the configuration definition/example in the class Doc Block, e.g. $config['options']

[ Top ]

process   [line 368]

array process( [array $config = array()], [string $convertName = ''], [boolean $returnAssoc = false], [string $ambiguity = ''], [boolean $exitHelp = true])

Parses the command line

See getoptplus() for a complete description.

  • Return: the parsed options, their arguments and parameters
  • Access: public

Overrides Console_GetoptPlus_Getopt::process() (Parses the command line)

Parameters:

array   $config   —  the command configuration
string   $convertName   —  returns short option names if set to "long2short", long ones if set to "short2long", as in the command line by default
boolean   $returnAssoc   —  returns an associative array if true, an index array if false
string   $ambiguity   —  directive to handle option names ambiguity: "loose", "strict", or "shortcuts"
boolean   $exitHelp   —  same as getoptplus()

[ Top ]

tidyOptions   [line 412]

array tidyOptions( array $options, [string $convertName = ''], [boolean $returnAssoc = false])

Tidies the command arguments

See getoptplus() for a complete description of the returned options.

  • Return: the tidied options arguments
  • Access: public

Parameters:

array   $options   —  the parsed options arguments, e.g.
string   $convertName   —  returns short option names if set to "long2short", long ones if set to "short2long", as in the command line by default
boolean   $returnAssoc   —  returns an associative array if true, an index array if false

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:14:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.