convenience functions for working with the Cmd module and other command line related stuff
Project description
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 ofoptions
before any command line options are appliedapply_opts(options,opts)
: apply theopts
tooptions
.opts
is 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 whereargv
contains 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 whereargv
contains the command line arguments.run_context(argv,options,cmd=None)
: 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 is
mainthen this is called with
cmd=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,
mainand
run_contextshould be static methods making no reference to
self`.
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.getopt
compatible option specifier. The default comes from the class'.GETOPT_SPEC
attribute.
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.Pfx
for context - intercepts
getopt.GetoptError
s, issues awarning
and runsself.do_help
with the method name, then returnsNone
- intercepts other
Exception
s, issues anexception
log message and returnsNone
The intended use is to decorate cmd.Cmd
do_
* methods:
@docmd def do_something(...): ... do something ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file cs.cmdutils-20190617.2.tar.gz
.
File metadata
- Download URL: cs.cmdutils-20190617.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f3e976c71275591d209284374c8db92b2065870f4d8354994a8fee444fac151 |
|
MD5 | c0946fb581c57a2bc78ed3b0e263935f |
|
BLAKE2b-256 | 0c4b1e1b56b66e296e8d7311fd75136418bf2d5f24b25795764fa3eed01613f8 |