duanchi19820419 2014-10-16 05:40
浏览 20

PHP为一组命令选项验证命令行argv数组

I am doing command line script with php and trying to develop validate the set of commands to see if they were there, but i am not going anyware with the following code, i dont know why it alwasy goes to the help().. even if i have the argv options like

sh#php geninvoices.php --type=generate --w --bill_day=7


$commands=array('--type','--bill_day','--save');
if(!in_array($commands, $argv)){
    print_r($argv);
    help();
} 

any help ?

  • 写回答

1条回答 默认 最新

  • douxiang3978 2014-10-16 06:01
    关注

    Just to show the idea

    $argv = array('generate.php', '--type=generate', '--write', '--bill_day=7');
    $args = array();
    foreach($argv as $i => $str)
    {
        if ($i == 0) continue;
        list($arg, $value) = explode('=', $str, 2);
        $args[ltrim($arg, '-')] = $value;
    }
    
    var_dump($args);
    

    And now you have arguments with values and you can do whatever you want.

    array(3) {
      ["type"]=>
      string(8) "generate"
      ["write"]=>
      NULL
      ["bill_day"]=>
      string(1) "7"
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)