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 whosepattern
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 ofOption
, 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 ofOption
.Notes
func
will become thecall
attribute associated with theCommand
object returned by this decorator. You should read the documentation ofcall
.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 ofOption
, the latter only being allowed for keyword arguments.
See also
- subcommand(func: Callable[[Command], Any])
Decorator used to register a subcommand inside a class’s context.