Skip to content

CLI App

Main CLI application entry point.

Main CLI application entry point.

This module defines the qsh command-line application, its top-level callback, and a set of built-in commands. Plugins are discovered and loaded lazily to keep startup fast.

Examples:

Running the CLI and showing help::

qsh --help

Starting the REPL with a specific context and JSON output::

qsh --context prod --output json repl

Printing shell completion for zsh::

qsh completion zsh > ~/.zsh/completions/_qsh

OutputFormat

Bases: str, Enum


              flowchart TD
              qsh.cli.app.OutputFormat[OutputFormat]

              

              click qsh.cli.app.OutputFormat href "" "qsh.cli.app.OutputFormat"
            

Output format options for the CLI.

app_callback

app_callback(context: Annotated[str | None, Option('--context', help='Select context from config')] = None, output: Annotated[OutputFormat | None, Option('-o', '--output', help='Output format: table|json')] = None, verbose: Annotated[bool, Option('--verbose', '-v', help='Enable verbose logging')] = False, wrap: Annotated[bool, Option('--wrap', help='Allow multiline wrapping in table output')] = False, private: Annotated[bool, Option('--private', help='Use private API endpoints (requires super admin access)')] = False) -> None

CLI for QuEra's Quantum Computing Service.

:param context: Optional config context to use for this invocation. :param output: Optional output format override ("table" or "json"). :param verbose: Enable verbose logging for diagnostics. :param wrap: Allow multiline wrapping in table output. :param private: Use private API endpoints (requires super admin access).

Source code in qsh/cli/app.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
@app.callback()
def app_callback(
    context: Annotated[
        str | None, typer.Option("--context", help="Select context from config")
    ] = None,
    output: Annotated[
        OutputFormat | None,
        typer.Option("-o", "--output", help="Output format: table|json"),
    ] = None,
    verbose: Annotated[
        bool, typer.Option("--verbose", "-v", help="Enable verbose logging")
    ] = False,
    wrap: Annotated[
        bool, typer.Option("--wrap", help="Allow multiline wrapping in table output")
    ] = False,
    private: Annotated[
        bool,
        typer.Option("--private", help="Use private API endpoints (requires super admin access)"),
    ] = False,
) -> None:
    """CLI for QuEra's Quantum Computing Service.

    :param context: Optional config context to use for this invocation.
    :param output: Optional output format override ("table" or "json").
    :param verbose: Enable verbose logging for diagnostics.
    :param wrap: Allow multiline wrapping in table output.
    :param private: Use private API endpoints (requires super admin access).
    """

    logger = setup_logging(verbose)
    logger.debug("CLI started with verbose logging enabled" if verbose else "CLI started")

    app.state = {
        "context": context,
        "output": output,
        "verbose": verbose,
        "wrap": wrap,
        "private": private,
    }

clear_app_context_cache

clear_app_context_cache() -> None

Clear the cached AppContext and config to force reload next time.

Source code in qsh/cli/app.py
312
313
314
315
316
317
def clear_app_context_cache() -> None:
    """Clear the cached ``AppContext`` and config to force reload next time."""
    if hasattr(app, "_context_cache"):
        app._context_cache.clear()
    if hasattr(app, "_config_cache"):
        app._config_cache.clear()

cli_main

cli_main() -> None

Main entry point that loads plugins only when needed.

This function may terminate the process with a non-zero exit code on error.

Source code in qsh/cli/app.py
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
def cli_main() -> None:
    """Main entry point that loads plugins only when needed.

    This function may terminate the process with a non-zero exit code on error.
    """
    try:
        _run_cli()
    except KeyboardInterrupt:
        _handle_keyboard_interrupt()
    except AuthConfigurationError as e:
        _handle_auth_configuration_error(e)
    except APIError as e:
        _handle_api_error(e)
    except QlamCoreError as e:
        _handle_qshcli_error(e)
    except httpx.RequestError as e:
        _handle_httpx_request_error(e)
    except Exception as e:  # noqa: BLE001
        # CLI boundary: last-resort catch-all. We intentionally map unexpected failures
        # to ExitCode.UNKNOWN_ERROR here to keep the rest of the codebase free of
        # blanket exception handling.
        _handle_generic_exception(e)

completion_command

completion_command(shell: Annotated[str, Argument(help='Shell type: bash, zsh, fish, or powershell')]) -> None

Generate shell completion scripts for the given shell.

:param shell: Target shell (bash, zsh, fish, or powershell). :raises typer.Exit: If the shell is unsupported.

Examples:

qsh completion bash > ~/.qsh_completion source ~/.qsh_completion

qsh completion zsh > ~/.zsh/completions/_qsh

qsh completion fish > ~/.config/fish/completions/qsh.fish

Source code in qsh/cli/app.py
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
@app.command("completion")
def completion_command(
    shell: Annotated[str, typer.Argument(help="Shell type: bash, zsh, fish, or powershell")],
) -> None:
    """Generate shell completion scripts for the given shell.

    :param shell: Target shell (``bash``, ``zsh``, ``fish``, or ``powershell``).
    :raises typer.Exit: If the shell is unsupported.

    Examples:
        qsh completion bash > ~/.qsh_completion
        source ~/.qsh_completion

        qsh completion zsh > ~/.zsh/completions/_qsh

        qsh completion fish > ~/.config/fish/completions/qsh.fish
    """

    shell = shell.lower()
    prog_name = "qsh"

    if shell == "bash":
        completion_script = f"""
_{prog_name.upper()}_COMPLETE=bash_source {prog_name}
"""
    elif shell == "zsh":
        completion_script = f"""
#compdef {prog_name}
_{prog_name.upper()}_COMPLETE=zsh_source {prog_name}
"""
    elif shell == "fish":
        completion_script = f"""
complete -c {prog_name} -f -a "(env _{prog_name.upper()}_COMPLETE=fish_source {prog_name})"
"""
    elif shell == "powershell":
        completion_script = f"""
Register-ArgumentCompleter -Native -CommandName {prog_name} -ScriptBlock {{
    param($wordToComplete, $commandAst, $cursorPosition)
    $env:_{prog_name.upper()}_COMPLETE="powershell_source"
    $result = & {prog_name} 2>&1
    $env:_{prog_name.upper()}_COMPLETE=""
    $result
}}
"""
    else:
        console.print(
            f"[red]Error: Unsupported shell '{shell}'. Supported shells: bash, zsh, fish, powershell[/red]"
        )
        raise typer.Exit(1)

    # Output the completion script
    print(completion_script.strip())

context_list

context_list(output: str | None = typer.Option(None, '-o', '--output', help='Output format (json, table).')) -> None

List all configured contexts.

Source code in qsh/cli/app.py
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
@_context_app.command("list")
def context_list(
    output: str | None = typer.Option(None, "-o", "--output", help="Output format (json, table)."),
) -> None:
    """List all configured contexts."""
    from qsh.cli.io import render

    ctx = create_app_context()
    config = ctx.config
    active_name = config.context_name

    rows: list[dict] = []
    for c in config.contexts:
        row: dict = {
            "name": c.name,
            "active": "*" if c.name == active_name else "",
            "qpu": c.qpu,
        }
        if c.defaults and c.defaults.api_base_url:
            row["api_base_url"] = str(c.defaults.api_base_url)
        rows.append(row)

    render(rows, output or ctx.effective_output_format)

context_set

context_set(name: str = typer.Argument(..., help='Context name to set as active.')) -> None

Set the active context persisted in config.

Source code in qsh/cli/app.py
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
@_context_app.command("set")
def context_set(
    name: str = typer.Argument(..., help="Context name to set as active."),
) -> None:
    """Set the active context persisted in config."""
    from qlam_core.config.config import Config

    config = Config.load()

    available = config.context_names
    if name not in available:
        typer.echo(f"Error: context '{name}' not found.", err=True)
        typer.echo(f"Available contexts: {', '.join(available)}", err=True)
        raise typer.Exit(code=1)

    config.set_context(name)
    config.save()
    clear_app_context_cache()

    typer.echo(f"Switched to context '{name}'.")

context_show

context_show(output: str | None = typer.Option(None, '-o', '--output', help='Output format (json, table).')) -> None

Display the current active context name.

Source code in qsh/cli/app.py
325
326
327
328
329
330
331
332
333
334
@_context_app.command("show")
def context_show(
    output: str | None = typer.Option(None, "-o", "--output", help="Output format (json, table)."),
) -> None:
    """Display the current active context name."""
    from qsh.cli.io import render

    ctx = create_app_context()
    result = {"current_context": ctx.config.context_name}
    render(result, output or ctx.effective_output_format)

create_app_context

create_app_context() -> 'AppContext'

Create and cache an AppContext populated from current CLI state.

The context and settings are cached across invocations during a single process run to avoid repeated disk I/O.

:return: Initialized AppContext instance.

Source code in qsh/cli/app.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
def create_app_context() -> "AppContext":
    """Create and cache an ``AppContext`` populated from current CLI state.

    The context and settings are cached across invocations during a single
    process run to avoid repeated disk I/O.

    :return: Initialized ``AppContext`` instance.
    """
    from qlam_core.common.context import AppContext

    state = getattr(app, "state", {})
    cache_key = f"context_{state.get('context', 'default')}_{state.get('verbose', False)}"

    if hasattr(app, "_context_cache") and cache_key in app._context_cache:
        from qlam_core.common.visibility import Visibility

        cached_context = app._context_cache[cache_key]
        # Update dynamic properties that may change between calls
        output = state.get("output")
        cached_context.output_format = output.value if output else None
        cached_context.wrap = state.get("wrap", False)
        cached_context.visibility = Visibility.PRIVATE if state.get("private") else None
        return cached_context

    main_logger = logging.getLogger("qsh")
    context_logger = main_logger.getChild("context")

    config_cache_key = f"config_{state.get('context', 'default')}"
    if not hasattr(app, "_config_cache"):
        app._config_cache = {}

    if config_cache_key not in app._config_cache:
        from qlam_core.config.config import Config

        config = Config.load(state.get("context"))
        app._config_cache[config_cache_key] = config
    else:
        config = app._config_cache[config_cache_key]

    from qlam_core.common.visibility import Visibility

    context = AppContext(
        context_name=state.get("context"),
        output_format=state.get("output").value if state.get("output") else None,
        verbose=state.get("verbose", False),
        wrap=state.get("wrap", False),
        logger=context_logger,
        cached_config=config,
        visibility=Visibility.PRIVATE if state.get("private") else None,
    )

    if not hasattr(app, "_context_cache"):
        app._context_cache = {}
    app._context_cache[cache_key] = context

    return context

help_command

help_command(ctx: Context) -> None

Show help for QSH commands (equivalent to qsh --help).

:param ctx: Typer context (unused; kept for parity with Typer conventions).

Source code in qsh/cli/app.py
404
405
406
407
408
409
410
411
412
413
@app.command("help")
def help_command(ctx: typer.Context) -> None:
    """Show help for QSH commands (equivalent to ``qsh --help``).

    :param ctx: Typer context (unused; kept for parity with Typer conventions).
    """
    click_app = typer.main.get_command(app)
    help_ctx = click_app.make_context("qsh", ["--help"], resilient_parsing=True)
    help_text = click_app.get_help(help_ctx)
    console.print(help_text)

load_plugins

load_plugins(app: Typer, ctx_provider: Callable[[], 'AppContext']) -> None

Lazy wrapper around plugin loading.

This keeps plugin discovery imports out of module import time for fast built-ins like qsh version and qsh completion.

Source code in qsh/cli/app.py
 93
 94
 95
 96
 97
 98
 99
100
101
def load_plugins(app: typer.Typer, ctx_provider: Callable[[], "AppContext"]) -> None:
    """Lazy wrapper around plugin loading.

    This keeps plugin discovery imports out of module import time for fast built-ins
    like ``qsh version`` and ``qsh completion``.
    """
    from qsh.cli.wiring import load_plugins as _load_plugins

    _load_plugins(app, ctx_provider)

main

main() -> None

Entry point for the qsh command.

Source code in qsh/cli/app.py
502
503
504
def main() -> None:
    """Entry point for the qsh command."""
    cli_main()

repl_command

repl_command() -> None

Start an interactive REPL session.

The REPL provides an interactive shell where you can run QSH commands without having to type 'qsh' before each command. It supports tab completion and maintains the current context config.

Examples:

qsh repl # Start interactive shell qsh --context prod repl # Start REPL with specific context

Source code in qsh/cli/app.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
@app.command(name="repl")
def repl_command() -> None:
    """Start an interactive REPL session.

    The REPL provides an interactive shell where you can run QSH commands
    without having to type 'qsh' before each command. It supports tab
    completion and maintains the current context config.

    Examples:
        qsh repl                    # Start interactive shell
        qsh --context prod repl     # Start REPL with specific context
    """
    from qsh.cli.repl import start_repl

    start_repl(app, console)

setup_logging

setup_logging(verbose: bool = False) -> logging.Logger

Configure logging with a rich handler and return the main logger.

:param verbose: Enable verbose log level (DEBUG) when True, otherwise WARNING. :return: Configured logger named qsh.

Source code in qsh/cli/app.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def setup_logging(verbose: bool = False) -> logging.Logger:
    """Configure logging with a rich handler and return the main logger.

    :param verbose: Enable verbose log level (DEBUG) when True, otherwise WARNING.
    :return: Configured logger named ``qsh``.
    """
    level = logging.DEBUG if verbose else logging.WARNING

    # Create main logger
    logger = logging.getLogger("qsh")
    logger.setLevel(level)

    # Remove any existing handlers to avoid duplicates
    for handler in logger.handlers[:]:
        logger.removeHandler(handler)

    # Add rich handler
    handler = RichHandler(console=console, rich_tracebacks=True, show_path=False)
    handler.setFormatter(logging.Formatter("%(message)s"))
    logger.addHandler(handler)

    # Prevent propagation to root logger
    logger.propagate = False

    return logger

version_command

version_command() -> None

Show version information.

Source code in qsh/cli/app.py
416
417
418
419
420
421
@app.command("version")
def version_command() -> None:
    """Show version information."""
    from qsh.utils.version import get_version

    console.print(f"qsh-client version {get_version()}")