straight.command Package

Table of Contents

The Command class

class straight.command.Command(parent=None)

Bases: object

Collections and parses options to implement a command.

Commands can collect options defined as attributes of a subclass or loaded from one or more namespaces, to be loaded by the plug-in loader straight.command.

An instance of Command can be used to parse() an argument list, or to run() the command, which first parses and then carries out the task the command was meant for.

before_opts()
default = False
execute(**kwargs)
loadOptions(namespace)

Load options from a plugin namespace, and also from any options defined as part of the class body.

The namespace is used to search all your available python packages and locate anything within that namespace. By default, the namespace "straight.command" is used to locate default options, which are found in the straight.command.default_options module.

Your application can define its own namespace where you can easily add options to be located, and if you document this namespace other developers can extend your commands with new options by providing namespace packages with their own options plugins.

option_ns = None
parse(arguments)

Parse all known arguments, populating the args dict.

run(arguments=None)

Parse arguments and invoke resulting actions.

subcommand = None
version = 'unknown'

Option types

class straight.command.Option(**kwargs)

Bases: object

Defines a single option a command can take.

Options can define a short (-s) or long (–long) or be positional.

Initialization parameters:

  • short an optional single-dash (-s) argument to accept

  • long an optional single-dash (–long) argument to accept

  • dest the name to save any resulting values to

  • action the action to peform if an option is matched Can be one of:

    store to accept one value to store append to accept multiple values to store in a list store_true to store True if matched store_false to store False if matched

  • coerce a callable accepting the given string value for an option, and returning a value of a correct type

  • short_circuit true if the option can be the only one run

action_append(consumer, ns, mode)

Action to collect all values of the option, if repeated, into a single list.

action_store(consumer, ns, mode)

Action to simple store an expected value.

action_store_false(consumer, ns, mode)

Action to store False, and not accept a value.

action_store_true(consumer, ns, mode)

Action to store True, and not accept a value.

index_for(cmd)

Provides the index number to order an option in a command.

Defaults the order they were created.

parse(consumer, ns)

Parse the next argument in args if it matches this option, and execute its action accordingly.

run(cmd)

An Option subclass can define run() to invoke some behavior during the commands run-phase, if the option had been matched.

class straight.command.SubCommand(name=None, command_class=None, *args, **kwargs)

Bases: straight.command.Option

Implements a “sub-command option”, which consumes all the remaining options and delegates them to another Command.

Requires a name and a Command sub-class to delegate to.

command_class = None
name = None
parse(consumer, ns)

Consumes ALL remaining arguments and prepares to send them to the sub-command.

run(cmd, as_default=False)

Runs the subcommand.

default_options Module

Default options inherited by all commands.

class straight.command.default_options.Help(**kwargs)

Bases: straight.command.Option

action = 'store_true'
dest = 'help'
help = 'Print this help message.'
long = '--help'
run(cmd)
short = '-h'
short_circuit = True
class straight.command.default_options.VersionOption(**kwargs)

Bases: straight.command.Option

action = 'store_true'
dest = 'version'
help = 'Report the current version of a command or subcommand.'
long = '--version'
run(cmd)
short_circuit = True