Skip to content

Commit ea4460f

Browse files
authored
Merge pull request #6 from sharbov/fix_completion_issue
Fix some completion issues
2 parents 99c6e21 + 0d4f3eb commit ea4460f

3 files changed

Lines changed: 58 additions & 20 deletions

File tree

open_cli/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def run_loop(self):
7373

7474
except Exception as exc:
7575
self.logger.error(exc)
76+
print(exc)
7677

7778
def execute(self, command):
7879
"""Parse and execute the given command."""

open_cli/completer.py

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ def _text_to_completions(self, text):
2323
except ValueError:
2424
words = text.split(" ")
2525

26-
operation, text = self._extract_operation(words=words)
26+
operation, remaining_text = self._extract_operation(words=words)
2727

2828
if callable(operation):
29-
return self._get_operation_params_completions(operation=operation, text=text)
30-
31-
return self._get_completion(text=text, options=dir(operation))
29+
return self._get_operation_params_completions(
30+
original_text=text,
31+
remaining_text=remaining_text,
32+
operation=operation,
33+
)
34+
35+
return self._get_completion(
36+
original_text=text,
37+
remaining_text=remaining_text,
38+
options=dir(operation)
39+
)
3240

3341
def _extract_operation(self, words):
3442
"""Get the required client operation and separate it from the remaining text."""
@@ -43,26 +51,41 @@ def _extract_operation(self, words):
4351

4452
return operation, ""
4553

46-
def _get_operation_params_completions(self, operation, text):
54+
def _get_operation_params_completions(self, original_text, remaining_text, operation):
4755
"""Get suggestions based on operation and remaining text."""
4856
completion_offset = 0
4957

5058
# Strip argument prefix
51-
if text.startswith("--"):
52-
text = text[2:]
53-
completion_offset = 2
59+
if remaining_text.startswith("--"):
60+
61+
if len(remaining_text.split("=")) == 2:
62+
# Already a valid param
63+
remaining_text = ""
64+
65+
else:
66+
remaining_text = remaining_text[2:]
67+
completion_offset = 2
5468

5569
# Handel definition type argument completions
56-
if "." in text:
57-
return self._get_definition_completions(operation, text)
70+
if "." in remaining_text:
71+
return self._get_definition_completions(
72+
original_text=original_text,
73+
remaining_text=remaining_text,
74+
operation=operation
75+
)
76+
77+
if self.should_hide_completions(original_text=original_text,
78+
remaining_text=remaining_text,
79+
allowed_suffixes=(" ", "-")):
80+
return []
5881

59-
return [("--" + attribute, len(text) + completion_offset)
82+
return [("--" + attribute, len(remaining_text) + completion_offset)
6083
for attribute in operation.operation.params
61-
if attribute.startswith(text) and not attribute.startswith("_")]
84+
if attribute.startswith(remaining_text) and not attribute.startswith("_")]
6285

63-
def _get_definition_completions(self, operation, text):
86+
def _get_definition_completions(self, original_text, remaining_text, operation):
6487
"""Get suggestions based on definition and remaining text."""
65-
param_words = text.split(".")
88+
param_words = remaining_text.split(".")
6689

6790
# Only two words parameter completion are supported
6891
if len(param_words) != 2:
@@ -87,10 +110,24 @@ def _get_definition_completions(self, operation, text):
87110
if not definition:
88111
return []
89112

90-
return self._get_completion(text=sub_name, options=dir(definition()))
113+
return self._get_completion(
114+
original_text=original_text,
115+
remaining_text=sub_name,
116+
options=dir(definition())
117+
)
91118

92-
@staticmethod
93-
def _get_completion(text, options):
119+
def _get_completion(self, original_text, remaining_text, options):
94120
"""Get completion properties based on text and possible options."""
95-
return [(option, len(text)) for option in options
96-
if option.startswith(text) and not option.startswith("_")]
121+
if self.should_hide_completions(original_text=original_text,
122+
remaining_text=remaining_text,
123+
allowed_suffixes=(" ", ".")):
124+
return []
125+
126+
return [(option, len(remaining_text)) for option in options
127+
if option.startswith(remaining_text) and not option.startswith("_")]
128+
129+
@staticmethod
130+
def should_hide_completions(original_text, remaining_text, allowed_suffixes):
131+
return (original_text and
132+
not remaining_text and
133+
original_text[-1] not in allowed_suffixes)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fire==0.1.3
2-
bravado==9.2.2
2+
bravado==10.3.2
33
tabulate==0.8.2
44
prompt-toolkit==1.0.15

0 commit comments

Comments
 (0)