While the Command is what wraps the main logic of your program, Bases are responsible for parsing options, providing help, and actually running the commands.
If your program does not need multiple subcommands, you can just use a SingleBase. It does not require as much configuration as a MultiBase, as it gets most of its information from its Command.
This kind of Base accepts one Command, and you give it during construction. To run it, you call run() with the arguments.
By default, its use_help attribute is True. This injects a -h/--help option into the options list which will display help when encountered.
Parameters: |
|
---|
This is used as a callback when parsing options. If it returns something other than None, the value will not be stored.
Parameters: |
|
---|
Often, using a SingleBase to run your command is as simple as:
if __name__ == '__main__':
base = SingleBase(CommandClass)
base.run()
On the other hand, multiple-subcommand programs along the lines of hg or svn need the MultiBase.
This base handles programs with multiple “subcommands”, like hg, git, or svn. You add commands with the add_command() method. Like SingleBase, call run() to run the program.
By default, its use_help attribute is True. This injects a help command into the commands list, which will give information on the entire program or just a certain command.
Parameter: | program_name – The name to use for the program in usage messages. It defaults to the basename of the script’s path. |
---|
This adds a command to the commands list. The command’s name will override other commands already named or aliased as such, but its aliases will not.
Parameter: | command – Either a Command class or an instance thereof. |
---|
This is used as a callback when parsing options. If it returns something other than None, the value will not be stored.
Parameters: |
|
---|
However, it is often better to create your own subclass of MultiBase to use in your application. For example, the “todo.py” demo application uses this code for its base:
class TodoBase(MultiBase):
long_desc = """This is a simple to-do list manager. It uses a simple \
text file, and you can mark tasks as high, normal, or low priority."""
global_options = (
Option('f', 'file', argument=True, desc="The to-do list file "
"(defaults to ~/.todolist"),
Option('v', 'verbose', desc="Print more output than normal")
)
global_defaults = dict(
file=os.path.expanduser('~/.todolist'),
verbose=False
)
Both SingleBase and MultiBase are derived from a single class, Base. If you want to write a command-line application working on completely different principles, you can subclass from this to gain some useful utilities.
This is the base class for Bases. It handles the stuff common to both kinds of Base.
This will run a command with the given positional and keyword arguments, automatically catching ScriptError and other kinds of Exception. If you want to provide additional arguments to your commands, you can override this method and use Base.run_command(self, cmd, whatever). (The actual Base classes always call it with the options dictionary and arguments list.)
Parameters: |
|
---|