Skip to content
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

add subcommand tab completion for data plugin commands #1035

Merged

Conversation

DropD
Copy link
Contributor

@DropD DropD commented Jan 11, 2018

I am not sure if this should be in the release but it makes the recently added data plugin commands more user friendly, usage example:

$ verdi data mydata <Tab>
export list show
$ verdi data mydata ex<Tab> # will complete to export

The way it works is by inserting code in the current completion system that recognizes click commands and treats them specially:

All click type commands are actually invoked by invoking aiida.cmdline.commands.verdi (a click group).
Therefore I had to use sys.argv in the completion code to find the class of the actual subcommands in order to create a completion function for it using the .listcommands functionality that all click.MultiCommands have.

It may look hackish but I don't see a much cleaner way without changing more about how the current completion system works.

@DropD DropD added good first issue Issues that should be relatively easy to fix also for beginning contributors topic/plugin-system topic/verdi type/accepted feature approved feature request priority/nice-to-have coding-day/wip labels Jan 11, 2018
@DropD DropD added this to the v0.11.0 milestone Jan 11, 2018
@DropD DropD self-assigned this Jan 11, 2018
@DropD DropD removed coding-day/done good first issue Issues that should be relatively easy to fix also for beginning contributors labels Jan 11, 2018
@DropD
Copy link
Contributor Author

DropD commented Jan 11, 2018

@ltalirz Feel free to reassign to v0.12.0 If you think this non-essential.

@DropD
Copy link
Contributor Author

DropD commented Jan 11, 2018

It is worth mentioning that the previous behaviour was something like

$ verdi data mydata <Tab>
Usage: ...
...
Exception: no command "completion".
$ 

@DropD
Copy link
Contributor Author

DropD commented Jan 11, 2018

Also, please let me know if you can think of a way to test tab completion on travis.

Copy link
Member

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, very good! I only added a couple of small comments/questions. It works ok for 'old' commands, I couldn't test with click commands, are there any in aiida_core that would trigger the new code, or they are all in plugins?

if isinstance(cmd_or_class, (click.Command, click.MultiCommand)):
import sys
from aiida.cmdline.commands import click_subcmd_complete

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining why you take out the first 3 args and what is the content of sys.argv[4:len(sys.argv)-1]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested info is in the docstring of the function now.

@click.group()
@click.option('--profile', '-p')
def verdi(profile):
pass


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this command work? Is this a replacement for the Completion class in verdilib/when is this called? If this is the case (i.e. 'verdi completion' calls now this and not directly the Completion() class, , maybe we should move the Completion class out of verdilib, where it is automatically discovered (I'm assuming the autodiscover of the classes in verdilib is still active).

@giovannipizzi
Copy link
Member

BTW, for the testing, one could try to use an approach similar/adapted from the one used in this package: https://github.com/kislyuk/argcomplete/blob/master/test/test.py

@DropD
Copy link
Contributor Author

DropD commented Jan 15, 2018

There are no commands with which to test this currently in core. I used a data command i am developing in aiida-vasp to test it.

@DropD DropD force-pushed the plugin-subcmd-completion branch from ed37cb0 to 87eda2f Compare January 15, 2018 12:48
@DropD DropD changed the base branch from develop to release_v0.11.0 January 15, 2018 12:48
@DropD
Copy link
Contributor Author

DropD commented Jan 15, 2018

Rebased on release_v0.11.0 and adjusted the base of the PR.

Copy link
Member

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants