Skip to content

Don't allow multiple options #160

@thewhaleking

Description

@thewhaleking

Typer allows multiple uses of the same option, and then just uses whichever was specified last. E.g. in btcli w balance --network ws://127.0.0.1:9945 --network local, it would use local and completely ignore the specified chain endpoint. This is usually not a big deal, but we have a number of aliases for network specifically:

"--network"
"--subtensor.network"
"--chain"
"--subtensor.chain_endpoint"

This can cause some confusion amongst people who are used to specifying --subtensor.network and --subtensor.chain_endpoint.

We can validate a single use of the commands with a callback, like so:
This does not work:

def validate_single_use(value: str, previous: str = None):
    if previous is not None:
        raise typer.BadParameter(f"Option '--network' was already used: {previous}")
    return value

@app.command()
def balance(
    network: str = typer.Option(None, "--network", callback=validate_single_use)
):
    typer.echo(f"Using network: {network}")

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions