Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix completion for Fire(fn)#336

Merged
dbieber merged 1 commit into
google:mastergoogle/python-fire:masterfrom
wchliao:fix-function-completionwchliao/python-fire:fix-function-completionCopy head branch name to clipboard
Dec 9, 2022
Merged

Fix completion for Fire(fn)#336
dbieber merged 1 commit into
google:mastergoogle/python-fire:masterfrom
wchliao:fix-function-completionwchliao/python-fire:fix-function-completionCopy head branch name to clipboard

Conversation

@wchliao

@wchliao wchliao commented Apr 26, 2021

Copy link
Copy Markdown
Contributor

This is a bug that should have been fixed long time ago. (#14)
However, it seems that the bug appeared again.

The issue is that completion for Fire(fn) is missing.
Take the following program as an example.

import fire

def hello(name):
  return 'Hello {name}!'.format(name=name)

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

When we type example.py the command should automatically complete --name, but the completion script generated by current Fire fails to do so.
The generated script is as follow.

# 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="--name"


  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
}

complete -F _complete-examplepy example.py

After the change, now completion for Fine(fn) works.
The generated script is as follow.

# 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="--name"


  case "${lastcommand}" in
  
    example.py)
      
      opts=" ${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

@google-cla google-cla Bot added the cla: yes Author has signed CLA label Apr 26, 2021
@dbieber

dbieber commented Dec 9, 2022

Copy link
Copy Markdown
Collaborator

Thanks for the fix.

We may need an analogous fix for the Fish script generator too.

@dbieber dbieber merged commit b2415b9 into google:master Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Author has signed CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.