Show an error if both --alias and --host are defined (#513)

Display an error if both --alias and --host are defined to avoid ambiguous target device:
```
❯ kasa --host 123 --alias 123 state
Usage: kasa [OPTIONS] COMMAND [ARGS]...
Try 'kasa --help' for help.

Error: Use either --alias or --host, not both.
```

Also, use `click.BadOptionUsage` consistently for other errors, like when only `--username` or `--password` is given.
This commit is contained in:
Teemu R
2023-10-04 23:35:26 +02:00
committed by GitHub
parent a2444da9df
commit 84a501bcdc
2 changed files with 27 additions and 5 deletions

View File

@@ -209,6 +209,9 @@ async def cli(
if ctx.invoked_subcommand == "discover":
return
if alias is not None and host is not None:
raise click.BadOptionUsage("alias", "Use either --alias or --host, not both.")
if alias is not None and host is None:
echo(f"Alias is given, using discovery to find host {alias}")
host = await find_host_from_alias(alias=alias, target=target)
@@ -219,8 +222,9 @@ async def cli(
return
if bool(password) != bool(username):
echo("Using authentication requires both --username and --password")
return
raise click.BadOptionUsage(
"username", "Using authentication requires both --username and --password"
)
credentials = Credentials(username=username, password=password)