class Ark::CLI::Spec

The Spec class defines the properties of an interface, namely its expected arguments and option definitions, as well as the program name and description. The Spec instance forms the DSL used for interface declarations with the methods name, desc, args, and opt.

Attributes

option_listing[R]

If true, the full option list will always be displayed in the usage info header

trailing_error[R]

If true, an error will be raised if trailing arguments are given

Public Class Methods

new() click to toggle source

Initialize a bare interface Spec

# File lib/ark/cli/spec.rb, line 19
def initialize()
  @arguments = {}
  @options = {}
  @variadic       = false
  @option_listing = false
  @trailing_error = false
end

Public Instance Methods

args(*input) click to toggle source

Define what arguments the program will accept

# File lib/ark/cli/spec.rb, line 120
def args(*input)
  @arguments = {}
  input.flatten.each_with_index do |item, i|
    item = item.to_s
    last = (input.length - (i + 1)) == 0
    arg  = Argument.parse(item)
    if arg.variadic?
      if !last
        raise ArgumentSyntaxError, "Variadic arguments must come last. Offending variad is '#{arg}'"
      else
        @variadic = true
        @variad = arg
      end
    end
    @arguments[arg.name] = arg
  end
end
desc(str) click to toggle source

Set the description of the program to str

# File lib/ark/cli/spec.rb, line 115
def desc(str)
  @desc = str.to_s if str
end
force_option_list() click to toggle source

Force the full option list display in the usage info, no matter how many options the program has

# File lib/ark/cli/spec.rb, line 159
def force_option_list()
  @option_list = true
end
get_arg(name) click to toggle source

Get an Argument object for the given argument name

# File lib/ark/cli/spec.rb, line 74
def get_arg(name)
  name = name.to_s
  if !@arguments.keys.member?(name)
    raise NoSuchArgumentError, "Error, no such argument: '#{name}'"
  end
  return @arguments[name]
end
get_args() click to toggle source

Get an array of argument names defined for this spec

# File lib/ark/cli/spec.rb, line 45
def get_args
  return @arguments
end
get_desc() click to toggle source

Get the description defined for this spec

# File lib/ark/cli/spec.rb, line 40
def get_desc
  return @desc
end
get_name() click to toggle source

Get the name defined for this spec

# File lib/ark/cli/spec.rb, line 35
def get_name
  return @name
end
get_opt(name) click to toggle source

Get an Option object for the given option name

# File lib/ark/cli/spec.rb, line 65
def get_opt(name)
  name = name.to_s
  if !@options.keys.member?(name)
    raise NoSuchOptionError, "Error, no such option: '#{name}'"
  end
  return @options[name]
end
get_opts() click to toggle source

Get a hash of any options defined on this spec

# File lib/ark/cli/spec.rb, line 60
def get_opts
  return @options
end
get_variad() click to toggle source

Return the argument name of the variadic argument

# File lib/ark/cli/spec.rb, line 88
def get_variad
  return @variad
end
get_version() click to toggle source

Get version information defined for this spec

# File lib/ark/cli/spec.rb, line 50
def get_version
  return @version
end
has_args?() click to toggle source

Return true if this interface has any arguments defined

# File lib/ark/cli/spec.rb, line 93
def has_args?
  @arguments.length > 0
end
has_options?() click to toggle source

Return true if this interface has any options defined for it

# File lib/ark/cli/spec.rb, line 55
def has_options?
  @options.values.uniq.length > 1
end
header(name: nil, desc: nil, args: [], version: nil) click to toggle source

Specify general information about the program

name

Name of the program

desc

Short description of the program

args

A list of named arguments

version

Current version information for the program

# File lib/ark/cli/spec.rb, line 102
def header(name: nil, desc: nil, args: [], version: nil)
  self.name(name)
  self.desc(desc)
  self.args(args)
  self.version(version)
end
is_variadic?() click to toggle source

Return true if this interface is variadic

# File lib/ark/cli/spec.rb, line 83
def is_variadic?
  return @variadic
end
name(str) click to toggle source

Set the name of the program to str

# File lib/ark/cli/spec.rb, line 110
def name(str)
  @name = str.to_s if str
end
opt(long, short=nil, args: nil, desc: nil) click to toggle source

Define an Option

keys

A list of names for this option

args

A list of arguments the option expects

desc

A short description of the option, used to provide usage info

# File lib/ark/cli/spec.rb, line 147
def opt(long, short=nil, args: nil, desc: nil)
  long = long.to_s
  short = short.to_s if short
  args = [args] if args.is_a?(String)
  args.map! {|a| Argument.parse(a) } if args
  o = Option.new(long, short, args, desc)
  @options[long] = o
  @options[short] = o if short
end
raise_on_trailing() click to toggle source

The parser will raise an error on finding trailing arguments (default behavior is to ignore and stuff the trailing args into Report.trailing_args)

# File lib/ark/cli/spec.rb, line 165
def raise_on_trailing()
  @trailing_error = true
end
version(str) click to toggle source

Set the version information for the program to str

# File lib/ark/cli/spec.rb, line 139
def version(str)
  @version = str.to_s if str
end