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
If true, the full option list will always be displayed in the usage info header
If true, an error will be raised if trailing arguments are given
Public Class Methods
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
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
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 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 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 an array of argument names defined for this spec
# File lib/ark/cli/spec.rb, line 45 def get_args return @arguments end
Get the description defined for this spec
# File lib/ark/cli/spec.rb, line 40 def get_desc return @desc end
Get the name defined for this spec
# File lib/ark/cli/spec.rb, line 35 def get_name return @name end
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 a hash of any options defined on this spec
# File lib/ark/cli/spec.rb, line 60 def get_opts return @options end
Return the argument name of the variadic argument
# File lib/ark/cli/spec.rb, line 88 def get_variad return @variad end
Get version information defined for this spec
# File lib/ark/cli/spec.rb, line 50 def get_version return @version end
Return true
if this interface has any arguments defined
# File lib/ark/cli/spec.rb, line 93 def has_args? @arguments.length > 0 end
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
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
Return true
if this interface is variadic
# File lib/ark/cli/spec.rb, line 83 def is_variadic? return @variadic end
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
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
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
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