API

argvard.__version__

The version as a string.

argvard.__version_info__

The version as a tuple, containing the major, minor, and bugfix version. You should use this, if you need to implement any version checks.

Application Object

class argvard.Argvard(defaults=None)

The argvard object is the central object of the command line application.

Instances are callable with the command line arguments, sys.argv by default.

The object acts as a registry for options and commands and calls them as necessary.

Parameters:defaults – A dictionary containing the initial values for the context.
classmethod from_main(signature='')

A decorator that creates an instance and registers the decorated function as main function, see main() for more information.

New in version 0.2.

main(signature='')

A decorator that is used to register the main function with the given signature:

@app.main()
def main(context):
    # do something
    pass

The main function is called, after any options and if no command has been called.

option(signature, overrideable=False)

A decorator for registering an option with the given signature:

@app.option('--option')
def option(context):
    # do something
    pass

If the name in the signature has already been used to register an option, a RuntimeError is raised unless the registered option has been defined with overrideable set to True.

Parameters:
  • signature – The signature of the option as a string.
  • overrideable – If True the registered option can be overridden.
register_command(name, command)

Registers the command with the given name.

If the name has already been used to register a command a RuntimeError will be raised.

Command Object

class argvard.Command(defaults=None)

A command - like an argvard object - is a registry of options and commands, that represents a distinct action.

Commands can be registered with any number of argvard objects, any number of times (under different names.)

Parameters:defaults – A dictionary containing the initial values for the context, any values already contained in the context once the command is called will not be overridden with a default value.
classmethod from_main(signature='')

A decorator that creates an instance and registers the decorated function as main function, see main() for more information.

New in version 0.2.

main(signature='')

A decorator that is used to register the main function with the given signature:

@app.main()
def main(context):
    # do something
    pass

The main function is called, after any options and if no command has been called.

option(signature, overrideable=False)

A decorator for registering an option with the given signature:

@app.option('--option')
def option(context):
    # do something
    pass

If the name in the signature has already been used to register an option, a RuntimeError is raised unless the registered option has been defined with overrideable set to True.

Parameters:
  • signature – The signature of the option as a string.
  • overrideable – If True the registered option can be overridden.
register_command(name, command)

Registers the command with the given name.

If the name has already been used to register a command a RuntimeError will be raised.

Context Object

class argvard.Context(argvard, application_name)

The context object is a dictionary, passed to options and main functions, which they can use to store information.

It further provides information useful for introspection and debugging through attributes.

argvard

The current application.

command

The current command or None.

command_path

A list containing the name of the application and the names of all commands called so far.

caller

The current command or argvard object.

Annotations

argvard.annotations(from_defaults=True, **kwargs)

A function decorator which coerces argument values to the annotated types. This decorator is implicitly applied to command and option functions. Explicitly wrapping your functions with it allows more fine-grained configuration and the usage of annotations in Python 2. Applying this decorator multiple times will raise a RuntimeError.

Parameters:
  • from_defaults – Infer the type of arguments by their default value.
  • kwargs – Python 2 doesn’t have function annotations, so you can also pass types here as keyword arguments.

Exceptions

class argvard.UsageError

The user used the program or command in a wrong way.

Raise this exception inside your functions to make argvard display the message, show the help page and exit.