PHP getopt operations

This question is about getopt function in php. I need to pass two parameters to php scripts like

PHP .php -f filename -t filetype 

Now depending on the type of file, which may be u, c or s, I need to do the right job.

I use a switch case for this:

Here is the code I'm using:

 // Get filename of input file when executing through command line. $file = getopt("f:t:"); 

The case with the switch should compare the type of file that I transmit from the command line (u, c or i), and, accordingly, match it and perform the operation.

 switch("I am not sure what should go in there and this is wrong,Please advice") { case `Not sure`: $p->ini(); break; case `Not sure`: $p->iniCon(); break; case `Not sure`: $p->iniImp(); break; } 

Please report this !!!

+7
file php getopt
source share
3 answers

If you just put something like this in your PHP script (called temp.php on my machine):

 <?php $file = getopt("f:t:"); var_dump($file); 

And call it from the command line by passing these two parameters:

 php temp.php -f filename -t filetype 

You will get the following output:

 array(2) { ["f"]=> string(8) "filename" ["t"]=> string(8) "filetype" } 

It means that:

  • $file['f'] contains the value passed to -f
  • and $file['t'] contains the value passed to -t


From now on, if you want your switch depend on the parameter passed as the -t value, you will use something like this:

 switch ($file['t']) { case 'u': // ... break; case 'c': // ... break; case 'i': // ... break; } 

Basically, you put:

  • The variable you want to test in switch()
  • And the possible values ​​in case s


And, for more information, you can read the PHP manual:

+13
source share

I used the getopt parser for this, supported short, long option names, type restrictions, print options, etc., and this library has been supported for over 6 years.

Description:

 <?php use GetOptionKit\OptionCollection; use GetOptionKit\OptionParser; $options = new OptionCollection; $spec = $options->add( 'f|foo:' , 'option require value' ); # returns spec object. $options->add( 'b|bar+' , 'option with multiple value' ); $options->add( 'z|zoo?' , 'option with optional value' ); $options->add( 'f|foo:=i' , 'option require value, with integer type' ); $options->add( 'f|foo:=s' , 'option require value, with string type' ); $options->add( 'v|verbose' , 'verbose flag' ); $options->add( 'd|debug' , 'debug flag' ); $parser = new OptionParser; $result = $parser->parse( array( 'program' , '-f' , 'foo value' , '-v' , '-d' ) ); $result = $parser->parse( $argv ); $spec = $result->verbose; $spec = $result->debug; $spec->value; # get value 

GetOptionKit \ OptionPrinter can print options for you:

 * Available options: -f, --foo option requires a value. -b, --bar option with multiple value. -z, --zoo option with optional value. -v, --verbose verbose message. -d, --debug debug message. --long long option name only. -s short option name only. 

Demo

Please check examples/demo.php .

Run:

 % php examples/demo.php -f test -b 123 -b 333 

Print

 * Available options: -f, --foo <value> option requires a value. -b, --bar <value>+ option with multiple value. -z, --zoo [<value>] option with optional value. -v, --verbose verbose message. -d, --debug debug message. --long long option name only. -s short option name only. Enabled options: * key:foo spec:-f, --foo <value> desc:option requires a value. value => test * key:bar spec:-b, --bar <value>+ desc:option with multiple value. Array ( [0] => 123 [1] => 333 ) 

GetOptionKit is on GitHub: https://github.com/c9s/GetOptionKit

+5
source share

There is a problem in your approach. What to do if the user by mistake skips the next command line. the script will not be able to detect it and will fail because $file['t'] will be an array.

 php temp.php -f filename -t filetype -f filename2 
0
source share

All Articles