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