PHP getopt操作

Rac*_*hel 10 php file getopt

这个问题是关于php中的getopt函数.我需要将两个参数传递给php脚本,如

php script.php -f filename -t filetype
Run Code Online (Sandbox Code Playgroud)

现在根据文件类型可以是u,c或s我需要做正确的操作.

我正在使用相同的开关盒:

这是我正在使用的代码:

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

Switch case应该比较我从命令行(u,c或i)传入的文件的类型,并相应地匹配它并进行操作.

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;
    }
Run Code Online (Sandbox Code Playgroud)

请建议!!!

Pas*_*TIN 18

如果你只是把这样的事情在你的PHP脚本(叫,我的机器上,temp.php):

<?php
$file = getopt("f:t:");
var_dump($file);
Run Code Online (Sandbox Code Playgroud)

并从命令行调用它,传递这两个参数:

php temp.php -f filename -t filetype
Run Code Online (Sandbox Code Playgroud)

你会得到这种输出:

array(2) {
  ["f"]=>
  string(8) "filename"
  ["t"]=>
  string(8) "filetype"
}
Run Code Online (Sandbox Code Playgroud)

意思就是 :

  • $file['f'] 包含传递的值 -f
  • $file['t']包含传递的值-t


从那里开始,如果你希望你switch依赖于作为值传递的选项-t,你将使用这样的东西:

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

基本上,你把:

  • 要在其中测试的变量 switch()
  • 以及cases中的可能值


而且,有关更多信息,您可以在PHP手册中阅读:


c9s*_*c9s 8

我为此实现了一个getopt解析器,支持短,长选项名称,类型约束,选项打印等等.这个库已经维护了6年多.

概要:

<?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
Run Code Online (Sandbox Code Playgroud)

GetOptionKit\OptionPrinter可以为您打印选项:

* 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.
Run Code Online (Sandbox Code Playgroud)

演示

请检查examples/demo.php.

跑:

% php examples/demo.php -f test -b 123 -b 333
Run Code Online (Sandbox Code Playgroud)

打印:

* 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
    )
Run Code Online (Sandbox Code Playgroud)

GetOptionKit在GitHub上:https://github.com/c9s/GetOptionKit