0

In a CakePHP shell, I need to use an indefinite number of arguments. For example:

cake MyShell mycommand inputFile inputFile2 inputFileN outputFile

In my case, I know for sure that should be at least 2 arguments (inputFile and outputFile), so the first and the last, but I don't know if there are others in between.

So, in my getOptionParser() I have:

$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)
        )
    )
));

Now, as you can imagine, this only works if I use two arguments and only two.
I know that I could not use getOptionParser() and manually check for $this->args, but I would like to know if there is a better solution.

Thanks.

Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36

1 Answers1

2

but I would like to know if there is a better solution.

Other shell tools allow you to specify lists this way:

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

Or do this way if you prefer but I think it's not very easy to read and understand compared to above.

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

You'll have to explode() the strings by comma, then compare the lenght of the two arrays if they match and if not show an error.

You should check if each file exists before doing something with them and verify that the output location is writeable as well.

floriank
  • 25,546
  • 9
  • 42
  • 66