Sha256: ef0a753b6f5e99ac5f8f4c9d71db2444278795672c27a70108506a3f745985cb

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module GLI
  # Logical element of a command line, mostly so that subclasses can have similar
  # initialization and interface
  class CommandLineToken
    attr_reader :name
    attr_reader :aliases
    attr_reader :description

    def initialize(names,description)
      @description = description
      @name,@aliases,@names = parse_names(names)
    end

    def usage
      all_forms
    end

    private
    # 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
      forms.join(joiner)
    end


    # Handles dealing with the "names" param, parsing
    # it into the primary name and aliases list
    def parse_names(names)
      names_hash = Hash.new
      names = names.is_a?(Array) ? names : [names]
      names.each { |n| names_hash[self.class.name_as_string(n)] = true }
      name = names.shift
      aliases = names.length > 0 ? names : nil
      [name,aliases,names_hash]
    end

    def all_forms_a
      forms = [self.class.name_as_string(name)]
      if aliases
        forms |= aliases.collect { |a| self.class.name_as_string(a) }.sort { |x,y| y.length <=> x.length }
      end
      forms
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
davetron5000-gli-0.1.4 lib/gli/command_line_token.rb
davetron5000-gli-0.1.5 lib/gli/command_line_token.rb
davetron5000-gli-0.1.6 lib/gli/command_line_token.rb
davetron5000-gli-0.2.0 lib/gli/command_line_token.rb
gli-0.1.6 lib/gli/command_line_token.rb