tags:

views:

392

answers:

2

Is anywhere written( tutorial or documentation ) how work properly with options in custom symfony task?

class mloMaintainceTask extends sfBaseTask
{
  protected function configure()
  {
    // ...
    $this->setOptions(array(
      new sfCommandOption('mloOption');
    ));
  }

  private function method3()
  {
    return $this->getOption('mloOption');
  }
}

Is there something like the getOption method from the example's method3

+1  A: 

You should pass real options' values to other methods explicitly.

private function method3($optionvalue)
{
  return $optionvalue;
}
protected function execute($arguments = array(), $options = array())
{
  /*blablabla*/
  echo $this->method3($options['mlooption']);
  /*blablabla*/
}
develop7
I met $options, but there are never the options which i gave to the task
Mailo
+3  A: 

Just in case you haven't already seen this: http://www.symfony-project.org/cookbook/1_1/en/tasks

That provides a good overview of custom task creation including options and arguments. I actually just sat down to write a couple tasks a few days ago so I'll try and go over what I've picked up so far. Here's an example from a task I created that updates a project from subversion for a quick rebuild:

$this->addOptions(array(
    new sfCommandOption('username', null, sfCommandOption::PARAMETER_REQUIRED, 'Subversion username'),
    ...
));

The first parameter is the name of the option and will play into the command execution by becoming:

./symfony my:task --username="foo"

The second parameter is defined as a shortcut by the class definition (%symfony_lib_dir%/command/sfCommandOption.class.php) but I haven't played with it yet, your guess is as good as mine.

The third parameter specifies if there's more to the argument then just it's calling. Your options are as follows:

PARAMETER_NONE
  ex: --username
PARAMETER_OPTIONAL
  ex: --username[=...]
PARAMETER_REQUIRED
  ex: --username=...

The fourth parameter is the description of the argument for help output.

There's an optional fifth parameter that can be specified for a default value as long as you're not PARAMETER_NONE (it'll throw an exception on execution if you are).

Once your options are set up you can access them from the second parameter passed to your task's execute function, they'll come through as an associative array. Continuing my example I would have the following:

protected function execute($arguments = array(), $options = array()) {
    $task = 'svn up';
    if($options['username']) {
        $task .= ' --username="' . $options['username'] . '"';
    }
    ....
}

What has always been most helpful for me is code examples and since Symfony is bundled with a great collection of existing tasks I highly recommend reviewing them (%symfony_lib_dir%/task/*) for inspiration and guidance. I was really helped by project/sfProjectDeploy.class.php specifically, it uses a wide variety of the sfTask functionality and was crucial to finding my way through my custom tasks.

Hope that helps.

Cryo
When i'm creating instance of form object i can pass options to the constructor and then access it in form class by $this->getOption('nameOfPassedVariable'). Tasks don't have something similar?
Mailo
Not as a function, no. Instead you can just use the associative array in your execute functions second parameter the same way ($options['option_name']). There really isn't a need for a function as the set of options and parameters available to you are restricted to what was set up in your configure method.
Cryo