@@ -38,44 +38,28 @@ class CommandNotFound(Exception):
3838
3939
4040class 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
4949def 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
0 commit comments