Making a shell

So you have some code you want to run on the command line, so we’ll build a shell for that. However I’m always looking for template code which includes shell sub-commands with arguments. I don’t find the Books Shell documentation very good for this, as I’m always looking for a template I can customise.

Easy to create the basic shell using Bake.

$ bin/cake bake shell Example

Help output

The actual help output uses an optionParser to output what the shell does. The easiest way to configure this is using the fluent interface.

<?php
/**
 * Manage the available sub-commands along with their arguments and help
 *
 * @return \Cake\Console\ConsoleOptionParser
 */
public function getOptionParser()
{
    $parser = parent::getOptionParser();

    $parser->addSubcommand('new', [
        'help' => __('Create a new partner')
    ]);

    $parser->addSubcommand('foo', ['help' => 'A foo command for fooing'])
        ->addArgument('b', [
            'help' => 'For creating bees',
            'required' => true,
            'choices' => ['biological', 'mechanical']
        ])
        ->addArgument('c', ['help' => 'Creates the sea'])
        ->addOption('wet', ['short' => 'w', 'help' => 'A wet run']);

    return $parser;
}

Make a brew

That’s it, make a brew.