InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Parsing Program Arguments with getopt in PHP
PHP

Parsing Program Arguments with getopt in PHP

InfinityCoder December 27, 2016

You want to parse program options that may be specified as short or long options, or they may be grouped.

Use the built-in getopt() function. As of PHP 5.3.0, it supports long options, optional values, and other convenient features:

1
2
3
4
5
// accepts -a, -b, and -c
$opts1 = getopt('abc');
 
// accepts --alice and --bob
$opts2 = getopt('', array('alice','bob'));

To parse short-style options, pass getopt() the array of command-line arguments and a string specifying valid options.

This example allows -a, -b, or -c as arguments, alone or in groups:

1
$opts = getopt('abc');

For the previous option string abc, these are valid sets of options to pass:

1
2
3
4
5
% program.php -a -b -c
 
% program.php -abc
 
% program.php -ab -c

The getopt() method returns an array. For each option specified on the command line, there is one element in the array.

The key of the array element is the option name and the value of the array element is the option value.

Counterintuitively, for flag-style options that don’t take a value (such as a, b and c in the preceding), the value in the
corresponding array element is false.

For example, if the preceding program is run as:

1
% program.php -a -b sneeze

This makes $opts:

1
2
3
4
5
6
array(2) {
   ["a"]=>
   bool(false)
   ["b"]=>
   bool(false)
}

Put a colon after an option in the specification string to indicate that it requires a value.
Two colons means the value is optional. So ab:c:: means that a can’t have a value, b must, and c can take a value if specified.

With this specification string, running the program as:

1
% program.php -a -b sneeze

This makes $opts:

1
2
3
4
5
6
array(2) {
   ["a"]=>
   bool(false)
   ["b"]=>
   string(6) "sneeze"
}

Instead of being ignored as an unspecified option, sneeze is now set as the value of b.
To parse long-style arguments, supply a second argument to getopt() containing an array that describes your desired arguments.

Put each argument in an array element (leave off the leading –) and follow it with : to indicate a mandatory argument or :: to indicate an optional argument.

The first argument to getopt() (the string for shortstyle arguments) can be left blank or not, depending on whether you also want to parse short-style arguments.

This example allows debug as an argument with no value, name with a mandatory value, and size with an optional value:

1
$opts = getopt('',array('debug','name:','size::'));

These are valid ways to run this program:

1
2
3
4
5
6
7
8
9
10
11
% program.php --debug
 
% program.php --name=Susannah
 
% program.php --name Susannah
 
% program.php --debug --size
 
% program.php --size=56 --name=Susannah
 
% program.php --name --debug

The last example is valid (if counterproductive) because it treats –debug as the value of the name argument and doesn’t consider the debug argument to be set.

Values can be separated from their arguments on the command line by either an = or a space.
Note that for long-style arguments, getopt() does not include the leading — in the array of parsed arguments.

An argument specified as –name on the command line results in a key of name in the parsed argument array.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Reading Standard Error from a Program in PHP
You want to read the error output from a program. …

Reading Standard Error from a Program in PHP

Dealing with Lost Passwords in PHP
You want to issue a password to a user who …

Dealing with Lost Passwords in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap