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

Class: Console_GetoptPlus_Getopt

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

Class Overview


Parsing of a command line.


Author(s):

Version:

  • Release:

Copyright:

  • 2008 Michel Corne

Methods


Child classes:

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

Inherited Variables

Inherited Methods


Class Details

[line 99]
Parsing of a command line.

See more examples in docs/examples.

Code Example 1:

  1.  require_once 'Console/GetoptPlus.php';
  2.  
  3.  try {
  4.     $shortOptions "b:c::";
  5.     $longOptions = array("foo""bar=");
  6.     $options = Console_Getoptplus::getopt($config$shortOptions$longOptions);
  7.     // some processing here...
  8.     print_r($options);
  9.  }
  10.     $error = array($e->getCode()$e->getMessage());
  11.     print_r($error);
  12.  }

Code Example 2:

  1.  require_once 'Console/GetoptPlus/Getopt.php';
  2.  
  3.  try {
  4.     $shortOptions "b:c::";
  5.     $longOptions = array("foo""bar=");
  6.     $options Console_GetoptPlus_Getopt::getopt($config$shortOptions$longOptions);
  7.     // some processing here...
  8.     print_r($options);
  9.  }
  10.     $error = array($e->getCode()$e->getMessage());
  11.     print_r($error);
  12.  }

Run:

 #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

createShorcuts   [line 169]

array createShorcuts( array $longOptionsDef, string $ambiguity)

Creates the option shorcut names
  • Return: the option shorcuts and the ambigous options
  • Access: public

Parameters:

array   $longOptionsDef     the long option names
string   $ambiguity     directive to handle option names ambiguity

[ Top ]

doGetopt   [line 218]

array doGetopt( [numeric $version = null], [array $args = array()], [string $shortOptions = ''], [array $longOptions = array()], [string $ambiguity = ''])

Parses the command line

See getopt() for a complete description.

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

Parameters:

numeric   $version     the getopt version: 1 or 2
array   $args     the arguments
string   $shortOptions     the short options definition, e.g. "ab:c::"
array   $longOptions     the long options definition
string   $ambiguity     directive to handle option names ambiguity

[ Top ]

getopt   [line 283]

array getopt( [array $args = array()], [string $shortOptions = ''], [array $longOptions = array()], [string $ambiguity = ''])

Parses the command line

See the definition/example in the class Doc Block.

Example: returning an index 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   $args     the arguments
string   $shortOptions     the short options definition, e.g. "ab:c::"
  • ":" : the option requires an argument
  • "::" : the option accepts an optional argument
  • otherwise the option accepts no argument
array   $longOptions     the long options definition, e.g. array("art", "bar=", "car==)
  • "=" : the option requires an argument
  • "==" : the option accepts an optional argument
  • otherwise the option accepts no argument
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"

[ Top ]

getopt2   [line 302]

array getopt2( [array $args = array()], [string $shortOptions = ''], [array $longOptions = array()], [string $ambiguity = ''])

Parses the command line

See getopt() for a complete description.

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

Parameters:

array   $args     the arguments
string   $shortOptions     the short options definition, e.g. "ab:c::"
array   $longOptions     the long options definition
string   $ambiguity     directive to handle option names ambiguity

[ Top ]

isOption   [line 315]

boolean isOption( string $argument)

Checks if the argument is an option
  • Return: true if an option, false otherwise
  • Access: public

Parameters:

string   $argument     the argument, e.g. "-f" or "--foo"

[ Top ]

parseLongOption   [line 328]

void parseLongOption( string $argument)

Parses a long option
  • Access: public

Parameters:

string   $argument     the option and argument (excluding the "--" prefix), e.g. "file=foo.php", "file foo.php", "bar"

[ Top ]

parseLongOptionsDef   [line 361]

array parseLongOptionsDef( array $options)

Parses the long option names and types
  • Return: the options name and type, e.g. array("foo"=>"noarg", "bar"=>"mandatory")
  • Access: public

Overridden in child classes as:

Console_GetoptPlus::parseLongOptionsDef()
Parses the long option names and types

Parameters:

array   $options     the long options, e.g. array("foo", "bar=")

[ Top ]

parseShortOption   [line 394]

void parseShortOption( string $argument)

Parses a short option
  • Access: public

Parameters:

string   $argument     the option and argument (excluding the "-" prefix), e.g. "zfoo.php", "z foo.php", "z".

[ Top ]

parseShortOptionsDef   [line 439]

array parseShortOptionsDef( string $options)

Parses the short option names and types
  • Return: the options name and type, e.g. array("a"=>"noarg", "b"=>"mandatory", "c"=>"optional")
  • Access: public

Overridden in child classes as:

Console_GetoptPlus::parseShortOptionsDef()
Parses the short option names and types

Parameters:

string   $options     the short options, e.g. array("ab:c::)

[ Top ]

process   [line 478]

array process( [array $args = array()], string $shortOptions, array $longOptions, [string $ambiguity = ''], [numeric $version = 2])

Parses the command line

See getopt() for a complete description.

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

Overridden in child classes as:

Console_GetoptPlus::process()
Parses the command line

Parameters:

array   $args     the arguments
string   $shortOptions     the short options definition, e.g. "ab:c::"
array   $longOptions     the long options definition, e.g. array("foo", "bar=")
string   $ambiguity     directive to handle option names ambiguity
numeric   $version     the getopt version: 1 or 2

[ Top ]

readPHPArgv   [line 544]

array readPHPArgv( )

Reads the command arguments
  • Return: the arguments
  • Access: public

[ Top ]

verifyNoAmbiguity   [line 565]

boolean verifyNoAmbiguity( array $longOptionsDef, string $ambiguity)

Verifies there is no ambiguity with option names
  • Return: no ambiguity if true, false otherwise
  • Access: public

Parameters:

array   $longOptionsDef     the long options names and their types
string   $ambiguity     directive to handle option names ambiguity, See getopt() for a complete description

[ Top ]


Documentation generated on Sat, 26 Jan 2008 14:30:05 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.