Class: EacCli::Runner::DocoptDoc

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_cli/runner/docopt_doc.rb

Constant Summary collapse

SEP =
' '
IDENT =
SEP * 2
OPTION_DESC_SEP =
IDENT * 2

Instance Method Summary collapse

Instance Method Details

#option_argument(option) ⇒ Object



21
22
23
# File 'lib/eac_cli/runner/docopt_doc.rb', line 21

def option_argument(option)
  option_long(option)
end

#option_definition(option) ⇒ Object



25
26
27
# File 'lib/eac_cli/runner/docopt_doc.rb', line 25

def option_definition(option)
  option.short + SEP + option_long(option) + OPTION_DESC_SEP + option.description
end

#option_long(option) ⇒ Object



29
30
31
32
33
# File 'lib/eac_cli/runner/docopt_doc.rb', line 29

def option_long(option)
  b = option.long
  b += '=<value>' if option.argument?
  b
end

#positional_argument(positional) ⇒ Object



15
16
17
18
19
# File 'lib/eac_cli/runner/docopt_doc.rb', line 15

def positional_argument(positional)
  r = "<#{positional.name}>"
  r += '...' if positional.repeat?
  r
end

#section(header, include_header = true) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/eac_cli/runner/docopt_doc.rb', line 35

def section(header, include_header = true)
  b = include_header ? "#{header.humanize}:\n" : ''
  b += send("self_#{header}") + "\n"
  definition.alternatives.each do |alternative|
    b += self.class.new(alternative).section(header, false)
  end
  b
end

#self_optionsObject



44
45
46
# File 'lib/eac_cli/runner/docopt_doc.rb', line 44

def self_options
  definition.options.map { |option| IDENT + option_definition(option) }.join("\n")
end

#self_usageObject



48
49
50
51
52
# File 'lib/eac_cli/runner/docopt_doc.rb', line 48

def self_usage
  b = IDENT + ::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO
  b += "#{SEP}[options]" if definition.options_argument
  b + self_usage_arguments
end

#self_usage_argumentsObject



54
55
56
57
58
# File 'lib/eac_cli/runner/docopt_doc.rb', line 54

def self_usage_arguments
  definition.options.select(&:show_on_usage?)
            .map { |option| "#{SEP}#{option_argument(option)}" }.join +
    definition.positional.map { |p| "#{SEP}#{positional_argument(p)}" }.join
end

#to_sObject



60
61
62
# File 'lib/eac_cli/runner/docopt_doc.rb', line 60

def to_s
  "#{definition.description}\n\n#{section('usage')}\n#{section('options')}\n"
end