Class: Ark::CLI::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/ark/cli/spec.rb

Overview

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.

Defined Under Namespace

Classes: ArgumentSyntaxError, NoSuchOptionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpec

Initialize a bare interface Spec



19
20
21
22
23
24
25
# File 'lib/ark/cli/spec.rb', line 19

def initialize()
  @arguments = {}
  @options = {}
  @variadic       = false
  @option_listing = false
  @trailing_error = false
end

Instance Attribute Details

#option_listingObject (readonly)

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



29
30
31
# File 'lib/ark/cli/spec.rb', line 29

def option_listing
  @option_listing
end

#trailing_errorObject (readonly)

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



32
33
34
# File 'lib/ark/cli/spec.rb', line 32

def trailing_error
  @trailing_error
end

Instance Method Details

#args(*input) ⇒ Object

Define what arguments the program will accept



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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) ⇒ Object

Set the description of the program to str



115
116
117
# File 'lib/ark/cli/spec.rb', line 115

def desc(str)
  @desc = str.to_s if str
end

#force_option_listObject

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



159
160
161
# File 'lib/ark/cli/spec.rb', line 159

def force_option_list()
  @option_list = true
end

#get_arg(name) ⇒ Object

Get an Argument object for the given argument name



74
75
76
77
78
79
80
# 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_argsObject

Get an array of argument names defined for this spec



45
46
47
# File 'lib/ark/cli/spec.rb', line 45

def get_args
  return @arguments
end

#get_descObject

Get the description defined for this spec



40
41
42
# File 'lib/ark/cli/spec.rb', line 40

def get_desc
  return @desc
end

#get_nameObject

Get the name defined for this spec



35
36
37
# File 'lib/ark/cli/spec.rb', line 35

def get_name
  return @name
end

#get_opt(name) ⇒ Object

Get an Option object for the given option name



65
66
67
68
69
70
71
# 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_optsObject

Get a hash of any options defined on this spec



60
61
62
# File 'lib/ark/cli/spec.rb', line 60

def get_opts
  return @options
end

#get_variadObject

Return the argument name of the variadic argument



88
89
90
# File 'lib/ark/cli/spec.rb', line 88

def get_variad
  return @variad
end

#get_versionObject

Get version information defined for this spec



50
51
52
# File 'lib/ark/cli/spec.rb', line 50

def get_version
  return @version
end

#has_args?Boolean

Return true if this interface has any arguments defined

Returns:

  • (Boolean)


93
94
95
# File 'lib/ark/cli/spec.rb', line 93

def has_args?
  @arguments.length > 0
end

#has_options?Boolean

Return true if this interface has any options defined for it

Returns:

  • (Boolean)


55
56
57
# 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) ⇒ Object

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



102
103
104
105
106
107
# 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?Boolean

Return true if this interface is variadic

Returns:

  • (Boolean)


83
84
85
# File 'lib/ark/cli/spec.rb', line 83

def is_variadic?
  return @variadic
end

#name(str) ⇒ Object

Set the name of the program to str



110
111
112
# 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) ⇒ Object

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



147
148
149
150
151
152
153
154
155
# 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_trailingObject

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



165
166
167
# File 'lib/ark/cli/spec.rb', line 165

def raise_on_trailing()
  @trailing_error = true
end

#version(str) ⇒ Object

Set the version information for the program to str



139
140
141
# File 'lib/ark/cli/spec.rb', line 139

def version(str)
  @version = str.to_s if str
end