To provide new chef commands, you need to override the getCommands method of
your plugin, and return command instances:
class MyPlugin(PieCrustPlugin):
name = 'myplugin'
def getCommands(self):
return [
MyNewCommand()]
To create a command class, inherit from the ChefCommand base class:
from piecrust.commands.base import ChefCommand
class MyNewCommand(ChefCommand):
def __init__(self):
super(MyNewCommand, self).__init__()
self.name = 'foobar'
self.description = "Does some foobar thing."
def setupParser(self, parser, app):
parser.add_argument('thing')
def run(self, ctx):
print("Doing %s" % ctx.args.thing)
- The
namewill be used for command line invocation, i.e. your new command will be invoked withchef foobar. - The
descriptionwill be used for help pages likechef --help. - The
setupParsermethod passes anargparse.ArgumentParserand aPieCrustapplication. You’re supposed to setup the syntax for your commend there. - The
runmethod is called when your command is executed. Thectxobject contains a couple useful things, among others:argsis the namespace obtained from runningparse_args. It has all the values of the arguments for your command.appis the instance of the currentPieCrustapplication.- For the other things, check-out
piecrust.commands.base.CommandContext.