Mercurial > piecrust2
comparison docs/api/02_components/01_commands.md @ 504:20fcadaaf871
docs: Add some API documentation.
At the moment, only the main page for plugin components, and a page for user
commands. Add skeleton pages for the rest.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sat, 25 Jul 2015 00:47:27 -0700 |
| parents | |
| children | dce482f7c62d |
comparison
equal
deleted
inserted
replaced
| 503:61d53d2163d6 | 504:20fcadaaf871 |
|---|---|
| 1 --- | |
| 2 title: Chef Commands | |
| 3 needs_pygments: true | |
| 4 --- | |
| 5 | |
| 6 To provide new `chef` commands, you need to override the `getCommands` method of | |
| 7 your plugin, and return command instances: | |
| 8 | |
| 9 | |
| 10 {% highlight 'python' %} | |
| 11 class MyPlugin(PieCrustPlugin): | |
| 12 name = 'myplugin' | |
| 13 | |
| 14 def getCommands(self): | |
| 15 return [ | |
| 16 MyNewCommand()] | |
| 17 {% endhighlight %} | |
| 18 | |
| 19 | |
| 20 To create a command class, inherit from the `ChefCommand` base class: | |
| 21 | |
| 22 {% highlight 'python' %} | |
| 23 from piecrust.commands.base import ChefCommand | |
| 24 | |
| 25 | |
| 26 class MyNewCommand(ChefCommand): | |
| 27 def __init__(self): | |
| 28 super(MyNewCommand, self).__init__() | |
| 29 self.name = 'foobar' | |
| 30 self.description = "Does some foobar thing." | |
| 31 | |
| 32 def setupParser(self, parser, app): | |
| 33 parser.add_argument('thing') | |
| 34 | |
| 35 def run(self, ctx): | |
| 36 print("Doing %s" % ctx.args.thing) | |
| 37 {% endhighlight %} | |
| 38 | |
| 39 | |
| 40 * The `name` will be used for command line invocation, _i.e._ your new command | |
| 41 will be invoked with `chef foobar`. | |
| 42 * The `description` will be used for help pages like `chef --help`. | |
| 43 * The `setupParser` method passes an `argparse.ArgumentParser` and a `PieCrust` | |
| 44 application. You're supposed to setup the syntax for your commend there. | |
| 45 * The `run` method is called when your command is executed. The `ctx` object | |
| 46 contains a couple useful things, among others: | |
| 47 * `args` is the namespace obtained from running `parse_args`. It has all the | |
| 48 values of the arguments for your command. | |
| 49 * `app` is the instance of the current `PieCrust` application. | |
| 50 * For the other things, check-out `piecrust.commands.base.CommandContext`. | |
| 51 |
