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

Extending python -m completions? #632

Open
shachargluska opened this issue Oct 19, 2021 · 1 comment
Open

Extending python -m completions? #632

shachargluska opened this issue Oct 19, 2021 · 1 comment

Comments

@shachargluska
Copy link

bash-completion adds auto completions for python executable, including -m flag and the existing modules.
But once a specific module is selected - is there a way to extend the completion?
E.g. if I type python -m http.server - and expected to see "-h, --help, --cgi, --bind", etc...

To clarify, I don't think bash-completions should include completions for all modules. That could be generated separately by an external tool (such as shtab or argcomplete).

Is there a way to extend the existing python completions?

This is related to iterative/shtab#55

@scop
Copy link
Owner

scop commented Oct 20, 2021

Only by modifying the current python completion code (or replacing it with some other implementation altogether). Here's an uglyish PoC:

--- a/completions/python
+++ b/completions/python
@@ -63,5 +63,14 @@ _python()
 
     # if -c or -m is already given, complete all kind of files.
-    if [[ ${words[*]::cword} == *\ -[cm]\ * ]]; then
+    if [[ ${words[*]::cword} =~ \ -([cm])\ +([A-Za-z0-9_.]+)? ]]; then
+        # For options after "-m foo", try to parse module help
+        if [[ $cur == -* &&
+            ${BASH_REMATCH[1]} == m && ${BASH_REMATCH[2]-} ]]; then
+            local opts=$(_parse_help "$1" "-m ${BASH_REMATCH[2]} -h")
+            if [[ ${opts-} ]]; then
+                COMPREPLY=($(compgen -W '$opts' -- "$cur"))
+                return
+            fi
+        fi
         _filedir
     elif [[ $cur != -* ]]; then

This is incomplete though. At minimum, we should additionally check if we are completing module options and skip the case blocks above entirely if we are. And this implementation doesn't have any support for module option arguments (such as --bind <TAB> for http.server), it just completes filenames for everything that doesn't look like an option. But perhaps that would already be an improvement (if the first issue would be fixed/implemented first).

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

No branches or pull requests

2 participants