Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2 Shell Arguments help / example

I can't seem to find a simple concrete example of how to receive an argument on a CakePHP 2 shell script.

    class TestShell extends AppShell {
        public function argumentTest(){
            $parser = parent::getOptionParser();
            $parser->addArgument('testArgument', array('short' => 't','help' => 'The test argument'));
            var_dump($this->params);
        }
    }

I then try and call it:

  • Console/cake TestShell argumentTest --t 45

  • Console/cake TestShell argumentTest -t 45

  • Console/cake TestShell argumentTest --testArgument 45

  • Console/cake TestShell argumentTest -testArgument 45

with all of the above I get this as a response:

Usage:
cake lot_web_service [-h] [-v] [-q]

What am I doing wrong?

like image 280
tomwoods Avatar asked Sep 05 '26 07:09

tomwoods


1 Answers

You should set up the arguments when the option parser is set up:

public function getOptionParser() {
    $parser = parent::getOptionParser();
    //configure parser
    return $parser;
}

This ensures that the options and arguments are set up prior to dispatching the call. Also, from what it looks like, you're wanting addOption() instead of addArgument().

like image 171
jeremyharris Avatar answered Sep 08 '26 06:09

jeremyharris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!