dryparse.decorators

The decorator API.

command(func: Callable[[...], Any])

Take a callable and turn it into a Command object.

Each positional argument will be converted to an Arguments object whose pattern will be generated using the type annotation.

Each keyword argument will be converted to an Option object, again using the information from the type annotation. If the annotation is an instance of Option, this instance will be used directly.

IMPORTANT: Type annotations must be actual types and not Union, Any etc. This is required because the type will used to convert CLI arguments into their Python representations. As a special case, the annotation for keyword arguments can be an instance of Option.

Notes

  • func will become the call attribute associated with the Command object returned by this decorator. You should read the documentation of call.

  • If a parameter is neither annotated nor has a default value, its type is assumed to be str.

  • The text for specifying an option on the command line is derived from the argument name in the following way:

    • The short text is formed by prepending a - before the first character of the argument name. Note that if multiple argument names start with the same character, only the first one will get a short text.

      Example: recursive becomes -r.

    • The long text is formed by prepending -- before the argument name, additionally replacing all _ characters with -.

      Example: work_dir becomes --work-dir.

Raises

AnnotationMustBeTypeOrSpecialError – If a type annotation is not a type, or an instance of Option, the latter only being allowed for keyword arguments.

subcommand(func: Callable[[Command], Any])

Decorator used to register a subcommand inside a class’s context.