|
| 1 | +#!/usr/bin/env python |
| 2 | +import nicfit |
| 3 | + |
| 4 | +@nicfit.Command.register |
| 5 | +class Hi(nicfit.Command): |
| 6 | + NAME = "hi" |
| 7 | + ALIAS = ["hello"] |
| 8 | + HELP = "A simple hello command." |
| 9 | + DESC = "This command can speak Spanish a command line option." |
| 10 | + |
| 11 | + def _initArgParser(self, parser): |
| 12 | + parser.add_argument("--spanish", action="store_true", |
| 13 | + help="Respond in Spanish") |
| 14 | + |
| 15 | + def _run(self): |
| 16 | + print("Hola" if self.args.spanish else "Hi") |
| 17 | + |
| 18 | +@nicfit.Command.register |
| 19 | +class bye(nicfit.Command): |
| 20 | + HELP = "A simple hello command." |
| 21 | + def _run(self): |
| 22 | + print("Bye") |
| 23 | + |
| 24 | +# Until Python 3.7 ArgumentParser.add_subparsers does not support the `required` |
| 25 | +# keyword, nicfit.ArgumentParser handles this. |
| 26 | +parser = nicfit.ArgumentParser() |
| 27 | +nicfit.Command.loadCommandMap(parser.add_subparsers(dest="cmd", required=True)) |
| 28 | +args = parser.parse_args() |
| 29 | +args.command_func(args) |
| 30 | + |
| 31 | +""" |
| 32 | +$ ./cmds2.py |
| 33 | +usage: cmds2.py [-h] {bye,hi} ... |
| 34 | +cmds2.py: error: the following arguments are required: cmd |
| 35 | +$ ./cmds2.py --help |
| 36 | +usage: cmds2.py [-h] {hi,bye} ... |
| 37 | +
|
| 38 | +positional arguments: |
| 39 | + {hi,bye} |
| 40 | + hi A simple hello command. |
| 41 | + bye A simple hello command. |
| 42 | +
|
| 43 | +optional arguments: |
| 44 | + -h, --help show this help message and exit |
| 45 | +$ ./cmds2.py hi --help |
| 46 | +usage: cmds2.py hi [-h] [--spanish] |
| 47 | +
|
| 48 | +This command can speak Spanish a command line option. |
| 49 | +
|
| 50 | +optional arguments: |
| 51 | + -h, --help show this help message and exit |
| 52 | + --spanish Respond in Spanish |
| 53 | +$ ./cmds2.py hi --spanish |
| 54 | +Hola |
| 55 | +""" |
0 commit comments