Skip to content

CLI Dev

Dariusz Jarosz edited this page Apr 9, 2025 · 1 revision

Development of CLIs using the CLI Framework

With the package componentdb-cli it is possible to use it to create additional CLIs that can resue the configuration, authentication session, printing style, etc.

Configuration Manager

The configuration manager allows the user to define a configuration file or use the default of ~/.cdb/cdb.conf or ./cdb.conf. A custom path can also be specified using the following env variable CDB_CLI_CONFIG_FILE.

Configuration File

[WebPortal]
portalWebAddress = https://cdb.aps.anl.gov/cdb

[SessionInfo]
sessionCookieFilePath = ~/.cdb/cdb_api_session

Minimal CLI

It is possible to simply use the CLI framework to prompt for the configuration and session and use different frameworks for UI. Below you will find code example for the minimal CLI.

No Auth required

from CdbApiFactory import CdbApiFactory
from cdbCli.common.cli.cliBase import CliBase


cli = CliBase()

factory: CdbApiFactory = cli.require_api()

# Inteact with configured API
item_api = factory.getItemApi()

Auth required

from CdbApiFactory import CdbApiFactory
from cdbCli.common.cli.cliBase import CliBase


cli = CliBase()

factory: CdbApiFactory = (
    cli.require_authenticated_api()
)  # Will prompt for user and password if needed

# Inteact with configured API
item_api = factory.getItemApi()

Integrated CLI

The recommended appraoch is to follow the same framework and architecture to develop CLIs that match the CLIs that ship with the componentdb-cli package.

#!/usr/bin/env python3

import click

from rich.console import Console
from CdbApiFactory import CdbApiFactory
from cdbCli.common.cli import cliBase
from cdbCli.common.cli.cliBase import FORMAT_RICH_OPT, CliBase

# Default CLI configuration directory: ~/.cdb/


@click.command()
# Optional: Used for printing results using a unified CDB CLI format
@cliBase.wrap_print_format_cli_click_options  # Provides format argument.
def new_cli(format=FORMAT_RICH_OPT):
    cli = CliBase()

    # Optional: Used for printing results using a unified CDB CLI format
    console = Console()

    factory: CdbApiFactory = cli.require_authenticated_api()

    # For non-auth API calls use:
    # factory = cli.require_api()

    # TODO add some functionality that requires authentication
    cable_catalog_api = factory.getCableCatalogItemApi()
    cable_catalog_items = cable_catalog_api.get_cable_catalog_item_list()

    # Optional: Used for printing results using a unified CDB CLI format
    result_sections = {}
    cable_catalog_printables = []
    result_sections["Cable Catalog Items"] = cable_catalog_printables
    for cable_catalog_item in cable_catalog_items:
        cable_catalog_printable = {}
        cable_catalog_printable["ID"] = cable_catalog_item.id
        cable_catalog_printable["Name"] = cable_catalog_item.name
        cable_catalog_printables.append(cable_catalog_printable)
    cliBase.print_results(console, result_sections, format=format)


if __name__ == "__main__":
    new_cli()
Clone this wiki locally