Module: Af::OptionParser

Includes:
Interface
Included in:
Application
Defined in:
lib/fiksu-af/option_parser.rb,
lib/fiksu-af/option_parser/dsl.rb,
lib/fiksu-af/option_parser/helper.rb,
lib/fiksu-af/option_parser/option.rb,
lib/fiksu-af/option_parser/interface.rb,
lib/fiksu-af/option_parser/columnizer.rb,
lib/fiksu-af/option_parser/get_options.rb,
lib/fiksu-af/option_parser/option_type.rb,
lib/fiksu-af/option_parser/option_check.rb,
lib/fiksu-af/option_parser/option_group.rb,
lib/fiksu-af/option_parser/option_store.rb,
lib/fiksu-af/option_parser/option_finder.rb,
lib/fiksu-af/option_parser/option_select.rb,
lib/fiksu-af/option_parser/instance_variable_setter.rb

Defined Under Namespace

Modules: Dsl, Interface Classes: BadChoiceError, Columnizer, Error, GetOptions, Helper, InstanceVariableSetter, MisconfiguredOptionError, Option, OptionCheck, OptionCheckError, OptionFinder, OptionGroup, OptionSelect, OptionSelectError, OptionStore, OptionType, UndeterminedArgumentTypeError

Class Method Summary collapse

Methods included from Interface

#opt, #opt_check, #opt_error, #opt_group, #opt_select, #process_command_line_options, #usage

Class Method Details

.add_option_typesObject



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fiksu-af/option_parser.rb', line 30

def self.add_option_types
  OptionType.new(:Switch, :switch, "", lambda {|argument,option|
                   if ["t", "true", "yes", "on"].include?(argument.to_s.downcase)
                     return true
                   elsif ["f", "false", "no", "off"].include?(argument.to_s.downcase)
                     return false
                   else
                     if option.default_value == nil
                       return true
                     else
                       return !option.default_value
                     end
                   end
                 }, lambda{ |value| return (value.is_a?(TrueClass) || value.is_a?(FalseClass)) })
  OptionType.new(:Boolean, :boolean, "", lambda {|argument,option|
                   if ["t", "true", "yes", "on"].include?(argument.to_s.downcase)
                     return true
                   elsif ["f", "false", "no", "off"].include?(argument.to_s.downcase)
                     return false
                   else
                     if option.default_value == nil
                       return true
                     else
                       return !option.default_value
                     end
                   end
                 }, lambda{ |value| return (value.is_a?(TrueClass) || value.is_a?(FalseClass)) })
  OptionType.new(:Int, :int, "INTEGER", :to_i, Fixnum)
  OptionType.new(:Integer, :integer, "INTEGER", :to_i, Fixnum)
  OptionType.new(:Float, :float, "NUMBER", :to_f, Float)
  OptionType.new(:Number, :number, "NUMBER", :to_f, Float)
  OptionType.new(:String, :string, "STRING", :to_s, String)
  OptionType.new(:Uri, :uri, "URI", lambda {|argument, option_parser| return URI.parse(argument) }, URI::HTTP)
  OptionType.new(:Date, :date, "DATE", lambda {|argument, option_parser| return Time.zone.parse(argument).to_date }, Date)
  OptionType.new(:Time, :time, "TIME", lambda {|argument, option_parser| return Time.zone.parse(argument) }, Time)
  OptionType.new(:DateTime, :datetime, "DATETIME", lambda {|argument, option_parser| return Time.zone.parse(argument) }, DateTime)
  OptionType.new(:Choice,
                 :choice,
                 "CHOICE",
                 lambda { |argument, option_parser|
                   choice = argument.to_sym
                   choices = option_parser.choices
                   unless choices.blank?
                     unless choices.include? choice
                       raise BadChoiceError.new("invalid choice '#{choice}' not in list of choices: #{choices.map(&:to_s).join(', ')}")
                     end
                   end
                   return choice
                 }, Symbol)
  OptionType.new(:Hash,
                 :hash,
                 "K1=V1,K2=V2,K3=V3...",
                 lambda { |argument, option_parser| return Hash[argument.split(',').map{ |ai| ai.split('=') }] },
                 Hash)
  OptionType.new(:Ints,
                 :ints,
                 "INT1,INT2,INT3...",
                 lambda { |argument, option_parser| return argument.split(',').map(&:to_i) },
                 lambda { |value| return value.class == Array && value.first.class == Fixnum })
  OptionType.new(:Integers,
                 :integers,
                 "INT1,INT2,INT3...",
                 lambda { |argument, option_parser| return argument.split(',').map(&:to_i) },
                 lambda { |value| return value.class == Array && value.first.class == Fixnum })
  OptionType.new(:Floats,
                 :floats,
                 "NUM1,NUM2,NUM3...",
                 lambda { |argument, option_parser| return argument.split(',').map(&:to_f) },
                 lambda { |value| return value.class == Array && value.first.class == Float })
  OptionType.new(:Numbers,
                 :numbers,
                 "NUM1,NUM2,NUM3...",
                 lambda { |argument, option_parser| return argument.split(',').map(&:to_f) },
                 lambda { |value| return value.class == Array && value.first.class == Float })
  OptionType.new(:Strings,
                 :strings,
                 "STR1,STR2,STR3...",
                 lambda { |argument, option_parser| return argument.split(',').map(&:to_s) },
                 lambda { |value| return value.class == Array && value.first.class == String })
  OptionType.new(:Uris,
                 :uris,
                 "URL1,URL2,URL3...",
                 lambda { |argument, option_parser| return argument.split(',').map{ |a| URI.parse(a)} },
                 lambda { |value| return value.class == Array && value.first.class == URI::HTTP })
  OptionType.new(:Dates,
                 :dates,
                 "DATE1,DATE2,DATE3...",
                 lambda { |argument, option_parser| return argument.split(',').map{ |a| Time.zone.parse(a).to_date} },
                 lambda { |value| return value.class == Array && value.first.class == Date })
  OptionType.new(:Times,
                 :times,
                 "TIME1,TIME2,TIME3...",
                 lambda { |argument, option_parser| return argument.split(',').map{ |a| Time.zone.parse(a) } },
                 lambda { |value| return value.class == Array && value.first.class == Time })
  OptionType.new(:DateTimes,
                 :datetimes,
                 "TIME1,TIME2,TIME3...",
                 lambda { |argument, option_parser| return argument.split(',').map{ |a| Time.zone.parse(a) } },
                 lambda { |value| return value.class == Array && value.first.class == DateTime })
  OptionType.new(:Choices,
                 :choices,
                 "CHOICE1,CHOICE2,CHOICE3...",
                 lambda { |argument, option_parser|
                   choice_list = argument.split(',').map(&:to_sym)
                   choices = option_parser.choices
                   unless choices.blank?
                     choice_list.each do |choice|
                       unless choices.include? choice
                         raise BadChoiceError.new("invalid choice '#{choice}' not in list of choices: #{choices.map(&:to_s).join(', ')}")
                       end
                     end
                   end
                   return choice_list
                 }, lambda {|value| return value.class == Array && value.first.class == Symbol })
end

.included(base) ⇒ Object



25
26
27
28
# File 'lib/fiksu-af/option_parser.rb', line 25

def self.included(base)
  add_option_types
  base.extend(Dsl)
end