Sha256: 608a7448717ff6d459542a09b987e5cdddd76f8ecb03ca9b6cc5e3df92cf4f77

Contents?: true

Size: 1.69 KB

Versions: 15

Compression:

Stored size: 1.69 KB

Contents

require 'gli/command_line_token.rb'

module GLI
  # Defines a flag, which is to say a switch that takes an argument
  class Flag < Switch

    attr_reader :default_value

    def initialize(names,description,argument_name=nil,default=nil,long_desc=nil)
      super(names,description,long_desc)
      @argument_name = argument_name || "arg"
      @default_value = default
    end

    def get_value!(args)
      args.each_index() do |index|
        arg = args[index]
        present,matched,value = find_me(arg)
        if present
          args.delete_at index
          if !value || value == ''
            if args[index]
              value = args[index]
              args.delete_at index
              return value
            else
              raise "#{matched} requires an argument"
            end
          else
            return value
          end
        end
      end
      return @default_value
    end

    def find_me(arg)
      if @names[arg]
        return [true,arg,nil] if arg.length == 2
        # This means we matched the long-form, but there's no argument
        raise "#{arg} requires an argument via #{arg}=argument"
      end
      @names.keys.each() do |name|
        match_string = "^#{name}=(.*)$"
        match_data = arg.match(match_string)
        return [true,name,$1] if match_data;
      end
      [false,nil,nil]
    end

    # Returns a string of all possible forms
    # of this flag.  Mostly intended for printing
    # to the user.
    def all_forms(joiner=', ')
      forms = all_forms_a
      string = forms.join(joiner)
      if forms[-1] =~ /^\-\-/
        string += '='
      else
        string += ' '
      end
      string += @argument_name
      return string
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
davetron5000-gli-0.2.1 lib/gli/flag.rb
davetron5000-gli-0.2.3 lib/gli/flag.rb
gli-1.2.2 lib/gli/flag.rb
gli-1.2.1 lib/gli/flag.rb
gli-1.2.0 lib/gli/flag.rb
bbcloud-0.8.1 lib/bbcloud/vendor/gli-1.1.2/lib/gli/flag.rb
gli-1.1.3 lib/gli/flag.rb
gli-1.1.2 lib/gli/flag.rb
gli-1.1.1 lib/gli/flag.rb
gli-1.1.0 lib/gli/flag.rb
gli-1.0.0 lib/gli/flag.rb
gli-0.3.1 lib/gli/flag.rb
gli-0.3.0 lib/gli/flag.rb
gli-0.2.1 lib/gli/flag.rb
gli-0.2.3 lib/gli/flag.rb