Skip to content

Commit c9900a5

Browse files
committed
Simplify parse_special_command logic to match mycli.
1 parent be32889 commit c9900a5

2 files changed

Lines changed: 24 additions & 40 deletions

File tree

litecli/packages/special/main.py

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,28 @@ class CommandNotFound(Exception):
3838

3939

4040
class Verbosity(Enum):
41-
"""Mode for special command invocation: regular, verbose (+), or succinct (-)."""
41+
"""Invocation verbosity: succinct (-), normal, or verbose (+)."""
4242

43-
REGULAR = "regular"
44-
VERBOSE = "verbose"
4543
SUCCINCT = "succinct"
44+
NORMAL = "normal"
45+
VERBOSE = "verbose"
4646

4747

4848
@export
4949
def parse_special_command(sql):
5050
"""
51-
Parse a special command prefix, extracting the base command name,
52-
an invocation mode (regular, verbose, or succinct), and the argument.
51+
Parse a special command, extracting the base command name, verbosity
52+
(normal, verbose (+), or succinct (-)), and the remaining argument.
53+
Mirrors mycli's behavior.
5354
"""
54-
raw, _, arg = sql.partition(" ")
55-
raw = raw.strip()
56-
57-
suffix_count = 0
58-
idx = len(raw) - 1
59-
while idx >= 0 and raw[idx] in "+-":
60-
suffix_count += 1
61-
idx -= 1
62-
if suffix_count > 1:
63-
raise ValueError("Invalid special command: %s" % raw)
64-
65-
if suffix_count == 1:
66-
suffix = raw[-1]
67-
base_cmd = raw[:-1]
68-
mode = Verbosity.VERBOSE if suffix == "+" else Verbosity.SUCCINCT
69-
else:
70-
suffix = None
71-
base_cmd = raw
72-
mode = Verbosity.REGULAR
73-
74-
last_char = base_cmd[-1] if base_cmd else None
75-
if suffix is None and last_char and not (last_char.isalnum() or last_char in "?."):
76-
raise ValueError("Invalid special command: %s" % raw)
77-
78-
return (base_cmd, mode, arg.strip())
55+
command, _, arg = sql.partition(" ")
56+
verbosity = Verbosity.NORMAL
57+
if "+" in command:
58+
verbosity = Verbosity.VERBOSE
59+
elif "-" in command:
60+
verbosity = Verbosity.SUCCINCT
61+
command = command.strip().strip("+-")
62+
return (command, verbosity, arg.strip())
7963

8064

8165
@export
@@ -135,7 +119,7 @@ def execute(cur, sql):
135119
"""Execute a special command and return the results. If the special command
136120
is not supported a KeyError will be raised.
137121
"""
138-
command, mode, arg = parse_special_command(sql)
122+
command, verbosity, arg = parse_special_command(sql)
139123

140124
if (command not in COMMANDS) and (command.lower() not in COMMANDS):
141125
raise CommandNotFound
@@ -150,7 +134,7 @@ def execute(cur, sql):
150134
if special_cmd.arg_type == NO_QUERY:
151135
return special_cmd.handler()
152136
elif special_cmd.arg_type == PARSED_QUERY:
153-
return special_cmd.handler(cur=cur, arg=arg, verbose=(mode is Verbosity.VERBOSE))
137+
return special_cmd.handler(cur=cur, arg=arg, verbose=(verbosity == Verbosity.VERBOSE))
154138
elif special_cmd.arg_type == RAW_QUERY:
155139
return special_cmd.handler(cur=cur, query=sql)
156140

tests/test_special_iocommands.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ def test_pipe_once_command():
6363
@pytest.mark.parametrize(
6464
"sql,expected",
6565
[
66-
(r"\d table_name", ("\\d", Verbosity.REGULAR, "table_name")),
66+
(r"\d table_name", ("\\d", Verbosity.NORMAL, "table_name")),
6767
(r"\d+ table_name", ("\\d", Verbosity.VERBOSE, "table_name")),
68-
(r"\?", ("\\?", Verbosity.REGULAR, "")),
69-
(r"\llm Question", ("\\llm", Verbosity.REGULAR, "Question")),
68+
(r"\?", ("\\?", Verbosity.NORMAL, "")),
69+
(r"\llm Question", ("\\llm", Verbosity.NORMAL, "Question")),
7070
(r"\llm-", ("\\llm", Verbosity.SUCCINCT, "")),
7171
(r"\llm+", ("\\llm", Verbosity.VERBOSE, "")),
7272
],
@@ -79,11 +79,11 @@ def test_parse_special_command(sql, expected):
7979
assert result == expected
8080

8181

82-
def test_parse_special_command_error():
82+
def test_parse_special_command_edge_cases():
83+
# mycli-compatible behavior: no ValueError on special characters; it parses leniently.
8384
sql = r"\llm* Question"
84-
with pytest.raises(ValueError):
85-
parse_special_command(sql)
85+
assert parse_special_command(sql) == ("\\llm*", Verbosity.NORMAL, "Question")
8686

8787
sql = r"\llm+- Question"
88-
with pytest.raises(ValueError):
89-
parse_special_command(sql)
88+
# '+' in command sets verbosity; strip('+-') removes both suffixes
89+
assert parse_special_command(sql) == ("\\llm", Verbosity.VERBOSE, "Question")

0 commit comments

Comments
 (0)