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.



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

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.



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

def args
  @args
end

#optsObject (readonly, protected)

Returns the value of attribute opts.



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

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)



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

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)



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

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

#arguments(cmd) ⇒ Object (protected)



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

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(command = nil) ⇒ Object (protected)



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

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

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



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

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)



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

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)



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

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)



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

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(
               __dir__, '../../../..', 'templates/completion/bash.erb'
             )).read,
    trim_mode: '-'
  ).result(binding)
end

#global_commandsObject (protected)



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

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)



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

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)



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

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