python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Completion does not work with nested commands with 'group flags'

Open jelleklaver opened this issue 4 years ago • 1 comments

I'm trying to create a CLI which consists of several command groups. Commands within the command group share some common flags. In my example that is e.g. the --namespace flag in the sub command group.

I got this working. However, it breaks some fire functionality like generating command completion and a proper --help output.

Note that I use the class object in stead of class instances for the command groups (in contrast to the documentation):

import fire

class Sub(object):
    """Command group which has group-flags
    """

    def __init__(self, namespace='default'):
        self._namespace = namespace

    def command(self):
        """Run as `mycli sub command`"""
        print(f"Now I return {self._namespace}")

class MyCLI(object):
    """My super fancy CLI
    """
    def __init__(self, verbose=False):
        # It just references the class, not an instance as shown in the docs
        self.sub = Sub
        # ...

if __name__ == '__main__':
    fire.Fire(MyCLI, name='mycli')

Running a command works properly (you can even rearrange the flag):

$ mycli sub command                                                                                                                                                         
Now I return default

$ mycli sub command --namespace test                                                                                                                                         
Now I return test

However, when running mycli -- --completion it will just print a completion script without any actual command completions.

It seems that my use-case is either not supported, or I have implemented it quite wrong. I'm quite new to fire and python cli tools, so I suppose the latter could definitely be the case ;-).

jelleklaver avatar May 06 '20 18:05 jelleklaver

I've hit a similar issue with groups:

#!/usr/bin/env python3

import fire

class Sub1(object):
    def command1(self, param1):
        print('command {}'.format(param1))

class Sub2(object):
    def command2(self, param2):
        print('command {}'.format(param2))

class CLI(object):

  def __init__(self):
    self.group1 = Sub1()
    self.group2 = Sub2()

if __name__ == '__main__':
    fire.Fire(CLI)

the generated autocompletion script looks like just a skeleton without my commands:

# bash completion support for cli2
# DO NOT EDIT.
# This script is autogenerated by fire/completion.py.

_complete-cli2()
{
  local cur prev opts lastcommand
  COMPREPLY=()
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  cur="${COMP_WORDS[COMP_CWORD]}"
  lastcommand=$(get_lastcommand)

  opts=""
  GLOBAL_OPTIONS=""


  case "${lastcommand}" in
  
  esac

  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  return 0
}

get_lastcommand()
{
  local lastcommand i

  lastcommand=
  for ((i=0; i < ${#COMP_WORDS[@]}; ++i)); do
    if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[
      ${COMP_WORDS[i]} != $cur ]]; then
      lastcommand=${COMP_WORDS[i]}
    fi
  done

  echo $lastcommand
}

filter_options()
{
  local opts
  opts=""
  for opt in "$@"
  do
    if ! option_already_entered $opt; then
      opts="$opts $opt"
    fi
  done

  echo $opts
}

option_already_entered()
{
  local opt
  for opt in ${COMP_WORDS[@]:0:COMP_CWORD}
  do
    if [ $1 == $opt ]; then
      return 0
    fi
  done
  return 1
}

is_prev_global()
{
  local opt
  for opt in $GLOBAL_OPTIONS
  do
    if [ $opt == $prev ]; then
      return 0
    fi
  done
  return 1
}

snowch avatar May 09 '20 15:05 snowch