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
brightbox-cli-0.18.1 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.18.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
gli-1.6.0 lib/gli/command_line_token.rb
brightbox-cli-0.17.5 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.17.4 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.17.3 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.17.2 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
gli-1.5.1 lib/gli/command_line_token.rb
brightbox-cli-0.17.1 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
iisconfig-0.0.1.pre2 build/bundler/ruby/1.9.1/gems/gli-1.4.0/lib/gli/command_line_token.rb
brightbox-cli-0.17.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
iisconfig-0.0.1.pre1 build/bundler/ruby/1.9.1/gems/gli-1.4.0/lib/gli/command_line_token.rb
gli-1.4.0 lib/gli/command_line_token.rb
brightbox-cli-0.16.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
gli-1.3.7 lib/gli/command_line_token.rb
gli-1.3.6 lib/gli/command_line_token.rb
brightbox-cli-0.15.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
gli-1.3.5 lib/gli/command_line_token.rb
brightbox-cli-0.14.1 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb
brightbox-cli-0.14.0 lib/brightbox-cli/vendor/gli/lib/gli/command_line_token.rb