Class: Main::Usage

Inherits:
Array show all
Defined in:
lib/main/usage.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Usage

Returns a new instance of Usage.



7
8
9
10
11
12
13
14
15
# File 'lib/main/usage.rb', line 7

def initialize opts={} 
  self.fields=[]
  self.chunkname = lambda{|chunkname| chunkname.to_s.strip.upcase}
  self.upcase = true 
  self.eos = "\n\n"
  if opts.has_key?(:upcase) or opts.has_key?('upcase')
    self.upcase = opts[:upcase] || opts['optcase']
  end
end

Class Method Details

.default_synopsis(main) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/main/usage.rb', line 25

def self.default_synopsis main
# build up synopsis
  s = "#{ main.name }"

# mode info
  if main.mode_name != 'main'
    s << " #{ main.fully_qualified_mode.join ' ' }"
  end

#p 'main.breadth_first_modes' => main.breadth_first_modes.map{|m| m.name}
#p 'main.depth_first_modes' => main.depth_first_modes.map{|m| m.name}
#p 'main.modes' => main.modes.map{|m| m.name}

  unless main.breadth_first_modes.empty?
    modes = main.breadth_first_modes.map{|mode| mode.name}.join('|')
    s << " (#{ modes })"
  end

# argument info
  main.parameters.each do |p|
    if p.type == :argument
      if(p.required? and p.arity != -1)
        if p.arity > 0
          p.arity.times{ s << " #{ p.name }" }
        else
          (p.arity.abs - 1).times{ s << " #{ p.name }" }
          s << " #{ p.name }*"
        end
      else
        #s << " [#{ p.name }]"
        if p.arity > 0
          a = []
          p.arity.times{ a << "#{ p.name }" }
          s << " [#{ a.join ' ' }]"
        else
          a = []
          (p.arity.abs - 1).times{ a << "#{ p.name }" }
          a << "#{ p.name }*"
          s << " [#{ a.join ' ' }]"
        end
      end
    end
  end

# keyword info
  main.parameters.each do |p|
    if p.type == :keyword
      if p.required?
        s << " #{ p.name }=#{ p.name }"
      else
        s << " [#{ p.name }=#{ p.name }]"
      end
    end
  end

# option info
  n = 0
  main.parameters.each do |p|
    if p.type == :option
      if p.required?
        case p.argument
          when :required
            s << " --#{ p.name }=#{ p.name }"
          when :optional
            s << " --#{ p.name }=[#{ p.name }]"
          else
            s << " --#{ p.name }"
        end
      else
        n += 1
      end
    end
  end
  if n > 0
    s << " [options]+"
  end

# help info
=begin
  if main.modes.size > 0
    modes = main.modes.keys.join('|')
    s << "\n#{ main.name } (#{ modes }) help"
  end
  if main.mode_name != 'main'
    s << "\n#{ main.name } #{ main.fully_qualified_mode.join ' ' } help"
  else
    s << "\n#{ main.name } help"
  end
=end

  s
end

.default_usage(main) ⇒ Object Also known as: default



173
174
175
176
177
178
179
180
181
# File 'lib/main/usage.rb', line 173

def default_usage main
  usage = new
  usage.main = main
# HACK
  %w( name synopsis description parameters author ).each do |key|
    usage[key] = nil
  end
  usage
end

Instance Method Details

#author_sectionObject



168
169
170
# File 'lib/main/usage.rb', line 168

def author_section
  main.author
end

#clearObject



17
18
19
20
21
# File 'lib/main/usage.rb', line 17

def clear
  super
ensure
  fields.clear
end

#description_sectionObject



130
131
132
# File 'lib/main/usage.rb', line 130

def description_section
  main.description if main.description?
end

#name_sectionObject



118
119
120
121
122
123
124
# File 'lib/main/usage.rb', line 118

def name_section
  if main.version?
    "#{ main.name } v#{ main.version }"
  else
    "#{ main.name }"
  end
end

#parameters_sectionObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/main/usage.rb', line 134

def parameters_section
  arguments = main.parameters.select{|p| p.type == :argument}
  keywords = main.parameters.select{|p| p.type == :keyword}
  options = main.parameters.select{|p| p.type == :option}
  environment = main.parameters.select{|p| p.type == :environment}

  help, nothelp = options.partition{|p| p.name == 'help'}
  options = nothelp + help

  parameters = arguments + keywords + options + environment

  s =
    parameters.map do |p|
      ps = ''
      ps << Util.columnize("#{ p.synopsis }", :indent => 2, :width => 78)
      #ps << Util.columnize("* #{ p.synopsis }", :indent => 2, :width => 78)
      #ps << "\n"
      if p.description?
        ps << "\n"
        ps << Util.columnize("#{ p.description }", :indent => 6, :width => 78)
        #ps << Util.columnize(p.description, :indent => 6, :width => 78)
        #ps << "\n"
      end
      #ps << "\n"
      unless(p.examples.nil? or p.examples.empty?)
        p.examples.each do |example|
          ps << "\n"
          ps << Util.columnize("#{ example }", :indent => 8, :width => 78)
        end
      end
      ps
    end.join("\n")
end

#set_defaults!Object



188
189
190
191
192
193
194
195
# File 'lib/main/usage.rb', line 188

def set_defaults!
  usage = self
  usage['name']        ||= name_section
  usage['synopsis']    ||= synopsis_section
  usage['description'] ||= description_section
  usage['parameters']  ||= parameters_section unless main.parameters.empty?
  usage['author']      ||= author_section if main.author?
end

#synopsis_sectionObject



126
127
128
# File 'lib/main/usage.rb', line 126

def synopsis_section
  main.synopsis
end

#to_sObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/main/usage.rb', line 197

def to_s
  set_defaults!
  s = '' 
  each_pair do |key, value|
    next unless(key and value)
    up, down = key.to_s.upcase, key.to_s.downcase
    if value
      s << (upcase ? up : down) << "\n" 
      s << Util.indent(Util.unindent(value.to_s), 2)
      s << eos 
    end
  end
  s
end