File: subcommands.rst

package info (click to toggle)
python-cliapp 1.20160724-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 416 kB
  • ctags: 603
  • sloc: python: 3,157; makefile: 93
file content (34 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Subcommands
===========

Sometimes a command line tool needs to support subcommands.
For example, version control tools often do this:
``git commit``, ``git clone``, etc. To do this with ``cliapp``,
you need to add methods with names like ``cmd_commit`` and
``cmd_clone``::

    class VersionControlTool(cliapp.Application):

        def cmd_commit(self, args):
            '''commit command description'''
            pass
        def cmd_clone(self, args):
            '''clone command description'''
            pass

If any such methods exist, ``cliapp`` automatically supports
subcommands. The name of the method, without the ``cmd_`` prefix,
forms the name of the subcommand. Any underscores in the method
name get converted to dashes in the command line. Case is
preserved.

Subcommands may also be added using the ``add_subcommand`` method.

All options are global, not specific to the subcommand.
All non-option arguments are passed to the method in its only
argument.

Subcommands are implemented by the ``process_args`` method.
If you override that method, you need to support subcommands
yourself (perhaps by calling the ``cliapp`` implementation).