code: def _BashScript(name, commands, default_options=None):
      """Returns a Bash script registering a completion function for the commands.
  
      Args:
        name: The first token in the commands, also the name of the command.
        commands: A list of all possible commands that tab completion can complete
            to. Each command is a list or tuple of the string tokens that make up
            that command.
        default_options: A dict of options that can be used with any command. Use
            this if there are flags that can always be appended to a command.
      Returns:
        A string which is the Bash script. Source the bash script to enable tab
        completion in Bash.
      """
      default_options = default_options or set()
      global_options, options_map, subcommands_map = _GetMaps(
          name, commands, default_options
      )
  
      bash_completion_template = """# bash completion support for {name}
  # DO NOT EDIT.
  # This script is autogenerated by fire/completion.py.
  
  _complete-{identifier}()
  {{
    local cur prev opts lastcommand
    COMPREPLY=()
    prev="${{COMP_WORDS[COMP_CWORD-1]}}"
    cur="${{COMP_WORDS[COMP_CWORD]}}"
    lastcommand=$(get_lastcommand)
  
    opts="{default_options}"
    GLOBAL_OPTIONS="{global_options}"
  
  {checks}
  
    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-{identifier} {command}
  """
  
      check_wrapper = """
    case "${{lastcommand}}" in
    {lastcommand_checks}
    esac"""
  
      lastcommand_check_template = """
      {command})
        {opts_assignment}
        opts=$(filter_options $opts)
      ;;"""
  
      opts_assignment_subcommand_template = """
        if is_prev_global; then
          opts="${{GLOBAL_OPTIONS}}"
        else
          opts="{options} ${{GLOBAL_OPTIONS}}"
        fi"""
  
      opts_assignment_main_command_template = """
        opts="{options} ${{GLOBAL_OPTIONS}}" """
  
      def _GetOptsAssignmentTemplate(command):
          if command == name:
              return opts_assignment_main_command_template
          else:
              return opts_assignment_subcommand_template
  
      lines = []
      commands_set = set()
      commands_set.add(name)
      commands_set = commands_set.union(set(subcommands_map.keys()))
      commands_set = commands_set.union(set(options_map.keys()))
      for command in commands_set:
          opts_assignment = _GetOptsAssignmentTemplate(command).format(
              options=" ".join(
                  sorted(
                      options_map[command].union(
                          subcommands_map[command]
                      )
                  )
              ),
          )
          lines.append(
              lastcommand_check_template.format(
                  command=command, opts_assignment=opts_assignment
              )
          )
      lastcommand_checks = "\n".join(lines)
  
      checks = check_wrapper.format(
          lastcommand_checks=lastcommand_checks,
      )
  
      return bash_completion_template.format(
          name=name,
          command=name,
          checks=checks,
          default_options=" ".join(default_options),
          identifier=name.replace("/", "")
          .replace(".", "")
          .replace(",", ""),
          global_options=" ".join(global_options),
      )
  
file_path: python-fire/fire/completion.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import copy
  import inspect
  from fire import inspectutils
  import six
label_desc: Use image node_id 2 for calling a global function with example usage: _BashScript(name, commands, default_options) and returns: bash_completion_template, opts_assignment_main_command_template, opts_assignment_subcommand_template