Command-line autocompletion for python -m module












0














Is it possible to get command-line autocompletion of python -m package.subpackage.module?



This is similar to, but not the same as, python ./package/subpackage/module.py, which does autocomplete the directory and file paths. However with -m, python runs the library's module as a script with the appropriate namespacing and import paths.



I'd like to be able to do python -m package.s[TAB] and get autocompletion to subpackage.



Is this feature built in somewhere, or how can I set it up?










share|improve this question




















  • 1




    The bash-completion tool is extensible.
    – Klaus D.
    Nov 12 '18 at 20:15
















0














Is it possible to get command-line autocompletion of python -m package.subpackage.module?



This is similar to, but not the same as, python ./package/subpackage/module.py, which does autocomplete the directory and file paths. However with -m, python runs the library's module as a script with the appropriate namespacing and import paths.



I'd like to be able to do python -m package.s[TAB] and get autocompletion to subpackage.



Is this feature built in somewhere, or how can I set it up?










share|improve this question




















  • 1




    The bash-completion tool is extensible.
    – Klaus D.
    Nov 12 '18 at 20:15














0












0








0







Is it possible to get command-line autocompletion of python -m package.subpackage.module?



This is similar to, but not the same as, python ./package/subpackage/module.py, which does autocomplete the directory and file paths. However with -m, python runs the library's module as a script with the appropriate namespacing and import paths.



I'd like to be able to do python -m package.s[TAB] and get autocompletion to subpackage.



Is this feature built in somewhere, or how can I set it up?










share|improve this question















Is it possible to get command-line autocompletion of python -m package.subpackage.module?



This is similar to, but not the same as, python ./package/subpackage/module.py, which does autocomplete the directory and file paths. However with -m, python runs the library's module as a script with the appropriate namespacing and import paths.



I'd like to be able to do python -m package.s[TAB] and get autocompletion to subpackage.



Is this feature built in somewhere, or how can I set it up?







python bash autocomplete bash-completion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:34









Amessihel

1,9691723




1,9691723










asked Nov 12 '18 at 20:13









HatshepsutHatshepsut

1,31211025




1,31211025








  • 1




    The bash-completion tool is extensible.
    – Klaus D.
    Nov 12 '18 at 20:15














  • 1




    The bash-completion tool is extensible.
    – Klaus D.
    Nov 12 '18 at 20:15








1




1




The bash-completion tool is extensible.
– Klaus D.
Nov 12 '18 at 20:15




The bash-completion tool is extensible.
– Klaus D.
Nov 12 '18 at 20:15












1 Answer
1






active

oldest

votes


















1














As said in the comment section, you need to extend the bash-completion tool. Then, you'll create a script which handles the cases you need (ie: when the last argument was -m).



This little sample below shows a start for your custom completion script. Let's name it python_completion.sh.



_python_target() {
local cur prev opts

# Retrieving the current typed argument
cur="${COMP_WORDS[COMP_CWORD]}"

# Retrieving the previous typed argument ("-m" for example)
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Preparing an array to store available list for completions
# COMREPLY will be checked to suggest the list
COMPREPLY=()

# Here, we'll only handle the case of "-m"
# Hence, the classic autocompletion is disabled
# (ie COMREPLY stays an empty array)
if [[ "$prev" != "-m" ]]
then
return 0
fi

# Retrieving paths and converts their separators into dots
# (if packages doesn't exist, same thing, empty array)
if [[ ! -e "./package" ]]
then
return 0
fi

# Otherwise, we retrieve first the paths starting with "./package"
# and converts their separators into dots
opts="$(find ./package -type d | sed -e 's+/+.+g' -e 's/^.//' | head)"

# We store the whole list by invoking "compgen" and filling
# COMREPLY with its output content.
COMPREPLY=($(compgen -W "$opts" -- "$cur"))

}

complete -F _python_target python


(Warning. This script has a flaw, it won't work with filenames containing spaces). To test it, run it in the current environnement:



. ./python_completion.sh


And test it:



python -m packag[TAB]


Here is a tutorial to continue in this way.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269417%2fcommand-line-autocompletion-for-python-m-module%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As said in the comment section, you need to extend the bash-completion tool. Then, you'll create a script which handles the cases you need (ie: when the last argument was -m).



    This little sample below shows a start for your custom completion script. Let's name it python_completion.sh.



    _python_target() {
    local cur prev opts

    # Retrieving the current typed argument
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Retrieving the previous typed argument ("-m" for example)
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Preparing an array to store available list for completions
    # COMREPLY will be checked to suggest the list
    COMPREPLY=()

    # Here, we'll only handle the case of "-m"
    # Hence, the classic autocompletion is disabled
    # (ie COMREPLY stays an empty array)
    if [[ "$prev" != "-m" ]]
    then
    return 0
    fi

    # Retrieving paths and converts their separators into dots
    # (if packages doesn't exist, same thing, empty array)
    if [[ ! -e "./package" ]]
    then
    return 0
    fi

    # Otherwise, we retrieve first the paths starting with "./package"
    # and converts their separators into dots
    opts="$(find ./package -type d | sed -e 's+/+.+g' -e 's/^.//' | head)"

    # We store the whole list by invoking "compgen" and filling
    # COMREPLY with its output content.
    COMPREPLY=($(compgen -W "$opts" -- "$cur"))

    }

    complete -F _python_target python


    (Warning. This script has a flaw, it won't work with filenames containing spaces). To test it, run it in the current environnement:



    . ./python_completion.sh


    And test it:



    python -m packag[TAB]


    Here is a tutorial to continue in this way.






    share|improve this answer




























      1














      As said in the comment section, you need to extend the bash-completion tool. Then, you'll create a script which handles the cases you need (ie: when the last argument was -m).



      This little sample below shows a start for your custom completion script. Let's name it python_completion.sh.



      _python_target() {
      local cur prev opts

      # Retrieving the current typed argument
      cur="${COMP_WORDS[COMP_CWORD]}"

      # Retrieving the previous typed argument ("-m" for example)
      prev="${COMP_WORDS[COMP_CWORD-1]}"

      # Preparing an array to store available list for completions
      # COMREPLY will be checked to suggest the list
      COMPREPLY=()

      # Here, we'll only handle the case of "-m"
      # Hence, the classic autocompletion is disabled
      # (ie COMREPLY stays an empty array)
      if [[ "$prev" != "-m" ]]
      then
      return 0
      fi

      # Retrieving paths and converts their separators into dots
      # (if packages doesn't exist, same thing, empty array)
      if [[ ! -e "./package" ]]
      then
      return 0
      fi

      # Otherwise, we retrieve first the paths starting with "./package"
      # and converts their separators into dots
      opts="$(find ./package -type d | sed -e 's+/+.+g' -e 's/^.//' | head)"

      # We store the whole list by invoking "compgen" and filling
      # COMREPLY with its output content.
      COMPREPLY=($(compgen -W "$opts" -- "$cur"))

      }

      complete -F _python_target python


      (Warning. This script has a flaw, it won't work with filenames containing spaces). To test it, run it in the current environnement:



      . ./python_completion.sh


      And test it:



      python -m packag[TAB]


      Here is a tutorial to continue in this way.






      share|improve this answer


























        1












        1








        1






        As said in the comment section, you need to extend the bash-completion tool. Then, you'll create a script which handles the cases you need (ie: when the last argument was -m).



        This little sample below shows a start for your custom completion script. Let's name it python_completion.sh.



        _python_target() {
        local cur prev opts

        # Retrieving the current typed argument
        cur="${COMP_WORDS[COMP_CWORD]}"

        # Retrieving the previous typed argument ("-m" for example)
        prev="${COMP_WORDS[COMP_CWORD-1]}"

        # Preparing an array to store available list for completions
        # COMREPLY will be checked to suggest the list
        COMPREPLY=()

        # Here, we'll only handle the case of "-m"
        # Hence, the classic autocompletion is disabled
        # (ie COMREPLY stays an empty array)
        if [[ "$prev" != "-m" ]]
        then
        return 0
        fi

        # Retrieving paths and converts their separators into dots
        # (if packages doesn't exist, same thing, empty array)
        if [[ ! -e "./package" ]]
        then
        return 0
        fi

        # Otherwise, we retrieve first the paths starting with "./package"
        # and converts their separators into dots
        opts="$(find ./package -type d | sed -e 's+/+.+g' -e 's/^.//' | head)"

        # We store the whole list by invoking "compgen" and filling
        # COMREPLY with its output content.
        COMPREPLY=($(compgen -W "$opts" -- "$cur"))

        }

        complete -F _python_target python


        (Warning. This script has a flaw, it won't work with filenames containing spaces). To test it, run it in the current environnement:



        . ./python_completion.sh


        And test it:



        python -m packag[TAB]


        Here is a tutorial to continue in this way.






        share|improve this answer














        As said in the comment section, you need to extend the bash-completion tool. Then, you'll create a script which handles the cases you need (ie: when the last argument was -m).



        This little sample below shows a start for your custom completion script. Let's name it python_completion.sh.



        _python_target() {
        local cur prev opts

        # Retrieving the current typed argument
        cur="${COMP_WORDS[COMP_CWORD]}"

        # Retrieving the previous typed argument ("-m" for example)
        prev="${COMP_WORDS[COMP_CWORD-1]}"

        # Preparing an array to store available list for completions
        # COMREPLY will be checked to suggest the list
        COMPREPLY=()

        # Here, we'll only handle the case of "-m"
        # Hence, the classic autocompletion is disabled
        # (ie COMREPLY stays an empty array)
        if [[ "$prev" != "-m" ]]
        then
        return 0
        fi

        # Retrieving paths and converts their separators into dots
        # (if packages doesn't exist, same thing, empty array)
        if [[ ! -e "./package" ]]
        then
        return 0
        fi

        # Otherwise, we retrieve first the paths starting with "./package"
        # and converts their separators into dots
        opts="$(find ./package -type d | sed -e 's+/+.+g' -e 's/^.//' | head)"

        # We store the whole list by invoking "compgen" and filling
        # COMREPLY with its output content.
        COMPREPLY=($(compgen -W "$opts" -- "$cur"))

        }

        complete -F _python_target python


        (Warning. This script has a flaw, it won't work with filenames containing spaces). To test it, run it in the current environnement:



        . ./python_completion.sh


        And test it:



        python -m packag[TAB]


        Here is a tutorial to continue in this way.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 21:28

























        answered Nov 12 '18 at 21:22









        AmessihelAmessihel

        1,9691723




        1,9691723






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269417%2fcommand-line-autocompletion-for-python-m-module%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values