-
Notifications
You must be signed in to change notification settings - Fork 124
Autocompleter #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Autocompleter #409
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6652557
Some fixes to autocompleter to make it easier to do delimited and fil…
anselor f44871b
Merge remote-tracking branch 'origin/master' into autocompleter
anselor 1c1da02
Changed some unit tests to use pytest-mock instead of mocker/monkeypa…
anselor 17159bf
Added new command to test validation.
anselor b75a2ee
reverting this test to see what happens
anselor 34c7e65
Bug fixes due to API differences in older versions of argcomplete
tleonhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -472,23 +472,43 @@ def _complete_for_arg(self, action: argparse.Action, | |
| if action.dest in self._arg_choices: | ||
| arg_choices = self._arg_choices[action.dest] | ||
|
|
||
| if isinstance(arg_choices, tuple) and len(arg_choices) > 0 and callable(arg_choices[0]): | ||
| completer = arg_choices[0] | ||
| # if arg_choices is a tuple | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything looks OK to me |
||
| # Let's see if it's a custom completion function. If it is, return what it provides | ||
| # To do this, we make sure the first element is either a callable | ||
| # or it's the name of a callable in the application | ||
| if isinstance(arg_choices, tuple) and len(arg_choices) > 0 and \ | ||
| (callable(arg_choices[0]) or | ||
| (isinstance(arg_choices[0], str) and hasattr(self._cmd2_app, arg_choices[0]) and | ||
| callable(getattr(self._cmd2_app, arg_choices[0])) | ||
| ) | ||
| ): | ||
|
|
||
| if callable(arg_choices[0]): | ||
| completer = arg_choices[0] | ||
| elif isinstance(arg_choices[0], str) and callable(getattr(self._cmd2_app, arg_choices[0])): | ||
| completer = getattr(self._cmd2_app, arg_choices[0]) | ||
|
|
||
| # extract the positional and keyword arguments from the tuple | ||
| list_args = None | ||
| kw_args = None | ||
| for index in range(1, len(arg_choices)): | ||
| if isinstance(arg_choices[index], list) or isinstance(arg_choices[index], tuple): | ||
| list_args = arg_choices[index] | ||
| elif isinstance(arg_choices[index], dict): | ||
| kw_args = arg_choices[index] | ||
| if list_args is not None and kw_args is not None: | ||
| return completer(text, line, begidx, endidx, *list_args, **kw_args) | ||
| elif list_args is not None: | ||
| return completer(text, line, begidx, endidx, *list_args) | ||
| elif kw_args is not None: | ||
| return completer(text, line, begidx, endidx, **kw_args) | ||
| else: | ||
| return completer(text, line, begidx, endidx) | ||
| try: | ||
| # call the provided function differently depending on the provided positional and keyword arguments | ||
| if list_args is not None and kw_args is not None: | ||
| return completer(text, line, begidx, endidx, *list_args, **kw_args) | ||
| elif list_args is not None: | ||
| return completer(text, line, begidx, endidx, *list_args) | ||
| elif kw_args is not None: | ||
| return completer(text, line, begidx, endidx, **kw_args) | ||
| else: | ||
| return completer(text, line, begidx, endidx) | ||
| except TypeError: | ||
| # assume this is due to an incorrect function signature, return nothing. | ||
| return [] | ||
| else: | ||
| return AutoCompleter.basic_complete(text, line, begidx, endidx, | ||
| self._resolve_choices_for_arg(action, used_values)) | ||
|
|
@@ -499,6 +519,16 @@ def _resolve_choices_for_arg(self, action: argparse.Action, used_values=()) -> L | |
| if action.dest in self._arg_choices: | ||
| args = self._arg_choices[action.dest] | ||
|
|
||
| # is the argument a string? If so, see if we can find an attribute in the | ||
| # application matching the string. | ||
| if isinstance(args, str): | ||
| try: | ||
| args = getattr(self._cmd2_app, args) | ||
| except AttributeError: | ||
| # Couldn't find anything matching the name | ||
| return [] | ||
|
|
||
| # is the provided argument a callable. If so, call it | ||
| if callable(args): | ||
| try: | ||
| if self._cmd2_app is not None: | ||
|
|
@@ -535,7 +565,10 @@ def _print_action_help(self, action: argparse.Action) -> None: | |
|
|
||
| prefix = '{}{}'.format(flags, param) | ||
| else: | ||
| prefix = '{}'.format(str(action.dest).upper()) | ||
| if action.dest != SUPPRESS: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this PR close Issue #407? If so, you should mark it as doing so in the description so that it auto-closes on merge. |
||
| prefix = '{}'.format(str(action.dest).upper()) | ||
| else: | ||
| prefix = '' | ||
|
|
||
| prefix = ' {0: <{width}} '.format(prefix, width=20) | ||
| pref_len = len(prefix) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anselor Would you please review the changes I made to your PR. These changes were to support older versions of
argcompletewhich have a few API differences including:FilesCompleteris not accessible at the top level__init__methodThese older versions of
argcompletewould commonly be found on older versions, in particular Python 3.4.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.