Class: SubCmdOptParser::OptionParserForSubCmd

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/subcommand_optparse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subcmd, description, width, indent) ⇒ OptionParserForSubCmd

Returns a new instance of OptionParserForSubCmd.



13
14
15
16
17
18
# File 'lib/subcommand_optparse.rb', line 13

def initialize(subcmd, description, width, indent)
  @subcommand_name = subcmd
  @description = description
  @summary = nil
  super(nil, width, indent)
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/subcommand_optparse.rb', line 8

def description
  @description
end

#example=(value) ⇒ Object (writeonly)

Sets the attribute example

Parameters:

  • value

    the value to set the attribute example to.



11
12
13
# File 'lib/subcommand_optparse.rb', line 11

def example=(value)
  @example = value
end

#subcommand_nameObject

Returns the value of attribute subcommand_name.



7
8
9
# File 'lib/subcommand_optparse.rb', line 7

def subcommand_name
  @subcommand_name
end

#summaryObject

Returns the value of attribute summary.



9
10
11
# File 'lib/subcommand_optparse.rb', line 9

def summary
  @summary
end

#usageObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/subcommand_optparse.rb', line 20

def usage
  usage_str = "Usage: "
  if @usage
    @usage.each_line.with_index do |line, ind|
      usage_str << "       " if ind > 0
      usage_str << line.sub(/\n?\z/, "\n")
    end
  else
    usage_str << "#{program_name} #{@subcommand_name || '<command>'} [options]\n"
  end
  usage_str
end

Instance Method Details



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/subcommand_optparse.rb', line 33

def banner
  unless @banner
    @banner = usage
    if @summary
      @banner << "\n#{@summary.sub(/\n?\z/, "\n")}"
    elsif @description
      @banner << "\n#{@description.sub(/\n?\z/, "\n")}"
    end
    if @example
      if @example.each_line.count == 1
        @banner << "\nExample: #{@example.strip}"
      else
        @banner << "\nExamples:\n"
        @example.each_line do |line|
          @banner << "    #{line.sub(/\n?\z/, "\n")}"
        end
      end
    end
  end
  @banner
end

#helpObject Also known as: to_s



55
56
57
58
59
60
61
62
# File 'lib/subcommand_optparse.rb', line 55

def help
  str_banner = "#{banner}".sub(/\n?\z/, "\n")
  str_summary =  summarize("")
  if str_summary.size > 0
    str_banner << "\nOptions:\n"
  end
  str_banner + str_summary
end