1

For example, I have a python script "control_center.py" which takes the $1 (start stop reboot) as command, and extra options (-a foo/bar).

control_center.py [start|stop|reboot] -a [foo|bar]

I have used argcomplete, it works fine with the options.

cotrol_center.py start -a <tap> <tap>
bar foo

However, the argcomplete can not auto fills the command (start|stop|reboot) for me.

I've tried to edit one bash complete script my self, it does can gives me the hint on the first argument, but it disabled the argcomplete at the same time.

control_center.py <tap> <tap>
start stop reboot
control_center.py start -a <tap> <tap>
# nothing pops out

What I want is something like below:

control_center.py <tap> <tap>
start stop reboot
control_center.py start -a <tap> <tap>
bar foo

Btw, here is my auto completion bash script.

#!/usr/bin/env bash
_control_center_completions()
{
  if [ "${#COMP_WORDS[@]}" != "2" ]; then
    return
  fi

  COMPREPLY=($(compgen -W "start stop reboot" -- "${COMP_WORDS[1]}"))
}

complete -F _control_center_completions control_center.py

I wonder if I can do some modification on that script so the argcomplete won't be disabled.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Mingo Pan
  • 37
  • 5
  • its not clear what your question is. what do you mean by "back to normal"? and "make both of them work"? both of who? – pynexj Jan 15 '21 at 10:07
  • Argcomplete works fine with the options but it cannot do the auto complete on non-optional argument ($1), my own bash complete script works fine with the $1, but it disabled the argcomplete. my question is if there any modification that need to be made on the bash complete script to make them compatible. like `contorl_center.py `, it pops out the `start stop reboot`, `control_center.py play -a ` the option `foo` pops out. – Mingo Pan Jan 16 '21 at 06:26
  • still confused. could you give a [repro] to help clarify? – pynexj Jan 16 '21 at 07:25
  • Hi, pynexj, thanks for the reply! I've re-edit the question. – Mingo Pan Jan 18 '21 at 03:41
  • i know nothing about argcomplete so cannot reproduce your problem. let's wait for someone who knows both bash-complete and argcomplete. – pynexj Jan 18 '21 at 03:52

1 Answers1

0

I think the documentation makes this hard to grasp but here is how I go about this which might make this easy to understand.

Usually you see cur and prev in examples they denote the current and previous word being typed.

In the script below I think its obvious that the last COMPREPLY takes care of completing the start|stop|reboot positional arguments.

I add [[ ${prev:0:1} == '-' ]] && LAST_OPT=${prev} which sets $LAST_OPT to the last word if it starts with a dash. Why is it the last word and not the current? Because you pressed space to complete your option. I.e -a<space>. That will work for --thislongopt or -a. Now you write a simple if to catch which options you want to build COMPREPLY vars for. I hard coded "a1 a2 a3" but you could have "$(ls /etc/systemd/)"

Hope that clears things up a bit.

here is the working pattern.

_control_center_completions()
{
    local cur prev LAST_OPT
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    
    
    [[ ${prev:0:1} == '-' ]] && LAST_OPT=${prev}
    
    if [ "${LAST_OPT}" == "-a" ]; then
        COMPREPLY=( $(compgen  -W "a1 a2 a3" -- ${cur}) )
        return 0
    fi

    COMPREPLY=($(compgen -W "start stop reboot" -- "${cur}"))
}

complete -F _control_center_completions control_center
Peter Moore
  • 1,632
  • 1
  • 17
  • 31