Class: Gestopft::App
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #__default__ ⇒ Object
- #dispatch ⇒ Object
- #help_message ⇒ Object
-
#initialize(argv) ⇒ App
constructor
A new instance of App.
- #parse_arg ⇒ Object
Constructor Details
#initialize(argv) ⇒ App
Returns a new instance of App.
30 31 32 33 34 35 |
# File 'lib/gestopft/app.rb', line 30 def initialize(argv) @argv = argv.option_args @expectation = self.class.expectation @options = {} @commands = self.class.commands.map {|cmd| method(cmd) } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/gestopft/app.rb', line 28 def @options end |
Class Method Details
.commands ⇒ Object
24 25 26 |
# File 'lib/gestopft/app.rb', line 24 def self.commands public_instance_methods - Gestopft::App.public_instance_methods end |
.expectation ⇒ Object
6 7 8 |
# File 'lib/gestopft/app.rb', line 6 def self.expectation @expectation ||= [] end |
.option(name, *args) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/gestopft/app.rb', line 10 def self.option(name, *args) desc = args.find {|arg| arg.is_a? String } params = args.find {|arg| arg.is_a? Array } expectation << Gestopft::Option.new(name, { :desc => desc, :params => params }) end |
.run(argv = ARGV) ⇒ Object
19 20 21 22 |
# File 'lib/gestopft/app.rb', line 19 def self.run(argv=ARGV) new(argv).parse_arg.dispatch self end |
Instance Method Details
#__default__ ⇒ Object
98 99 100 |
# File 'lib/gestopft/app.rb', line 98 def __default__ STDIN.puts end |
#dispatch ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gestopft/app.rb', line 57 def dispatch argv = @argv.reject {|arg| arg.option? } @commands.each do |cmd| if pos = argv.find_index(cmd.name.to_s) params = argv[pos + 1, cmd.arity.abs] return cmd.call(*params) end end send(:__default__) end |
#help_message ⇒ Object
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 |
# File 'lib/gestopft/app.rb', line 68 def msg = [] msg << <<-BANNER.strip Usage: $ #{File.basename($0)} [options] #{"command [args, ...]" unless @commands.empty?} BANNER unless @expectation.empty? msg << "Options: " @expectation.each do |opt| line = ["\t", opt.option_name] line << opt.params.map {|param| param.to_s.upcase }.join(' ') unless opt.params.empty? line << opt.desc unless opt.desc.empty? msg << line.join(" ") end end unless @commands.empty? msg << <<-COMMANDS.strip Commands: #{@commands.map {|cmd| cmd.name }.join(' ')} COMMANDS end msg << "" msg.join("\n\n") end |
#parse_arg ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gestopft/app.rb', line 37 def parse_arg @expectation.each do |opt| if opt.require_args? if ( pos = @argv.find_index(opt.option_name) and params = @argv[pos + 1, opt.arity] and params.none? {|param| param.option? } and params.size == opt.arity ) @options[opt.name] = params else raise NotSatisfiedRequirements end else @options[opt.name] = opt.option_name end end self end |