Module: Thor::Completion::Bash

Defined in:
lib/thor/completion/bash.rb,
lib/thor/completion/bash/subcmd.rb,
lib/thor/completion/bash/request.rb,
lib/thor/completion/bash/thor_mixin.rb,
lib/thor/completion/bash/command_mixin.rb,
lib/thor/completion/bash/argument_mixin.rb

Overview

Experimental support for Bash completions.

To enable, require this module and add the following to your entry-point Thor subclass:

include Thor::Completion::Bash

You should now have a ‘bash-complete` subcommand present. You can test this out with

YOUR_EXE bash-complete help

where ‘YOUR_EXE` is replaced with your executable name.

You should see output like

Commands:
  locd bash-complete complete -- CUR PREV CWORD SPLIT WORDS...  # (...)
  locd bash-complete help [COMMAND]                             # (...)
  locd bash-complete setup                                      # (...)

Then, to hook your executable into Bash’s ‘compelte` builtin:

source <(YOUR_EXE bash-complete setup)

where, again, ‘YOUR_EXE` is replaced with your executable name.

Defined Under Namespace

Modules: ArgumentMixin, CommandMixin, ThorMixin Classes: Request, Subcmd

Class Method Summary collapse

Class Method Details

.included(base) ⇒ nil

Hook to setup Bash complete on including Thor subclass.

  1. Mixes ThorMixin into Thor.

  2. Mixes CommandMixin into Thor::Command.

  3. Creates a new subclass of Subcmd boun0d to ‘base` and adds that as `bash-complete` to `base` via Thor.subcommand.

Parameters:

  • base (Class<Thor>)

    The class that inluded Thor::Completion::Bash. Should be a Thor subclass and be the main/entry command of the program, though neither of these are enforced.

Returns:

  • (nil)

    Totally side-effect based.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/thor/completion/bash.rb', line 70

def self.included base
  
  unless Thor.include? ThorMixin
    Thor.send :include, ThorMixin
  end

  unless Thor::Command.include? CommandMixin
    Thor::Command.send :include, CommandMixin
  end

  unless Thor::Argument.include? ArgumentMixin
    Thor::Argument.send :include, ArgumentMixin
  end

  subcmd_class = Class.new Subcmd do
    def self.target
      @target
    end
  end

  subcmd_class.instance_variable_set :@target, base
  
  # Install {Subcmd} as a subcommand
  base.send :subcommand,
            'bash-complete',
            subcmd_class,
    desc:   "Support for Bash command completion."

  nil
end