# and finally checking the options according to the casts. Returns nil if
# argv is empty, otherwise an options hash.
def parse(argv,cast_list=[],script_path=$0)
- casts = Casts.new(cast_list)
+ casts = Casts.new(cast_list)
- pp casts.class
- pp casts.methods
+ pp casts
@script_path = script_path
TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
MANDATORY = %w[long short type mandatory default allowed disallowed]
- def initialize(casts=[])
- @casts = casts
+ def initialize(cast_list=[])
+ @cast_list = cast_list
ubiquitous
check
+ self << @cast_list
end
# Add ubiquitous options casts.
def ubiquitous
- @casts << {:long => 'help', :short => '?', :type => 'flag', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
- @casts << {:long => 'stream_in', :short => 'I', :type => 'files!', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
- @casts << {:long => 'stream_out', :short => 'O', :type => 'file', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
- @casts << {:long => 'verbose', :short => 'v', :type => 'flag', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
+ @cast_list << {:long => 'help', :short => '?', :type => 'flag', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
+ @cast_list << {:long => 'stream_in', :short => 'I', :type => 'files!', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
+ @cast_list << {:long => 'stream_out', :short => 'O', :type => 'file', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
+ @cast_list << {:long => 'verbose', :short => 'v', :type => 'flag', :mandatory => false, :default => nil, :allowed => nil, :disallowed => nil}
end
# Check integrity of the casts.
# Check if all mandatory keys are present in casts and raise if not.
def check_keys
- @casts.each do |cast|
+ @cast_list.each do |cast|
MANDATORY.each do |mandatory|
raise CastError, "Missing symbol in cast: '#{mandatory.to_sym}'" unless cast.has_key? mandatory.to_sym
end
# Check if all values in casts are valid.
def check_values
- @casts.each do |cast|
+ @cast_list.each do |cast|
check_val_long(cast)
check_val_short(cast)
check_val_type(cast)
# Check cast for duplicate long or short options names.
def check_duplicates
check_hash = {}
- @casts.each do |cast|
+ @cast_list.each do |cast|
raise CastError, "Duplicate argument: '--#{cast[:long]}'" if check_hash.has_key? cast[:long]
raise CastError, "Duplicate argument: '-#{cast[:short]}'" if check_hash.has_key? cast[:short]
check_hash[cast[:long]] = true