Parent
- Object
Information
Executioner
class MyCLI < Executioner
# cmd --debug
def debug?
$DEBUG
end
def debug=(bool)
$DEBUG = bool
end
# $ foo remote
class Remote < Executioner
# $ foo remote --verbose
def verbose?
@verbose
end
def verbose=(bool)
@verbose = bool
end
# $ foo remote --force
def force?
@force
end
def remote=(bool)
@force = bool
end
# $ foo remote --output <path>
def output=(path)
@path = path
end
# $ foo remote -o <path>
alias_method :o=, :output=
# $ foo remote add
class Add < self
def main(name, branch)
# ...
end
end
# $ foo remote show
class Show < self
def main(name)
# ...
end
end
end
end
Public Class Methods
attr_switch(name)
click to toggle source
Helper method for creating switch attributes.
def name=(val)
@name = val
end
def name?
@name
end
# File lib/executioner.rb, line 104
104: def attr_switch(name)
105: attr_writer name
106: module_eval %{
107: def #{name}?
108: @#{name}
109: end
110: }
111: end
descriptions()
click to toggle source
Hash for storing descriptions.
# File lib/executioner.rb, line 309
309: def descriptions
310: @descriptions ||= (
311: parent = ancestors[1]
312: if Executioner > parent
313: parent.descriptions.dup
314: else
315: {}
316: end
317: )
318: end
execute(argv=ARGV)
click to toggle source
# File lib/executioner.rb, line 114
114: def execute(argv=ARGV)
115: argv = parse_arguments(argv)
116:
117: cmd, argv = parse_subcommand(argv)
118: cli = cmd.new
119: args = parse(cli, argv)
120:
121: cli.main(*args)
122:
123: return cli
124: end
Also aliased as: run
find_longer_option(obj, char)
click to toggle source
TODO: Sort alphabetically?
# File lib/executioner.rb, line 274
274: def find_longer_option(obj, char)
275: meths = obj.methods.map{ |m| m.to_s }
276: meths = meths.select do |m|
277: m.start_with?(char) and (m.end_with?('=') or m.end_with?('!'))
278: end
279: meths.first
280: end
header(text=nil)
click to toggle source
Get or set a help header for the command.
# File lib/executioner.rb, line 283
283: def header(text=nil)
284: @header = text unless text.nil?
285: @header
286: end
help(description)
click to toggle source
Define help information for an option.
help "this options does blah blah"
def foo=(val)
...
end
# File lib/executioner.rb, line 304
304: def help(description)
305: @help = description
306: end
inspect()
click to toggle source
# File lib/executioner.rb, line 328
328: def inspect
329: name
330: end
invoke(obj, meth, argv)
click to toggle source
# File lib/executioner.rb, line 266
266: def invoke(obj, meth, argv)
267: m = Method === meth ? meth : obj.method(meth)
268: a = []
269: m.arity.abs.times{ a << argv.shift }
270: m.call(*a)
271: end
method_added(name)
click to toggle source
# File lib/executioner.rb, line 321
321: def method_added(name)
322: #name = name.to_s.chomp('?').chomp('=')
323: descriptions[name.to_s] = @help if @help
324: @help = nil
325: end
parse(obj, argv, args=[])
click to toggle source
# File lib/executioner.rb, line 169
169: def parse(obj, argv, args=[])
170: case argv
171: when String
172: require 'shellwords'
173: argv = Shellwords.shellwords(argv)
174: #else
175: # argv = argv.dup
176: end
177:
178: #subc = nil
179: #@args = [] #opts, i = {}, 0
180:
181: while argv.size > 0
182: case arg = argv.shift
183: when /=/
184: parse_equal(obj, arg, argv, args)
185: when /^--/
186: parse_option(obj, arg, argv, args)
187: when /^-/
188: parse_flags(obj, arg, argv, args)
189: else
190: #if Executioner === obj
191: # if cmd_class = obj.class.subcommands[arg]
192: # cmd = cmd_class.new(obj)
193: # subc = cmd
194: # parse(cmd, argv, args)
195: # else
196: args << arg
197: # end
198: #end
199: end
200: end
201:
202: return args
203: end
parse_arguments(argv)
click to toggle source
Make sure arguments are an array. If argv is a String, then parse using Shellwords module.
# File lib/executioner.rb, line 146
146: def parse_arguments(argv)
147: if String === argv
148: require 'shellwords'
149: argv = Shellwords.shellwords(argv)
150: end
151: argv.to_a
152: end
parse_equal(obj, opt, argv, args)
click to toggle source
# File lib/executioner.rb, line 206
206: def parse_equal(obj, opt, argv, args)
207: if md = /^[-]*(.*?)=(.*?)$/.match(opt)
208: x, v = md[1], md[2]
209: else
210: raise ArgumentError, "#{x}"
211: end
212: if obj.respond_to?("#{x}=")
213: obj.send("#{x}=", v) # TODO: to_b if 'true' or 'false' ?
214: else
215: obj.option_missing(x, v) # argv?
216: end
217: end
parse_flags(obj, opt, argv, args)
click to toggle source
TODO: This needs some thought concerning character spliting and arguments.
# File lib/executioner.rb, line 237
237: def parse_flags(obj, opt, argv, args)
238: x = opt[1..1]
239: c = 0
240: x.split(//).each do |k|
241: if obj.respond_to?("#{k}=")
242: m = obj.method("#{k}=")
243: if obj.respond_to?("#{x}?")
244: m.call(true)
245: else
246: invoke(obj, m, argv) #m.call(argv.shift)
247: end
248: elsif obj.respond_to?("#{k}!")
249: invoke(obj, "#{k}!", argv)
250: else
251: long = find_longer_option(obj, k)
252: if long
253: if long.end_with?('=') && obj.respond_to?(long.chomp('=')+'?')
254: invoke(obj, long, [true])
255: else
256: invoke(obj, long, argv)
257: end
258: else
259: obj.option_missing(x, argv)
260: end
261: end
262: end
263: end
parse_option(obj, opt, argv, args)
click to toggle source
Parse a command-line option.
# File lib/executioner.rb, line 220
220: def parse_option(obj, opt, argv, args)
221: x = opt.sub(/^\-+/, '') # remove '--'
222: if obj.respond_to?("#{x}=")
223: m = obj.method("#{x}=")
224: if obj.respond_to?("#{x}?")
225: m.call(true)
226: else
227: invoke(obj, m, argv)
228: end
229: elsif obj.respond_to?("#{x}!")
230: invoke(obj, "#{x}!", argv)
231: else
232: obj.option_missing(x, argv)
233: end
234: end
parse_subcommand(argv)
click to toggle source
# File lib/executioner.rb, line 155
155: def parse_subcommand(argv)
156: cmd = self
157: arg = argv.first
158:
159: while c = cmd.subcommands[arg]
160: cmd = c
161: argv.shift
162: arg = argv.first
163: end
164:
165: return cmd, argv
166: end
subcommands()
click to toggle source
List if subcommands.
# File lib/executioner.rb, line 130
130: def subcommands
131: @subcommands ||= (
132: consts = constants - superclass.constants
133: consts.inject({}) do |h, c|
134: c = const_get(c)
135: if Class === c && Executioner > c
136: n = c.name.split('::').last.downcase
137: h[n] = c
138: end
139: h
140: end
141: )
142: end
Public Instance Methods
main(*args)
click to toggle source
# File lib/executioner.rb, line 79
79: def main(*args)
80: #puts self.class # TODO: fix help
81: raise NoCommandError
82: end
option_missing(opt, argv)
click to toggle source
Override option_missing if needed. This receives the name of the option and the remaining arguments list. It must consume any argument it uses from the (begining of) the list.
# File lib/executioner.rb, line 88
88: def option_missing(opt, argv)
89: raise NoOptionError, opt
90: end
Disabled; run with --debug to generate this.