Skip to content

BUG: When using a Class for grouping commands --self shows as a flag #382

Open

Description

@chey

Example code (example.py):

import fire

class Foo:
    def bar(self):
        return 'bar'

    def do_stuff(self, arg1):
        return 'stuff'

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

Run:

python3 example.py -- --completion

Completion Output (take note of --self in output):

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

_complete-examplepy()
{
  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

    bar)

      if is_prev_global; then
        opts="${GLOBAL_OPTIONS}"
      else
        opts="--self ${GLOBAL_OPTIONS}"
      fi
      opts=$(filter_options $opts)
    ;;

    do-stuff)

      if is_prev_global; then
        opts="${GLOBAL_OPTIONS}"
      else
        opts="--arg1 --self ${GLOBAL_OPTIONS}"
      fi
      opts=$(filter_options $opts)
    ;;

    example.py)

      opts="bar do-stuff ${GLOBAL_OPTIONS}"
      opts=$(filter_options $opts)
    ;;
  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
}

complete -F _complete-examplepy example.py

Activity

  1. suryakant-source commented on Oct 15, 2026

    @suryakant-source

    I'd like to fix this. I reproduced it on master: completion._Commands(Foo) yields ('bar', '--self') and ('do-stuff', '--self'), because accessing an instance method on the class gives a plain function and its first argument is treated as a flag. My plan is to skip the first argument of instance methods (not static or class methods) when generating completions, with a test. I'll open a PR shortly.

    Disclosure: I use an AI coding assistant for parts of my contributions, and I check the change myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions