Class Clio::Usage::Option
In: lib/clio/usage/option.rb
Parent: Object

Methods

===   aliases   arg   argument   completion   flag?   help   initialize_copy   inspect   key   multiple   multiple?   new   to_s   xor  

Attributes

aliases  [R]  Alternate names for this option.
arguments  [R]  Option arguments.
excludes  [R]  Option (names) that are mutually exlusive to this option.
help  [R]  Help text for this option.
multiple  [R]  Option can be used more that once.
name  [R]  Option name.

Public Class methods

 New Option.

def initialize(name, parent=nil, &block)

[Source]

# File lib/clio/usage/option.rb, line 38
      def initialize(name, &block)
        @name      = clean_name(name)
        #@parent    = parent
        @aliases   = []
        @arguments = []
        @multiple  = false
        #@greedy    = false
        @exclude   = []
        @help      = ''
        instance_eval(&block) if block
      end

Public Instance methods

[Source]

# File lib/clio/usage/option.rb, line 171
      def ===(other)
        other = clean_key(other)
        return true if clean_key(key) == other
        return true if aliases.include?(other)
        return false
      end

Specify aliases for the option.

[Source]

# File lib/clio/usage/option.rb, line 95
      def aliases(*names)
        return @aliases if names.empty?
        names.each do |name|
          @aliases << clean_key(name)
        end
      end

Argument shorthand.

  arg('PIN', 'pin number')

[Source]

# File lib/clio/usage/option.rb, line 137
      def arg(slot, help=nil)
        argument(slot).help(help)
      end

Assign an argument to the option.

[Source]

# File lib/clio/usage/option.rb, line 87
      def argument(name, &block)
        arg = Argument.new(name) #, self)
        arg.instance_eval(&block) if block
        @arguments << arg
        arg
      end

Tab completion.

[Source]

# File lib/clio/usage/option.rb, line 126
      def completion
        arguments.collect{|c| c.type}
      end

Is this option a boolean flag?

[Source]

# File lib/clio/usage/option.rb, line 84
      def flag?; @arguments.empty?; end

[Source]

# File lib/clio/usage/option.rb, line 120
      def help(string=nil)
        @help.replace(string.to_s) if string
        @help
      end

[Source]

# File lib/clio/usage/option.rb, line 51
      def initialize_copy(o)
        @name    = o.name.dup
        @aliases = o.aliases.dup
        #@multiple = o.multiple
        @exclude = o.exclude.dup
        @help    = o.help.dup
      end

[Source]

# File lib/clio/usage/option.rb, line 72
      def inspect
        to_s
        #s  = "[--#{key}"
        #s << "*" if multiple
        #s << "=" + arguments.join(',') unless arguments.empty?
        #s << " " + aliases.collect{ |a| "-#{a}" } unless aliases.empty?
        ##s << " @excludes=#{@excludes.inspect}" unless @excludes.empty?
        #s << "]"
        #s
      end

Same as name but given as a symbol.

[Source]

# File lib/clio/usage/option.rb, line 60
      def key
        name.to_sym
      end

Specify if the option be used multiple times.

[Source]

# File lib/clio/usage/option.rb, line 103
      def multiple(bool=nil)
        return @multiple if bool.nil?
        @multiple = bool
        self
      end

Can this option occur multiple times in the command line?

[Source]

# File lib/clio/usage/option.rb, line 66
      def multiple? ; @multiple ; end

[Source]

# File lib/clio/usage/option.rb, line 142
      def to_s
        tiny = aliases.select do |a|
          a.to_s.size == 1
        end
        tiny.unshift(name) if name.size == 1

        long = aliases.select do |a|
          a.to_s.size > 1
        end
        long.unshift(name) if name.size > 1

        tiny = tiny.collect{ |l| "-#{l}" }
        long = long.collect{ |w| "--#{w}" }

        if tiny.empty?
          opts = [ '  ', *long ]
        else
          opts = tiny + long
        end

        unless arguments.empty?
          args = arguments.collect{ |a| a.to_s.sub(/^[<]/,'').sub(/[>]$/,'') }
          opts.last << "=" + args.join(',')
        end

        opts.join(' ')
      end

Specify mutually exclusive options.

[Source]

# File lib/clio/usage/option.rb, line 110
      def xor(*opts)
        @exclusive.concat(opts)
      end

[Validate]