class Ark::CLI::Option

Represents an option and stores the option’s current state, as well as usage information.

Attributes

count[R]

A count of how many times this option has been given on the command line. Useful for flags that might be specified repeatedly, like -vvv to raise verbosity three times.

desc[R]

A short description of the option, if given

long[R]

Long name for this option

short[R]

Short name for this option

Public Class Methods

new(long, short=nil, args=nil, desc=nil) click to toggle source

Initialize a new Option instance

keys

A list of names this option will be identified by

args

A list of argument named this option will expect

desc

A short description of this option

# File lib/ark/cli/option.rb, line 12
def initialize(long, short=nil, args=nil, desc=nil)
  @long      = long
  @short     = short
  @args      = args || []
  @flag      = false
  @count     = 0
  @desc      = desc || ''
  @arg_index = 0
end

Public Instance Methods

arity() click to toggle source

Return the number of arguments this option expects

# File lib/ark/cli/option.rb, line 37
def arity()
  return @args.length
end
flag?() click to toggle source

True if this option expects no arguments; opposite of has_args?

# File lib/ark/cli/option.rb, line 52
def flag?
  return @args.empty?
end
full?() click to toggle source

True if this option has received all the arguments it expects, or if this option expects no arguments

# File lib/ark/cli/option.rb, line 43
def full?
  if self.flag?
    return true
  else
    return (@args.length - @arg_index) < 1
  end
end
has_args?() click to toggle source

True if this option expects an argument. Opposite of flag?

# File lib/ark/cli/option.rb, line 87
def has_args?
  return @args.length > 0
end
header() click to toggle source

Return basic usage information: the option’s names and arguments

# File lib/ark/cli/option.rb, line 92
def header()
  if self.flag?
    args = ''
  else
    args = ' ' + @args.map {|a| a.name }.join(', ').upcase
  end
  short = @short ? "-#{@short} " : ''
  return "#{short}--#{@long}#{args}"
end
push(val) click to toggle source

Pass an argument to this option

# File lib/ark/cli/option.rb, line 69
def push(val)
  arg = @args[@arg_index]
  @arg_index += 1
  arg.set(val)
end
to_s() click to toggle source

Represent this option as a string

# File lib/ark/cli/option.rb, line 103
def to_s()
  return "(#{self.header})"
end
toggle() click to toggle source

Toggle this option to the true state and increment the toggle count. Only valid for options which expect no argument (flags). Attempting to toggle a option with expected arguments will raise an error.

# File lib/ark/cli/option.rb, line 59
def toggle()
  if self.flag?
    @count += 1
    @flag = true
  else
    raise StandardError, "Tried to toggle an option which expects an argument"
  end
end
value() click to toggle source

Return the current value of this option

# File lib/ark/cli/option.rb, line 76
def value()
  if self.flag?
    return @flag
  else
    vals = @args.map {|a| a.value }
    vals = vals.first if vals.length == 1
    return vals
  end
end