10

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

php script.php -f filename -t filetype

现在取决于可以是 u、c 或 s 的文件类型,我需要进行正确的操作。

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

这是我正在使用的代码:

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

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;
    }

请就此提出建议!!!

4

4 回答 4

19

如果你只是把这样的东西放在你的 PHP 脚本中(在我的机器上调用temp.php

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

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

php temp.php -f filename -t filetype

你会得到这样的输出:

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

意思就是 :

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


从那里开始,如果您希望switch依赖作为 的值传递的选项-t,您将使用如下内容:

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

基本上,你把:

  • 您要在switch()
  • case以及s中的可能值


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

于 2010-03-11T17:28:04.453 回答
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

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.

演示

请检查examples/demo.php

跑:

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

打印:

* 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 在 GitHub 上:https ://github.com/c9s/GetOptionKit

于 2011-11-29T06:02:08.583 回答
0

你的方法有问题。如果用户错误地给出以下命令行怎么办。脚本将无法检测到它并且会像$file['t']数组一样失败。

php temp.php -f filename -t filetype -f filename2
于 2010-08-19T21:18:57.180 回答
0
<?php
$file = getopt("f:t:");
var_dump($file);
$f = file($file['f']);
var_dump($f);
?>

#php file.php -f filename.txt 会读取输入文件

于 2021-05-25T04:34:12.043 回答