Class: Simple::CLI::Runner::CommandHelp

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/cli/runner/command_help.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, method_id) ⇒ CommandHelp

Returns a new instance of CommandHelp.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/simple/cli/runner/command_help.rb', line 14

def initialize(mod, method_id)
  raise(ArgumentError, "#{method_id.inspect} should be a Symbol") unless method_id.is_a?(Symbol)

  @method_id            = method_id
  @method               = mod.instance_method(@method_id)
  @method_parameters_ex = mod.method_parameters_ex(@method_id)
end

Class Method Details

.option_names(app, subcommand) ⇒ Object



8
9
10
11
12
# File 'lib/simple/cli/runner/command_help.rb', line 8

def self.option_names(app, subcommand)
  new(app, subcommand).option_names
rescue NameError
  []
end

Instance Method Details

#fullObject

Full help as read from the method comments



28
29
30
# File 'lib/simple/cli/runner/command_help.rb', line 28

def full
  comments.join("\n") if comments.first
end

#headObject

First line of the help as read from the method comments.



23
24
25
# File 'lib/simple/cli/runner/command_help.rb', line 23

def head
  comments.first
end

#interface(binary_name, command_name, include_subcommand: false) ⇒ Object

A help string constructed from the commands method signature.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/simple/cli/runner/command_help.rb', line 46

def interface(binary_name, command_name, include_subcommand: false)
  args = @method_parameters_ex.map do |mode, name|
    case mode
    when :req   then "<#{name}>"
    when :opt   then "[ <#{name}> ]"
    when :rest  then "[ <#{name}> .. ]"
    end
  end.compact

  options = @method_parameters_ex.map do |mode, name, default_value|
    case mode
    when :key     then
      case default_value
      when false  then  "[ --#{name} ]"
      when true   then  "[ --no-#{name} ]"
      when nil    then  "[ --#{name}=<#{name}> ]"
      else              "[ --#{name}=#{default_value} ]"
      end
    when :keyreq  then
      "--#{name}=<#{name}>"
    end
  end.compact

  help = "#{binary_name}"
  help << " #{command_to_string(command_name)}" if include_subcommand
  help << " #{options.join(' ')}" unless options.empty?
  help << " #{args.join(' ')}" unless args.empty?
  help
end

#option_namesObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple/cli/runner/command_help.rb', line 32

def option_names
  option_names = @method_parameters_ex.map do |mode, name, _|
    case mode
    when :key     then name
    when :keyreq  then name
    end
  end.compact

  option_names.map do |name|
    ["--#{name}", "--#{name}="]
  end.flatten
end