Sha256: 2baef1a9397357ab2fb88770e4df9a962cc82b7196a5e0699b834d5267e920ba

Contents?: true

Size: 1.73 KB

Versions: 21

Compression:

Stored size: 1.73 KB

Contents

module GLI
  # Abstract base class for a logical element of a command line, mostly so that subclasses can have similar
  # initialization and interface
  class CommandLineToken
    attr_reader :name #:nodoc:
    attr_reader :aliases #:nodoc:
    attr_reader :description #:nodoc:
    attr_reader :long_description #:nodoc:

    def initialize(names,description,long_description=nil) #:nodoc:
      @description = description
      @long_description = long_description
      @name,@aliases,@names = parse_names(names)
    end

    def usage #:nodoc:
      all_forms
    end

    # Sort based on primary name
    def <=>(other)
      self.name.to_s <=> other.name.to_s
    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)
      # Allow strings; convert to symbols
      names = [names].flatten.map { |name| name.to_sym } 
      names_hash = {}
      names.each do |name| 
        raise ArgumentError.new("#{name} has spaces; they are not allowed") if name.to_s =~ /\s/
        names_hash[self.class.name_as_string(name)] = true
      end
      name = names.shift
      aliases = names.length > 0 ? names : nil
      [name,aliases,names_hash]
    end

    def negatable?
      false;
    end

    def all_forms_a
      forms = [self.class.name_as_string(name,negatable?)]
      if aliases
        forms |= aliases.map { |one_alias| self.class.name_as_string(one_alias,negatable?) }.sort { |one,two| one.length <=> two.length }
      end
      forms
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
gli-2.5.6 lib/gli/command_line_token.rb
gli-2.5.5 lib/gli/command_line_token.rb
gli-2.5.4 lib/gli/command_line_token.rb
gli-2.5.3 lib/gli/command_line_token.rb
gli-2.5.2 lib/gli/command_line_token.rb
gli-2.5.1 lib/gli/command_line_token.rb
gli-2.5.0 lib/gli/command_line_token.rb
gli-2.4.1 lib/gli/command_line_token.rb
gli-2.4.0 lib/gli/command_line_token.rb
gli-2.3.0 lib/gli/command_line_token.rb
gli-2.3.0.rc1 lib/gli/command_line_token.rb
gli-2.2.1 lib/gli/command_line_token.rb
gli-2.2.0 lib/gli/command_line_token.rb
gli-2.1.0 lib/gli/command_line_token.rb
gli-2.0.0 lib/gli/command_line_token.rb
gli-2.0.0.rc8 lib/gli/command_line_token.rb
gli-2.0.0.rc7 lib/gli/command_line_token.rb
gli-2.0.0.rc6 lib/gli/command_line_token.rb
gli-2.0.0.rc5 lib/gli/command_line_token.rb
gli-2.0.0.rc4 lib/gli/command_line_token.rb