Class: OptparseLite::Command

Inherits:
Object
  • Object
show all
Includes:
HelpHelper, Lingual
Defined in:
lib/optparse-lite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelpHelper

#cmd, #hdr, #help_requested?, #looks_like_header?, #prefix, #txt

Methods included from Lingual

#methodize, #oxford_comma

Constructor Details

#initialize(spec, method_name, desc = nil, opts = nil, usage = nil, subcommands = nil) ⇒ Command

Returns a new instance of Command.



130
131
132
133
134
135
136
137
138
139
# File 'lib/optparse-lite.rb', line 130

def initialize spec, method_name, desc=nil, opts=nil, usage=nil,
    subcommands=nil
  @spec = spec
  @method_name = method_name
  @desc = DescriptionAndOpts.new(desc || [])
  @opt_indexes = opts || []
  @subcommand_names = subcommands
  @syntax_sexp = nil
  @usage = usage || []
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



141
142
143
# File 'lib/optparse-lite.rb', line 141

def desc
  @desc
end

#given_nameObject

used only for subcommands



140
141
142
# File 'lib/optparse-lite.rb', line 140

def given_name
  @given_name
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



141
142
143
# File 'lib/optparse-lite.rb', line 141

def method_name
  @method_name
end

#parentObject

used only for subcommands



140
141
142
# File 'lib/optparse-lite.rb', line 140

def parent
  @parent
end

#specObject (readonly)

Returns the value of attribute spec.



141
142
143
# File 'lib/optparse-lite.rb', line 141

def spec
  @spec
end

#usageObject (readonly)

Returns the value of attribute usage.



141
142
143
# File 'lib/optparse-lite.rb', line 141

def usage
  @usage
end

Instance Method Details

#desc_onelineObject



142
143
144
# File 'lib/optparse-lite.rb', line 142

def desc_oneline
  desc.any? ? desc.first_desc_line : nil
end

#doc_sexpObject



145
146
147
# File 'lib/optparse-lite.rb', line 145

def doc_sexp
  common_doc_sexp @desc, :bdy
end

#optsObject



148
149
150
# File 'lib/optparse-lite.rb', line 148

def opts
  @opt_indexes.map{|x| @desc[x]}
end

#prettyObject



184
185
186
# File 'lib/optparse-lite.rb', line 184

def pretty
  given_name ? given_name.to_s : method_name.gsub(/_/,'-')
end

#pretty_fullObject



187
188
189
# File 'lib/optparse-lite.rb', line 187

def pretty_full
  parent ? "#{parent.pretty_full} #{pretty}" : pretty
end

#process_opt_parse_errors(resp, opts = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/optparse-lite.rb', line 151

def process_opt_parse_errors resp, opts={}
  opts = {opts=>true} if opts.kind_of?(Symbol)
  return help_requested(resp) if ! resp.detect do |x|
    ! x.respond_to?(:error_type) || x.error_type != :help_requested
  end
  ui = @disp.ui.err
  ui.puts "#{prefix}couldn't #{cmd(pretty)} because of "<<
    Np.new(proc{|b| b ? 'the following' : 'an'},'error',resp.size)
  ui.puts resp
  @disp.help.command_usage self, ui if opts[:show_usage]
  @disp.help.invite_to_more_command_help_specific self, ui
  return -1
end

#run(disp, argv) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/optparse-lite.rb', line 164

def run disp, argv
  @disp = disp
  opts = nil
  if parser = get_parser
    resp, opts = parser.parse(argv)
    return process_opt_parse_errors(resp, :show_usage) if resp.errors.any?
  end
  argv.unshift(opts) if opts
  resp = nil
  begin
    resp = disp.impl.send(method_name, *argv)
  rescue ArgumentError => e
    if one_of_ours(e)
      return process_opt_parse_errors [e.message], :show_usage
    else
      raise e
    end
  end
  resp
end

#subcommandsObject



190
191
192
# File 'lib/optparse-lite.rb', line 190

def subcommands
  @subcommands ||= SubCommands.new(self, @subcommand_names)
end

#syntax_sexpObject



193
194
195
196
197
198
199
# File 'lib/optparse-lite.rb', line 193

def syntax_sexp
  return @syntax_sexp unless @syntax_sexp.nil?
  # no support for union grammars yet (or evar!) (like git-branch).
  usage = @usage.any? ? @usage.join(' ') : default_usage
  md = (/\A(\[<opts>\] *)?(.*)\Z/m).match(usage) # matches all strings
  @syntax_sexp = [cmds_sexp, opts_sexp(md[1]), args_sexp(md[2])].compact
end