PHP getopt 操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2427176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:26:04  来源:igfitidea点击:

PHP getopt Operations

phpfilegetopt

提问by Rachel

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

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

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.

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

I am using switch case for the same:

我正在使用 switch 案例:

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

Kindly advise on this !!!

请就此提出建议!!!

回答by Pascal MARTIN

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

如果你只是在你的 PHP 脚本中加入这样的东西(在我的机器上调用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
  • $file['f']包含传递给的值 -f
  • $file['t']包含传递给的值-t


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


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

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
  • 您要测试的变量 switch()
  • 以及cases 中的可能值


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


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

回答by c9s

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.

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

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:

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.

Demo

演示

Please check examples/demo.php.

请检查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

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

回答by Mohit

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.

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

php temp.php -f filename -t filetype -f filename2