Sha256: c1f73deebbda1b6aa48852771ed4c2a8917a565ba920e598af4d811cac431d6a

Contents?: true

Size: 1.66 KB

Versions: 29

Compression:

Stored size: 1.66 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 #:ndoc:
    attr_reader :aliases #:ndoc:
    attr_reader :description #:ndoc:
    attr_reader :long_description #:ndoc:

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

    def usage #:nodoc:
      all_forms
    end

    # Sort based on 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 = Hash.new
      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 all_forms_a
      forms = [self.class.name_as_string(name)]
      if aliases
        forms |= aliases.collect { |one_alias| self.class.name_as_string(one_alias) }.sort { |one,two| two.length <=> one.length }
      end
      forms
    end
  end
end

Version data entries

29 entries across 29 versions & 4 rubygems

Version Path
gli-1.3.4 lib/gli/command_line_token.rb
brightbox-cli-0.13.1 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
bbcloud-0.13.0 lib/bbcloud/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.13.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
gli-1.3.3 lib/gli/command_line_token.rb
bbcloud-0.12.0 lib/bbcloud/vendor/gli/lib/gli/command_line_token.rb
gli-1.3.2 lib/gli/command_line_token.rb
gli-1.3.1 lib/gli/command_line_token.rb
gli-1.3.0 lib/gli/command_line_token.rb