PHP getopt Operations

2019-02-16 20:59发布

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

标签: php file getopt
3条回答
混吃等死
2楼-- · 2019-02-16 21:32

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 :

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-16 21:44

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

php temp.php -f filename -t filetype -f filename2
查看更多
爷、活的狠高调
4楼-- · 2019-02-16 21:45

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

查看更多
登录 后发表回答