Class BaseCommand
A base class for handling nestable command lines.
This class provides the basic parse and dispatch mechanisms for command lines. To implement a command line one instantiates a subclass of BaseCommand:
class MyCommand(BaseCommand):
GETOPT_SPEC = 'ab:c'
...
the_cmd = MyCommand()
Running a command is done by:
the_cmd.run(argv)
The subclass is customised by overriding the following methods:
apply_defaults(options): prepare the initial state ofoptionsbefore any command line options are appliedapply_opts(options,opts): apply theoptstooptions.optsis an option value mapping as returned bygetopot.getopt.cmd_subcmd(argv,options): if the command line options are followed by an argument whose value is subcmd, then methodcmd_subcmd(argv,options)will be called whereargvcontains the command line arguments after subcmd.main(argv,options): if there are no command line aguments after the options or the first argument does not have a correspondingcmd_subcmd method then methodmain(argv,options)will be called whereargvcontains the command line arguments.run_context(argv,options,cmd): a context manager to provide setup or teardown actions to occur before and after the command implementation respectively. If the implementation is acmd_subcmd method then this is called withcmd=subcmd; if the implementation ismainthen this is called withcmd=None.
To aid recursive use
it is intended that all the per command state
is contained in the options object
and therefore that in typical use
all of apply_opts, cmd_subcmd, mainandrun_contextshould be static methods making no reference toself`.
Editorial: why not arparse?
Primarily because when incorrectly invoked
an argparse command line prints the help/usage messgae
and aborts the whole programme with SystemExit.
Method BaseCommand.__init__(self, getopt_spec=None)
Initialise the BaseCommand.
Parameters:
getopt_spec: optionalgetopt.getoptcompatible option specifier. The default comes from the class'.GETOPT_SPECattribute.
Function docmd(dofunc)
Decorator for Cmd subclass methods to supply some basic quality of service.
This decorator:
- wraps the function call in a
cs.pfx.Pfxfor context - intercepts
getopt.GetoptErrors, issues awarningand runsself.do_helpwith the method name, then returnsNone - intercepts other
Exceptions, issues anexceptionlog message and returnsNone
The intended use is to decorate cmd.Cmd do_* methods:
from cmd import Cmd
class MyCmd(Cmd):
@docmd
def do_something(...):
... do something ...
Release files for cs-cmdutils 20190619
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cs.cmdutils-20190619.tar.gz | 4.9 kB | Details |
Release files / cs.cmdutils-20190619.tar.gz
| Download URL | cs.cmdutils-20190619.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5217cfe769a2ed67a3482453587f6e7af9b6206218040e8f8bc05a9c8a52c422
|
|
BLAKE2b-256 checksum How to use checksums |
05855356f1808dc1baf4e4e9edf64f02ed485926e3a44573313329b281623ced
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|