0

I have the following bash script that I source in my root directory:

ROOT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
eval "$(register-python-argcomplete ${ROOT_DIR}/bin/execute.py)"

The execute.py file has Python autocompletion enabled and it works perfectly when I use the full path to my script in the bash shell:

/absolute/path/to/root/bin/execute.py <Tab>   # This works perfectly

Now I want to create an alias, say my_command, so that I don't have to provide the full path. I want to use just:

my_command <Tab>

I've tried creating a simple bash alias:

alias my_command="${ROOT_DIR}/bin/execute.py"

or a function:

my_command() {
    ${ROOT_DIR}/bin/execute.py "$@"
}

but neither of them works. Do you know how to achieve this goal without writing a separate bash completion script?

EDIT

I think I found something that works. Notice that calling

complete | grep _python_argcomplete

in the bash shell gets completion functions for given Python scripts. In this output, I've found mine

complete -o bashdefault -o default -o nospace -F _python_argcomplete /absolute/path/to/root/bin/execute.py

thus I've edited my bash script this way

ROOT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

_my_command_autocomplete() {
    _python_argcomplete ${ROOT_DIR}/bin/execute.py
}

my_command() {
    ${ROOT_DIR}/bin/execute.py "$@"
}

eval "$(register-python-argcomplete ${ROOT_DIR}/bin/execute.py)"
complete -F _my_command_autocomplete my_command

and it worked

4
  • As this isn't really a programming question, maybe it would be a better fit for unix.stackexchange.com ? Commented Aug 15, 2024 at 13:46
  • Okay, and how can I move it there without creating a new question on unix.stackexhange? Commented Aug 15, 2024 at 14:12
  • I think that you have to create an account there, and just create a new question... Commented Aug 15, 2024 at 14:25
  • A question about programmable completion is about programming. Commented Aug 15, 2024 at 15:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.