Class: OsCtl::Lib::Cli::Completion::Bash

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/cli/completion/bash.rb

Defined Under Namespace

Classes: OptArg

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Bash

Returns a new instance of Bash

Parameters:

  • app (GLI::App)


44
45
46
47
48
49
50
# File 'lib/libosctl/cli/completion/bash.rb', line 44

def initialize(app)
  @app = app
  @opts = []
  @args = []
  @shortcuts = []
  @app_prefix = "_#{app.exe_name.gsub('-', '_')}"
end

Instance Attribute Details

#appObject (readonly, protected)

Returns the value of attribute app



78
79
80
# File 'lib/libosctl/cli/completion/bash.rb', line 78

def app
  @app
end

#app_prefixString (readonly)

Returns:

  • (String)


41
42
43
# File 'lib/libosctl/cli/completion/bash.rb', line 41

def app_prefix
  @app_prefix
end

#argsObject (readonly, protected)

Returns the value of attribute args



78
79
80
# File 'lib/libosctl/cli/completion/bash.rb', line 78

def args
  @args
end

#optsObject (readonly, protected)

Returns the value of attribute opts



78
79
80
# File 'lib/libosctl/cli/completion/bash.rb', line 78

def opts
  @opts
end

#shortcutsArray<String>

Returns shortcuts to subcommands

Returns:

  • (Array<String>)

    shortcuts to subcommands



38
39
40
# File 'lib/libosctl/cli/completion/bash.rb', line 38

def shortcuts
  @shortcuts
end

Instance Method Details

#app_exeObject (protected)



199
200
201
# File 'lib/libosctl/cli/completion/bash.rb', line 199

def app_exe
  app.exe_name
end

#arg(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :cmd (Symbol, Array<Symbol>)
  • :name (Symbol)
  • :expand (String)


64
65
66
# File 'lib/libosctl/cli/completion/bash.rb', line 64

def arg(opts)
  @args << OptArg.new(opts)
end

#arg_word_list(cmd, path, arg) ⇒ Object (protected)



126
127
128
129
# File 'lib/libosctl/cli/completion/bash.rb', line 126

def arg_word_list(cmd, path, arg)
  optarg = args.detect { |v| v.applicable?(path, arg) }
  optarg ? optarg.expand : ''
end

#arguments(cmd) ⇒ Object (protected)



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/libosctl/cli/completion/bash.rb', line 185

def arguments(cmd)
  ret = []
  return ret unless cmd.respond_to?(:arguments_description)

  cmd.arguments_description.split(' ').each do |arg|
    if (arg.start_with?('<') && arg.end_with?('>')) \
       || (arg.start_with?('[') && arg.end_with?(']'))
      ret << arg[1..-2].gsub('|', '')
    end
  end

  ret
end

#commands(cmd = nil) ⇒ Object (protected)



80
81
82
83
84
# File 'lib/libosctl/cli/completion/bash.rb', line 80

def commands(cmd = nil)
  (cmd || app).commands.reject do |name, cmd|
    cmd.description.nil? || name.to_s.start_with?('_')
  end
end

#each_command(parent: nil, path: [], &block) ⇒ Object (protected)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/libosctl/cli/completion/bash.rb', line 97

def each_command(parent: nil, path: [], &block)
  if parent.nil?
    yield(app, [app.exe_name])
    each_command(parent: app, path: [app.exe_name], &block)

  else
    parent.commands.each_value do |c|
      ([c.name] + (c.aliases || [])).each do |name|
        name_s = name.to_s
        block.call(c, path + [name_s])
        each_command(parent: c, path: path + [name_s], &block)
      end
    end
  end
end

#each_flag(cmd = nil) ⇒ Object (protected)



171
172
173
174
175
176
177
178
179
# File 'lib/libosctl/cli/completion/bash.rb', line 171

def each_flag(cmd = nil)
  # Flags with arguments
  (cmd || app).flags.each do |name, fl|
    fl.all_forms(', ').split(', ').each do |form|
      opt, arg = form.split('=')
      yield(name, fmt_opt(opt), arg)
    end
  end
end

#flags(cmd = nil) ⇒ Object (protected)



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/libosctl/cli/completion/bash.rb', line 157

def flags(cmd = nil)
  ret = []

  # Flags with arguments
  (cmd || app).flags.each_value do |fl|
    fl.all_forms(', ').split(', ').each do |form|
      opt, _ = form.split('=')
      ret << opt
    end
  end

  ret
end

#fmt_opt(v) ⇒ Object (protected)



181
182
183
# File 'lib/libosctl/cli/completion/bash.rb', line 181

def fmt_opt(v)
  v.gsub(/-/, '_')
end

#generateObject



68
69
70
71
72
73
74
75
# File 'lib/libosctl/cli/completion/bash.rb', line 68

def generate
  ERB.new(
    File.new(File.join(
      File.dirname(__FILE__), '../../../..', 'templates/completion/bash.erb'
    )).read,
    0, '-'
  ).result(binding)
end

#global_commandsObject (protected)



86
87
88
89
90
91
92
93
94
95
# File 'lib/libosctl/cli/completion/bash.rb', line 86

def global_commands
  ret = []

  app.commands.each_value do |c|
    ret << c.name
    ret.concat(c.aliases) if c.aliases
  end

  ret
end

#opt(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :cmd (Symbol, Array<Symbol>)
  • :name (Symbol)
  • :expand (String)


56
57
58
# File 'lib/libosctl/cli/completion/bash.rb', line 56

def opt(opts)
  @opts << OptArg.new(opts)
end

#opt_word_list(cmd, path, name, opt, arg_name) ⇒ Object (protected)



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/libosctl/cli/completion/bash.rb', line 113

def opt_word_list(cmd, path, name, opt, arg_name)
  optarg = opts.detect { |v| v.applicable?(path, arg_name) }

  if optarg
    optarg.expand

  elsif cmd.flags[name].must_match
    "echo #{cmd.flags[name].must_match.join(' ')}"
  else
    ''
  end
end

#options(cmd = nil) ⇒ Object (protected)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/libosctl/cli/completion/bash.rb', line 131

def options(cmd = nil)
  ret = []

  # Boolean switches
  (cmd || app).switches.each_value do |sw|
    sw.arguments_for_option_parser.each do |arg|
      if arg.include?('[no-]')
        ret << arg.sub('[no-]', '')
        ret << arg.sub('[no-]', 'no-')
      else
        ret << arg
      end
    end
  end

  # Flags with arguments
  (cmd || app).flags.each_value do |fl|
    fl.all_forms(', ').split(', ').each do |form|
      opt, _ = form.split('=')
      ret << opt
    end
  end

  ret
end