Skip to content

Commander

Command

Command decorator

This decorator is used to register a function as a command. It will automatically generate the command line arguments for the function based on the annotations.

Example
from duplocloud.commander import Command
from duplocloud import args
@Command()
def hello(name: args.NAME = "world"):
  print(f"Hello {name}!")

Returns:

Name Type Description
decorator Callable

The decorated function.

Resource

Resource decorator

Registers a class as a resource and optionally injects scope-specific functionality.

Parameters:

Name Type Description Default
name str

The name of the resource.

required
scope str

The scope of the resource. Valid values are "portal" or "tenant". Defaults to "portal".

'portal'

Returns:

Name Type Description
decorator

The decorator function.

Raises:

Type Description
ValueError

If an invalid scope is provided.

aliased_method

Aliased Method

Given the name of a command, check the schema and find the real method name because the command might be aliased. The given class will be used to discover any ancestors because the command may actually come from a parent class.

Parameters:

Name Type Description Default
cls Type

The class to check the schema for.

required
command str

The command to find the

required

Returns:

Name Type Description
method str

The true name of the commands method.

available_formats

Available Formats

Returns:

Type Description
List[str]

A list of available format names.

available_resources

Available Resources

Returns:

Type Description
List[str]

A list of available resources names.

commands_for

Commands For

Get all command methods decorated with @Command for a given resource.

This function builds a dictionary of all available commands for a resource, including inherited commands from parent classes. Parent class commands are added first to simulate class extension, then the resource's own commands are added, allowing child classes to override parent methods.

The schema is built from the global schema dict which tracks all @Command decorated methods, and the resources dict which tracks class inheritance relationships.

Parameters:

Name Type Description Default
name str

The name of the resource (e.g., "tenant", "service").

required

Returns:

Type Description
dict

A dict where keys are method names and values are command metadata dicts containing: - class: The class name where the method is defined - method: The method name - aliases: List of alternative command names

Example
commands_for("tenant")
# Returns: {"find": {"class": "DuploTenant", "method": "find", "aliases": []}}

extract_args

Extract Args

Extract the cli argument annotations from a function. This will only collect the args of type duplocloud.Arg. This list can now be used to generate an argparse.ArgumentParser object.

get_parser

Get Parser

Parameters:

Name Type Description Default
args List[Arg]

A list of Arg objects.

required

Returns: An argparse.ArgumentParser object with args from function.

load_format

Load Format

Load a Formatter function from the entry points.

Parameters:

Name Type Description Default
name str

The name of the format.

'string'

Returns: The class of the format.

load_resource

Load Service

Load a Service class from the entry points.

Parameters:

Name Type Description Default
name str

The name of the service.

required

Returns: The class of the service.