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

Free shell auto-completion #21

Open
dknecht opened this issue Jun 17, 2012 · 8 comments
Open

Free shell auto-completion #21

dknecht opened this issue Jun 17, 2012 · 8 comments

Comments

@dknecht
Copy link

dknecht commented Jun 17, 2012

No description provided.

@keleshev
Copy link
Member

Usually this requires modifying ~/.bashrc in order to work. And very few people do that even for such complex interfaces as git.

So, requiring high effort and having low impact, this is given low priority, unless someone implements that and makes a pull request.

@wiggin15
Copy link

Check out infi.docopt_completion. It generates shell auto-completion for a given docopt utility.

easy_install infi.docopt_completion
docopt-completion [script name]

The code is available here:
https://github.com/Infinidat/infi.docopt_completion/

@Honghe
Copy link

Honghe commented Dec 10, 2013

@wiggin15 Test at docopt.__version__ = 0.6.1 but have problems, it does not work. at Ubuntu 12.04 32bit.
test the simple file docopt_example.py as follow:

 #!/usr/bin//env python
 # coding: utf-8

 """Usage:
     docopt_example.py tcp <host> <port> [--timeout=<seconds>]
     docopt_example.py serial <port> [--baud=9600] [--timeout=<seconds>]
     docopt_example.py -h | --help | --version

 """
 from docopt import docopt

 if __name__ == '__main__':
     arguments = docopt(__doc__, version='0.1.1rc')
     print arguments

The test command is:

sudo docopt-completion ./docopt_example.py  
Completion file written to /etc/bash_completion.d/docopt_example.py.sh

I think this means is OK, but still can not use tab-completion at ./docopt_example.py
So Which steps I was wrong?
Thanks!

@wiggin15
Copy link

You're right, looks like this happens because docopt-completion does not sanitize the "." in ".py". Please open an issue in the docopt-completion repository (link above) and I'll fix it. In the meantime you can change the name from docopt_example.py to docopt_example and see if that works.

@Honghe
Copy link

Honghe commented Dec 10, 2013

Thanks! Cause notebook not at hand, will report it tomorrow morning.

Honghe at Android Gmail
On Dec 10, 2013 11:10 PM, "wiggin15" notifications@github.com wrote:

You're right, looks like this happens because docopt-completion does not
sanitize the "." in ".py". Please open an issue in the docopt-completion
repository (link above) and I'll fix it. In the meantime you can change the
name from docopt_example.py to docopt_example and see if that works.


Reply to this email directly or view it on GitHubhttps://github.com//issues/21#issuecomment-30234818
.

@kbrock
Copy link

kbrock commented Mar 30, 2016

Taking a note from a crystal library completion, if you pass in the option --completion it generates the bash necessary to define an auto completion method.

But if you notice, for autocompletion to work best, the library (docopt in this case) would need some convention as to the data types of the values. Currently the variable name is mostly used. So this could be tricky.

@FlorianHeigl
Copy link

This is utterly wonderful. I knew about docopt for 3 years or so but only now made the connection to autogenerating tab completion in bash. May the bless you all.

@edi9999
Copy link

edi9999 commented Jan 18, 2022

For those who might come across this issue, I've written a simple (not complete) completion script for bash :

If your command is called xt, you simply have to add the following script, followed by completor xt

__auto_completor() {
	cmd="${COMP_WORDS[0]}"
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	if [ "$prev" = "$cmd" ]
	then
		opts="$("$cmd" --help |  grep "    $cmd" | sed -E "s@      $cmd ([^ ]+)( .*)?@\1@g")"
		COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
	else
		COMPREPLY=( $(compgen -f -- "$cur") )
	fi
	return 0
}

completor() {
	cmd="$1"
	complete -F "__auto_completor" "$cmd"
}
# End of "completion-library", you can now call "completor cmdname"

completor xt

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

No branches or pull requests

7 participants