0

在 CakePHP shell 中,我需要使用不定数量的参数。例如:

cake MyShell mycommand inputFile inputFile2 inputFileN outputFile

就我而言,我确定应该至少有 2 个参数(inputFileoutputFile),所以第一个和最后一个,但我不知道中间是否还有其他参数。

所以,在我的getOptionParser()我有:

$parser->addSubcommand('mycommand', array(
    'help' => __('This is my example command'),
    'parser' => array(
        'arguments' => array(
            'inputFile' => array('help' => __('First input file'), 'required' => TRUE),
            'outputFile' => array('help' => __('Output file'), 'required' => TRUE)
        )
    )
));

现在,您可以想象,这仅在我使用两个参数且仅两个参数时才有效。
我知道我无法使用getOptionParser()和手动检查$this->args,但我想知道是否有更好的解决方案。

谢谢。

4

1 回答 1

2

但我想知道是否有更好的解决方案。

其他 shell 工具允许您以这种方式指定列表:

cake my --input "file1, file2, file3" --output "out1, out2, out3"

或者如果您愿意,也可以这样做,但我认为与上面相比,它不是很容易阅读和理解。

cake my "file1, file2, file3" "out1, out2, out3"

您必须用逗号explode() 字符串,然后比较两个数组的长度(如果它们匹配,则不显示错误)。

您应该在使用它们之前检查每个文件是否存在,并验证输出位置是否也是可写的。

于 2014-10-16T08:29:43.170 回答