Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP getopt Operations

Tags:

file

php

getopt

This question is regarding getopt function in php. I need to pass two parameter to the php scripts like

php script.php -f filename -t filetype

Now depending upon the file type which can be u, c or s I need to do proper operation.

I am using switch case for the same:

Here is the Code I am using:

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

Switch case should compare the type of the file which I am passing in from the command line (u, c or i) and accordingly match it and do 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;
    }

Kindly advise on this !!!

like image 565
Rachel Avatar asked Mar 11 '10 17:03

Rachel


2 Answers

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

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

And call it from the command-line, passing those two parameters :

php temp.php -f filename -t filetype

You'll get this kind of output :

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

Which means that :

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


Starting from there, if you want your switch to depend on the option passed as the value of -t, you'll use something like this :

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

Basically, you put :

  • The variable that you want to test in the switch()
  • And the possible values in the cases


And, for more informations, you can read, in the PHP manual :

  • The manual page of switch
  • And getopt()
like image 71
Pascal MARTIN Avatar answered Sep 21 '22 00:09

Pascal MARTIN


I implemented a getopt parser for this, support short,long option names, type constraint, option printing.. etc. and this library has been maintained over 6 years.

Synopsis:

<?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

like image 24
c9s Avatar answered Sep 18 '22 00:09

c9s