PHP Classes

PHP Command Line Arguments Parser: Parse and extract arguments from the command line

Recommend this page to a friend!
  Info   View files Example   View files View files (61)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (11 months ago) RSS 2.0 feedStarStarStarStar 75%Total: 122 All time: 9,423 This week: 48Up
Version License PHP version Categories
clparser 1.0.2GNU Free Document...5.5PHP 5, Console, Parsers
Description 

Author

This class can parse and extract arguments from the command line.

It takes a XML string with the definition of the supported syntax of parameters to be parsed from the command line.

The class parses the command line arguments array and returns the values of the respective parameters.

The values of the parsed command line arguments can be accessed as class variables.

It supports parameters of many types like: strings, filenames, directory names, flags, numeric value (with the ability to specify expressions to be computed at run-time), arrays, lists, sets and bitsets, email addresses, IP addresses, masks, ranges, domain names, keywords, etc..

The class can also show the usage help output message to tell the user what is the expected arguments syntax.

Innovation Award
PHP Programming Innovation award nominee
December 2016
Number 6
There are many solutions to parse and extract the arguments passed to a PHP script from the command line but most do not do much more than extracting the text values for each parameter switch.

This class provides a more advanced solution that can read the definitions of what is expected for each parameters from a XML files, so you can separate the parameter processing code from its definition.

It also supports many types of parameters so it can perform the necessary validation of each parameter with less application code.

The class can also perform calculations of numeric values passed as math expressions that the class can evaluate before returning the value to the application.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 57
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

#!/usr/bin/php
<?php
/**************************************************************************************************************

    The following example accepts the following arguments and displays their values :
    - A flag named 'boolean_flag', which also has the 'bf' alias
    - An optional string parameter, named 'string_parameter' (or 'sp') with a default value of
      "this is the default value".
    - An unlimiter number of strings or filenames.

    Since no "name=" attribute is specified on the <command> tag of the parameter definitions, it will be
    set to the name of the current script, without its extension.

    Note also that the usage text contains some PHP code, which displays the current date and time.

    You can invoke the script using the reserved parameters -help, -usage or -topics ; but you can also
    specify the special parameters beginning with "--".

 **************************************************************************************************************/

require_once ( dirname ( __FILE__ ) . '/../examples.inc.php' ) ;

$definitions = <<<END
<command allow-files="true" min-files="0">

        <usage>
            This is an example script which allows an unlimited number of filenames to be specified,
            a flag named -boolean_flag, and a string parameter named -string_parameter.
            The current date is : <?= date ( 'Y-m-d H:i:s' ) ; ?>.
        </usage>

        <flag name="boolean_flag, bf">
            A boolean flag.
        </flag>

        <double name="double_value, dv">
            A double (float) value.
        </double>

        <string name="string_parameter, sp" default="This is the default value">
            A string parameter.
        </string>
           
    </command>
END;

// Instantiate a command-line parser object
$CL = new CLParser ( $definitions ) ;

// Get the specified parameters
$bf = $CL -> boolean_flag ; // $CL -> bf will also work
$dv = $CL -> double_value ;
$sp = $CL -> string_parameter ;
$files = $CL -> Files ;

output ( "Value of -boolean_flag : $bf" ) ;
output ( "Value of -string_parameter : $sp" ) ;
output ( "Value of -double_parameter : $dv" ) ;
output ( "Files specified on the command line : " . implode ( ', ', $files ) ) ;


  Files folder image Files  
File Role Description
Files folder imageCL (5 files, 3 directories)
Files folder imageExamples (2 files, 3 directories)
Files folder imageHelp (1 file)
Accessible without login Plain text file CL.php Aux. Auxiliary script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  CL  
File Role Description
Files folder imageData (1 file)
Files folder imageTypes (18 files)
Files folder imageUtilities (15 files)
  Plain text file CL.phpclass Class Class source
  Plain text file CLAbstractParameter.phpclass Class Class source
  Plain text file CLExceptions.phpclass Class Class source
  Plain text file CLParser.phpclass Class Class source
  Plain text file CLValidators.phpclass Class Class source

  Files folder image Files  /  CL  /  Data  
File Role Description
  Accessible without login Plain text file ColorNames.txt Data Auxiliary data

  Files folder image Files  /  CL  /  Types  
File Role Description
  Plain text file Boolean.phpclass Class Class source
  Plain text file Color.phpclass Class Class source
  Plain text file CommandLine.phpclass Class Class source
  Plain text file DateTime.phpclass Class Class source
  Plain text file File.phpclass Class Class source
  Plain text file Flag.phpclass Class Class source
  Plain text file Internet.phpclass Class Class source
  Plain text file Keyword.phpclass Class Class source
  Plain text file List.phpclass Class Class source
  Plain text file Measure.phpclass Class Class source
  Accessible without login Plain text file Numeric.ini Data Auxiliary data
  Plain text file Numeric.phpclass Class Class source
  Plain text file Range.phpclass Class Class source
  Plain text file Set.phpclass Class Class source
  Plain text file SortOptions.phpclass Class Class source
  Plain text file Standard.phpclass Class Class source
  Plain text file String.phpclass Class Class source
  Accessible without login Plain text file Types.ini Data Auxiliary data

  Files folder image Files  /  CL  /  Utilities  
File Role Description
  Plain text file AsciiReport.phpclass Class Class source
  Plain text file Colors.phpclass Class Class source
  Plain text file Console.phpclass Class Class source
  Plain text file Convert.phpclass Class Class source
  Plain text file Debug.phpclass Class Class source
  Plain text file Encoder.phpclass Class Class source
  Plain text file Formatting.phpclass Class Class source
  Plain text file GenericWrapper.phpclass Class Class source
  Plain text file IniFile.phpclass Class Class source
  Plain text file Path.phpclass Class Class source
  Plain text file PHP.phpclass Class Class source
  Plain text file Regex.phpclass Class Class source
  Plain text file SimpleXML.phpclass Class Class source
  Plain text file String.phpclass Class Class source
  Plain text file Vector.phpclass Class Class source

  Files folder image Files  /  Examples  
File Role Description
Files folder image00.readme (1 file)
Files folder image01.general (1 file)
Files folder imagecommon (13 files)
  Accessible without login Plain text file examples.inc.php Aux. Auxiliary script
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  Examples  /  00.readme  
File Role Description
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  Examples  /  01.general  
File Role Description
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  Examples  /  common  
File Role Description
  Accessible without login Plain text file aliases.bad.php Example Example script
  Accessible without login Plain text file aliases.good.php Example Example script
  Accessible without login Plain text file arguments.php Example Example script
  Accessible without login Plain text file default.php Example Example script
  Accessible without login Plain text file help-group.php Example Example script
  Accessible without login Plain text file help-text.php Example Example script
  Accessible without login Plain text file hidden.php Example Example script
  Accessible without login Plain text file multiple.php Example Example script
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file required.php Example Example script
  Accessible without login Plain text file usage.php Example Example script
  Accessible without login Plain text file validation-regex.php Example Example script
  Accessible without login Plain text file value-text.php Example Example script

  Files folder image Files  /  Help  
File Role Description
  Accessible without login HTML file Help.html Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:122
This week:0
All time:9,423
This week:48Up
 User Ratings  
 
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:93%StarStarStarStarStar
Tests:-
Videos:-
Overall:75%StarStarStarStar
Rank:139