Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions allways/cli/swap_commands/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,18 @@ def parse_global_flags() -> dict:
if '=' in arg:
flag, value = arg.split('=', 1)
if flag in _GLOBAL_FLAGS:
if value == '':
raise click.UsageError(f'{flag} requires a value')
overrides[_GLOBAL_FLAGS[flag]] = value
i += 1
continue
# Handle --flag value form
if arg in _GLOBAL_FLAGS:
if i + 1 < len(sys.argv):
overrides[_GLOBAL_FLAGS[arg]] = sys.argv[i + 1]
i += 2
continue
if i + 1 >= len(sys.argv) or sys.argv[i + 1].startswith('--'):
raise click.UsageError(f'{arg} requires a value')
overrides[_GLOBAL_FLAGS[arg]] = sys.argv[i + 1]
i += 2
continue
new_argv.append(arg)
i += 1
sys.argv[:] = new_argv
Expand Down
29 changes: 29 additions & 0 deletions tests/test_global_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys

import click
import pytest

from allways.cli.swap_commands.helpers import parse_global_flags


def test_bare_netuid_global_flag_reports_missing_value(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['alw', 'view', 'rates', '--netuid'])

with pytest.raises(click.UsageError, match='--netuid requires a value'):
parse_global_flags()


def test_empty_netuid_global_flag_reports_missing_value(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['alw', 'view', 'rates', '--netuid='])

with pytest.raises(click.UsageError, match='--netuid requires a value'):
parse_global_flags()


def test_netuid_global_flag_with_value_is_stripped(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['alw', 'view', 'rates', '--netuid', '7'])

overrides = parse_global_flags()

assert overrides == {'netuid': '7'}
assert sys.argv == ['alw', 'view', 'rates']