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



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

def option_argument(option)
  option_long(option)
end

#option_definition(option) ⇒ Object



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

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

#option_long(option) ⇒ Object



34
35
36
37
38
# File 'lib/eac_cli/runner/docopt_doc.rb', line 34

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

#positional_argument(positional) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/eac_cli/runner/docopt_doc.rb', line 15

def positional_argument(positional)
  if positional.subcommand?
    ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
  else
    r = "<#{positional.name}>"
    r += '...' if positional.repeat?
    r = "[#{r}]" if positional.optional?
    r
  end
end

#section(header, include_header = true) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/eac_cli/runner/docopt_doc.rb', line 40

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



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

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

#self_usageObject



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

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



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

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



65
66
67
# File 'lib/eac_cli/runner/docopt_doc.rb', line 65

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