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()
  @args = []
  @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



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ark/cli/spec.rb', line 162

def args(*input)
  @args = []
  @defaults = {}

  input.flatten.each_with_index do |item, i|
    item = item.to_s
    last = (input.length - (i + 1)) == 0
    parse_arg(item, last: last)
  end

  @refargs = @args.clone
end

#desc(str) ⇒ Object

Set the description of the program to str



157
158
159
# File 'lib/ark/cli/spec.rb', line 157

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



203
204
205
# File 'lib/ark/cli/spec.rb', line 203

def force_option_list()
  @option_list = true
end

#get_argsObject

Get an array of argument names defined for this spec



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

def get_args
  return @args
end

#get_default(arg) ⇒ Object

Return the default value of the given argument arg



132
133
134
# File 'lib/ark/cli/spec.rb', line 132

def get_default(arg)
  @defaults[arg.to_s]
end

#get_defaultsObject

Return a hash of all default values



127
128
129
# File 'lib/ark/cli/spec.rb', line 127

def get_defaults
  return @defaults
end

#get_descObject

Get the description defined for this spec



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

def get_desc
  return @desc
end

#get_nameObject

Get the name defined for this spec



78
79
80
# File 'lib/ark/cli/spec.rb', line 78

def get_name
  return @name
end

#get_opt(name) ⇒ Object

Get an Option object for the given option name



103
104
105
106
107
108
109
# File 'lib/ark/cli/spec.rb', line 103

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



98
99
100
# File 'lib/ark/cli/spec.rb', line 98

def get_opts
  return @options
end

#get_variadObject

Return the argument name of the variadic argument



117
118
119
# File 'lib/ark/cli/spec.rb', line 117

def get_variad
  return @variad
end

#has_args?Boolean

Return true if this interface has any arguments defined

Returns:

  • (Boolean)


137
138
139
# File 'lib/ark/cli/spec.rb', line 137

def has_args?
  @args.length > 0
end

#has_default?(arg) ⇒ Boolean

Return true if the given argument arg has a default value

Returns:

  • (Boolean)


122
123
124
# File 'lib/ark/cli/spec.rb', line 122

def has_default?(arg)
  @defaults.key?(arg.to_s)
end

#has_options?Boolean

Return true if this interface has any options defined for it

Returns:

  • (Boolean)


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

def has_options?
  @options.values.uniq.length > 1
end

#header(name: nil, desc: nil, args: []) ⇒ Object

Specify general information about the program

name

Name of the program

desc

Short description of the program

args

A list of named arguments



145
146
147
148
149
# File 'lib/ark/cli/spec.rb', line 145

def header(name: nil, desc: nil, args: [])
  self.name(name)
  self.desc(desc)
  self.args(args)
end

#is_variadic?Boolean

Return true if this interface is variadic

Returns:

  • (Boolean)


112
113
114
# File 'lib/ark/cli/spec.rb', line 112

def is_variadic?
  return @variadic
end

#name(str) ⇒ Object

Set the name of the program to str



152
153
154
# File 'lib/ark/cli/spec.rb', line 152

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ark/cli/spec.rb', line 179

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)
  defaults = []
  if args
    args.map! do |arg|
      arg = arg.to_s
      if defaulted?(arg)
        defaults << parse_default(arg)
        arg = strip(arg)
      else
        defaults << nil
      end
      arg
    end
  end
  o = Option.new(long, short, args, defaults, 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)



209
210
211
# File 'lib/ark/cli/spec.rb', line 209

def raise_on_trailing()
  @trailing_error = true
end