kislyuk/argcomplete

Tab Completion does not work, if we register locally using register-python-argcomplete for a particular file only

Closed this issue · 7 comments

Issue description: Tab Completion does not work, if we register locally using register-python-argcomplete for a particular python file

Steps:

  1. basic python program with argcomplete enabled with # PYTHON_ARGCOMPLETE_OK included
  2. Registered python executable locally by adding to the bash file
cat ~/.bashrc | grep eval
eval "$(register-python-argcomplete deployment.py)"
  1. Registered python executable same can be validated using below
complete | grep deployment
complete -o default -o nospace -F _python_argcomplete deployment.py
  1. Executing the script and pressing TAB does not complete the completion
    python3 deployment.py

Env details:
Python 3.6.9
bash 4.4.20(1)-release

Note: Not enabled activate-global-python-argcomplete globally, Since the use case trying to enable only specific file

Please refer to this section of the README:

If you choose not to use global completion, or ship a completion module that depends on argcomplete, you must register your script explicitly using eval "$(register-python-argcomplete my-python-app)". Standard completion module registration rules apply: namely, the script name is passed directly to complete, meaning it is only tab completed when invoked exactly as it was registered. In the above example, my-python-app must be on the path, and the user must be attempting to complete it by that name. The above line alone would not allow you to complete ./my-python-app, or /path/to/my-python-app.

In this case, you would need to have deployment.py on your path and be executable as deployment.py.

@evanunderscore As i mentioned on the 4th step, deployment.py on the same path, registred with same name
eval "$(register-python-argcomplete deployment.py)" via .bashrc script

Have you noticed any mismatch in register and invocation steps mentioned above?

You are running python3 deployment.py, but completion is registered for deployment.py, not python3. There is currently no way to register completion for a single script invoked via Python; that only works via global completion.

Thank you for clarification @evanunderscore , With Global completion tab works.

@evanunderscore One more observervation , positional arguments does not list for Subparsers upon TAB

Sample Script:

#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import argparse
import argcomplete

def main():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='command')

    # Define a subparser
    subparser_a = subparsers.add_parser('command_a', help='Command A')
    subparser_a.add_argument('--optinonal_arg_aa', help='Positional argument for command AA')
    subparser_a.add_argument('positional_arg_bb', help='Positional argument for command bb')

    # Define another subparser
    subparser_b = subparsers.add_parser('command_b', help='Command B')
    subparser_b.add_argument('positional_arg_b', help='Positional argument for command B')

    argcomplete.autocomplete(parser)
    args = parser.parse_args()
    print(args)

if __name__ == '__main__':
    main()

Result: TAB Completion only list the optional argument not the Positional Argument i,e positional_arg_bb

ssahu@rk051-s25:~/testing_dir$ ./test.py command_
command_a  command_b
ssahu@rk051-s25:~/testing_dir$ ./test.py command_a
 -h                     --help                 --optinonal_arg_aa     test.py


ssahu@rk051-s25:~/testing_dir$ ./test.py command_a -h
usage: test.py command_a [-h] [--optinonal_arg_aa OPTINONAL_ARG_AA] positional_arg_bb

positional arguments:
  positional_arg_bb     Positional argument for command bb

optional arguments:
  -h, --help            show this help message and exit
  --optinonal_arg_aa OPTINONAL_ARG_AA
                        Positional argument for command AA

This is functioning as expected. You don't literally type positional_arg_bb on the command line, so argcomplete won't suggest it, in the same way it won't suggest anything for the value of the optional argument once you've typed the flag.

I am closing this issue for now as there is no indication of a bug in argcomplete. If you disagree, please comment about specific improvements that you wish to see, and we can discuss.