duanji1056 2017-06-23 10:41
浏览 17
已采纳

如何访问CakePHP Shell参数?

I am working on a cake shell script. When I use some named arguments eg:

--username=world

how can I get the "username" param / value?

My code looks like this:

class InviteShell extends AppShell
{
//... here are my methods.

public function getOptionParser()
    {
        $parser = parent::getOptionParser();
        $parser->addArgument('username', array(
            'help' => 'Send E-Mail to which user?'
        ))->addOption('method', array(
            'short' => 'm',
            'help' => __('The specific method you want help on.')
        ))->description(__('Lookup doc block comments for classes in CakePHP'));
        return $parser;
    }

}

And what is the difference between argument and option? And also how can I read these options in my code?

The one thing that works is I can read the $this->args array, but this is not named. All I can do is get the arg by index, eg.: $this->args[0]

I am using Cake 2.9

  • 写回答

1条回答 默认 最新

  • duanmei1350 2017-06-23 14:20
    关注

    Arguments are positional values, options are prefixed values:

    shell_method argument1 argument2 --optionA=value --optionB=value
    

    So in your case username is a positional argument that will be looked up at position 0, and method is a prefix option that can occour anywhere.

    shell_method userA --method=methodX
    shell_method --method=methodX userA
    

    In both cases the userA value will be available in $this->args[0], and the methodX value will be available in $this->params['method'] or via $this->param('method').

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算