Module: Morpheus::Cli::CliCommand::ClassMethods

Defined in:
lib/morpheus/cli/cli_command.rb

Instance Method Summary collapse

Instance Method Details

#add_subcommand(cmd_name, method) ⇒ Object



365
366
367
368
# File 'lib/morpheus/cli/cli_command.rb', line 365

def add_subcommand(cmd_name, method)
  @subcommands ||= {}
  @subcommands[cmd_name.to_s] = method
end

#add_subcommand_alias(alias_cmd_name, cmd_name) ⇒ Object



385
386
387
388
# File 'lib/morpheus/cli/cli_command.rb', line 385

def add_subcommand_alias(alias_cmd_name, cmd_name)
  @subcommand_aliases ||= {}
  @subcommand_aliases[alias_cmd_name.to_s] = cmd_name
end

#alias_subcommand(alias_cmd_name, cmd_name) ⇒ Object

register an alias for a command



376
377
378
379
# File 'lib/morpheus/cli/cli_command.rb', line 376

def alias_subcommand(alias_cmd_name, cmd_name)
  add_subcommand_alias(alias_cmd_name.to_s, cmd_name.to_s.gsub('_', '-'))
  return
end

#command_name ⇒ Object



312
313
314
315
# File 'lib/morpheus/cli/cli_command.rb', line 312

def command_name
  @command_name ||= default_command_name
  @command_name
end

#default_command_name ⇒ Object



308
309
310
# File 'lib/morpheus/cli/cli_command.rb', line 308

def default_command_name
  Morpheus::Cli::CliRegistry.cli_ize(self.name.split('::')[-1])
end

#default_subcommand ⇒ Object



352
353
354
# File 'lib/morpheus/cli/cli_command.rb', line 352

def default_subcommand
  @default_subcommand
end

#has_subcommand?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


360
361
362
363
# File 'lib/morpheus/cli/cli_command.rb', line 360

def has_subcommand?(cmd_name)
  return false if cmd_name.empty?
  @subcommands && @subcommands[cmd_name.to_s]
end

#hidden_command ⇒ Object



321
322
323
# File 'lib/morpheus/cli/cli_command.rb', line 321

def hidden_command
  !!@hidden_command
end

#register_subcommands(*cmds) ⇒ Object

construct map of command name => instance method



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/morpheus/cli/cli_command.rb', line 326

def register_subcommands(*cmds)
  @subcommands ||= {}
  cmds.flatten.each {|cmd| 
    if cmd.is_a?(Hash)
      cmd.each {|k,v| 
        # @subcommands[k] = v
        add_subcommand(k.to_s, v.to_s)
      }
    elsif cmd.is_a?(Array) 
      cmd.each {|it| register_subcommands(it) }
    elsif cmd.is_a?(String) || cmd.is_a?(Symbol)
      #k = Morpheus::Cli::CliRegistry.cli_ize(cmd)
      k = cmd.to_s.gsub('_', '-')
      v = cmd.to_s.gsub('-', '_')
      register_subcommands({(k) => v})
    else
      raise "Unable to register command of type: #{cmd.class} #{cmd}"
    end
  }
  return
end

#remove_subcommand(cmd_name) ⇒ Object



370
371
372
373
# File 'lib/morpheus/cli/cli_command.rb', line 370

def remove_subcommand(cmd_name)
  @subcommands ||= {}
  @subcommands.delete(cmd_name.to_s)
end

#remove_subcommand_alias(alias_cmd_name) ⇒ Object



390
391
392
393
# File 'lib/morpheus/cli/cli_command.rb', line 390

def remove_subcommand_alias(alias_cmd_name)
  @subcommand_aliases ||= {}
  @subcommand_aliases.delete(alias_cmd_name.to_s)
end

#set_command_hidden(val = true) ⇒ Object



317
318
319
# File 'lib/morpheus/cli/cli_command.rb', line 317

def set_command_hidden(val=true)
  @hidden_command = val
end

#set_command_name(cmd_name) ⇒ Object



303
304
305
306
# File 'lib/morpheus/cli/cli_command.rb', line 303

def set_command_name(cmd_name)
  @command_name = cmd_name
  Morpheus::Cli::CliRegistry.add(self, self.command_name)
end

#set_default_subcommand(cmd) ⇒ Object



348
349
350
# File 'lib/morpheus/cli/cli_command.rb', line 348

def set_default_subcommand(cmd)
  @default_subcommand = cmd
end

#subcommand_aliases ⇒ Object



381
382
383
# File 'lib/morpheus/cli/cli_command.rb', line 381

def subcommand_aliases
  @subcommand_aliases ||= {}
end

#subcommands ⇒ Object



356
357
358
# File 'lib/morpheus/cli/cli_command.rb', line 356

def subcommands
  @subcommands ||= {}
end