Module: CLASP
- Defined in:
- lib/clasp/doc_.rb,
lib/clasp/cli.rb,
lib/clasp/version.rb,
lib/clasp/arguments.rb,
lib/clasp/specifications.rb
Overview
Main module for CLASP.Ruby library
Examples
Simple command-line, no specifications
argv = %w{ --show-all=true infile -c outfile }
args = CLASP::Arguments.new(argv)
puts args.flags.size # => 1
puts args.flags[0] # => -c
puts args.flags[0].name # => -c
puts args.flags[0].inspect # => #<CLASP::Arguments::FlagArgument:0x007f87e18d4530 @arg="-c", @given_index=2, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>
puts args..size # => 1
puts args.[0] # => --show-all=true
puts args.[0].name # => --show-all
puts args.[0].value # => true
puts args.[0].inspect # => #<CLASP::Arguments::OptionArgument:0x007f87e18d4940 @arg="--show-all=true", @given_index=0, @given_name="--show-all", @argument_alias=nil, @given_hyphens=2, @given_label="show-all", @value="true", @name="--show-all", @extras={}>
puts args.values.size # => 2
puts args.values[0] # => infile
puts args.values[0].given_index # => 1
puts args.values[1] # => outfile
puts args.values[1].given_index # => 3
Use of the special double-slash flag to treat all subsequent arguments as values
argv = %w{ --show-all=true -- infile outfile -c }
args = CLASP::Arguments.new(argv)
puts args.flags.size # => 0
puts args..size # => 1
puts args.[0] # => --show-all
puts args.[0].inspect # => #<CLASP::Arguments::OptionArgument:0x007fd23aa3aca0 @arg="--show-all=true", @given_index=0, @given_name="--show-all", @argument_alias=nil, @given_hyphens=2, @given_label="show-all", @value="true", @name="--show-all", @extras={}>
puts args.values.size # => 3
puts args.values[0] # => infile
puts args.values[1] # => outfile
puts args.values[2] # => "-c"
Use of flag short forms
specifications = [
CLASP.Flag('--verbose', alias: '-v'),
CLASP.Flag('--trace-output', aliases: [ '-t', '--trace' ]),
]
argv = %w{ -trace -v }
args = CLASP::Arguments.new(argv, specifications)
puts args.flags.size # => 2
puts args.flags[0].name # => --trace-output
puts args.flags[1].name # => --verbose
puts args..size # => 0
puts args.values.size # => 0
Use of flag single short forms combined
specifications = [
CLASP.Flag('--expand', alias: '-x'),
CLASP.Flag('--verbose', alias: '-v'),
CLASP.Flag('--trace-output', aliases: [ '-t', '--trace' ]),
]
argv = %w{ -tvx }
args = CLASP::Arguments.new(argv, specifications)
puts args.flags.size # => 3
puts args..size # => 0
puts args.values.size # => 0
Use of option short form
specifications = [
CLASP.Option('--show-all', alias: '-a'),
]
argv = %w{ -c -a true infile outfile }
args = CLASP::Arguments.new(argv, specifications)
puts args.flags.size # => 1
puts args.flags[0] # => -c
puts args.flags[0].inspect # => #<CLASP::Arguments::FlagArgument:0x007f8593b0ddd8 @arg="-c", @given_index=0, @given_name="-c", @argument_alias=nil, @given_hyphens=1, @given_label="c", @name="-c", @extras={}>
puts args..size # => 1
puts args.[0] # => --show-all
puts args.[0].inspect # => "#<CLASP::Arguments::OptionArgument:0x007f8593b0db80 @arg="-a", @given_index=1, @given_name="-a", @argument_alias=#<CLASP::OptionArgument:0x007f8593b2ea10 @name="--show-all", @aliases=["-a"], @help=nil, @values_range=[], @default_value=nil, @extras={}>, @given_hyphens=1, @given_label="a", @value="true", @name="--show-all", @extras={}>"
puts args.values.size # => 2
puts args.values[0] # => infile
puts args.values[1] # => outfile
Classes of interest
-
::CLASP::Arguments
-
::CLASP::Arguments::FlagArgument
-
::CLASP::Arguments::OptionArgument
Functions of interest
-
::CLASP.show_version()
-
::CLASP#show_version()
-
::CLASP::show_version()
Defined Under Namespace
Modules: CLI_helpers_ Classes: AliasSpecification, Arguments, FlagSpecification, OptionSpecification
Constant Summary collapse
- VERSION =
Current version of the CLASP.Ruby library
'0.19.0'- VERSION_MAJOR =
Major version of the CLASP.Ruby library
VERSION_PARTS_[0]
- VERSION_MINOR =
Minor version of the CLASP.Ruby library
VERSION_PARTS_[1]
- VERSION_REVISION =
Revision version of the CLASP.Ruby library
VERSION_PARTS_[2]
- Alias =
######################################################################## # backwards-compatible
AliasSpecification- Flag =
FlagSpecification- FlagAlias =
FlagSpecification- Option =
OptionSpecification- OptionAlias =
OptionSpecification
Class Method Summary collapse
- .Alias(name, *args) ⇒ Object
-
.Flag(name, options = {}) ⇒ Object
Generator method that obtains a CLASP::FlagSpecification according to the given parameters.
-
.Option(name, options = {}) ⇒ Object
Generator method that obtains a CLASP::OptionSpecification according to the given parameters.
-
.show_usage(specifications, options = {}) ⇒ Object
Displays usage for the program according to the given specifications and options.
-
.show_version(specifications, options = {}) ⇒ Object
Displays version for the program according to the given specifications and options.
Class Method Details
.Alias(name, *args) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/clasp/specifications.rb', line 440 def CLASP.Alias(name, *args) = args.pop if args[-1].is_a?(::Hash) ||= {} if [:alias] aliases = [ [:alias] ] elsif [:aliases] aliases = [:aliases] else aliases = args end CLASP::AliasSpecification.new name, aliases end |
.Flag(name, options = {}) ⇒ Object
Generator method that obtains a CLASP::FlagSpecification according to the given parameters
Signature
-
Parameters:
name- ::String
-
The flag name, e.g. ‘–verbose’
options- ::Hash
-
An options hash, containing any of the following
options:
-
Options:
-
:alias[::String] An alias, e.g. ‘-v’ -
:aliases[::Array] An array of aliases, e.g. [ ‘-v’, ‘-verb’ ] Ignored if:aliasis specified -
:extrasAn application-defined object, usually a hash of custom attributes -
:help[::String] A help string
-
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/clasp/specifications.rb', line 321 def CLASP.Flag(name, = {}) aliases = nil help = nil extras = nil .each do |k, v| case k when Symbol case k when :alias aliases = [ v ] if v when :aliases aliases = v unless aliases when :help help = v when :extras extras = v else raise ArgumentError, "invalid option for flag: '#{k}' => '#{v}'" end else raise ArgumentError, "invalid option type for flag: '#{k}' (#{k.class}) => '#{v}'" end end CLASP::FlagSpecification.new(name, aliases, help, extras) end |
.Option(name, options = {}) ⇒ Object
Generator method that obtains a CLASP::OptionSpecification according to the given parameters
Signature
-
Parameters:
name- ::String
-
The flag name, e.g. ‘–verbose’
options- ::Hash
-
An options hash, containing any of the following
options:
-
Options:
-
:alias[::String] An alias, e.g. ‘-v’ -
:aliases[::Array] An array of aliases, e.g. [ ‘-v’, ‘-verb’ ]. Ignored if:aliasis specified -
:default_valueThe default value for the option -
:default[DEPRECATED] Alternative to:default_value -
:extrasAn application-defined object, usually a hash of custom attributes -
:help[::String] A help string required- boolean
-
Whether the option is required. May be
nilrequired_message- ::String
-
Message to be used when reporting
that a required option is missing. May be
nilin which case a message of the form “<option-name> not specified; use –help for usage”. If begins with the nul character (“0”), then is used in the place of the <option-name> and placed into the rest of the standard form messageextras-
An application-defined additional parameter. If
nil, it is assigned an emptyHash.
-
:values_range[::Array] An array defining the accepted values for the option -
:values[DEPRECATED] Alternative to:values_range
-
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/clasp/specifications.rb', line 388 def CLASP.Option(name, = {}) aliases = nil help = nil values_range = nil default_value = nil required = false = nil extras = nil .each do |k, v| case k when Symbol case k when :alias aliases = [ v ] if v when :aliases aliases = v unless aliases when :help help = v when :values_range, :values values_range = v when :default_value, :default default_value = v when :required required = v when :required_message = v when :extras extras = v else raise ArgumentError, "invalid option for option: '#{k}' => '#{v}'" end else raise ArgumentError, "invalid option type for option: '#{k}' (#{k.class}) => '#{v}'" end end CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, , extras) end |
.show_usage(specifications, options = {}) ⇒ Object
Displays usage for the program according to the given specifications and options
Signature
-
Parameters:
specifications-
(
Array) The arguments array. May not benil. Defaults toARGV.
options-
An options hash, containing any of the following options.
-
Options:
:exit-
a program exit code;
exit()not called if not specified (ornil).
:program_name-
program name; inferred from
$0if not specified.
:stream-
output stream;
$stdoutif not specified.
:suppress_blank_lines_between_options-
does exactly what it says on the tin.
:values-
appends this string to USAGE line if specified.
:flags_and_options-
inserts a custom string instead of the default string
'[ ... flags and options ... ]'.
:info_lines-
inserts 0+ information lines prior to the usage.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/clasp/cli.rb', line 128 def self.show_usage specifications, ={} ||= {} raise ArgumentError, "specifications may not be nil" if specifications.nil? raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select)) constants = CLI_helpers_::Constants specifications.each { |a| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === a } } alias_dups = {} specifications.each { |a| a.aliases.each { |aa| warn "WARNING: alias '#{aa}' is already used for alias '#{a}'" if alias_dups.has_key? aa; alias_dups[aa] = a; } } suppress_blanks = [:suppress_blank_lines_between_options] || ENV['SUPPRESS_BLANK_LINES_BETWEEN_OPTIONS'] stream = [:stream] || $stdout program_name = [:program_name] || File.basename($0) info_lines = [:info_lines] case info_lines when ::Array ; when ::NilClass info_lines = [] else info_lines = [ info_lines ] unless [ :each, :empty? ].all? { |m| info_lines.respond_to? m } end info_lines = info_lines.map do |line| case line when :version CLI_helpers_.generate_version_string_ else line end end values = [:values] || '' values = " #{values}" if !values.empty? && ' ' != values[0] = [:flags_and_options] || ' [ ... flags and options ... ]' = " #{}" if !.empty? && ' ' != [0] # sift the specifications to sort out which are value-option # specifications (VOAs) voas = {} specifications.select { |a| a.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ }.each do |a| a.name =~ /^(-+[a-zA-Z0-3_-]+)[=:](.+)$/ voas[$1] = [] unless voas.has_key? $1 voas[$1] << [ a, $2 ] end fas = {} specifications.select { |a| AliasSpecification === a }.each do |a| fas[a.name] = [] unless fas.has_key? $1 fas[a.name] << a end specifications = specifications.reject { |a| a.name =~ /^-+[a-zA-Z0-3_-]+[=:].+/ } info_lines.each { |info_line| stream.puts info_line } unless info_lines.empty? stream.puts "USAGE: #{program_name}#{}#{values}" stream.puts unless specifications.empty? stream.puts "flags/options:" stream.puts specifications.each do |a| case a when AliasSpecification next when FlagSpecification if fas.has_key? a.name fas[a.name].each do |fa| fa.aliases.each { |al| stream.puts "\t#{al}" } end end a.aliases.each { |al| stream.puts "\t#{al}" } stream.puts "\t#{a.name}" stream.puts "\t\t#{a.help}" when OptionSpecification if voas.has_key? a.name voas[a.name].each do |ar| ar[0].aliases.each { |al| stream.puts "\t#{al} #{ar[0].name}" } end end a.aliases.each { |al| stream.puts "\t#{al} <value>" } stream.puts "\t#{a.name}=<value>" stream.puts "\t\t#{a.help}" unless a.values_range.empty? stream.puts "\t\twhere <value> one of:" a.values_range.each { |v| stream.puts "\t\t\t#{v}" } end end stream.puts unless suppress_blanks end end exit_code = [:exit_code] || [:exit] exit exit_code if exit_code end |
.show_version(specifications, options = {}) ⇒ Object
Displays version for the program according to the given specifications and options
Signature
-
Parameters:
specifications-
(
Array) The arguments array. May not benil. Defaults toARGV.
options-
An options hash, containing any of the following options.
-
Options:
:exit-
a program exit code;
exit()not called if not specified (ornil).
:program_name-
program name; inferred from
$0if not specified.
:stream-
output stream;
$stdoutif not specified.
:version-
an array (of N elements, each of which will be separated by a period ‘.’), or a string. Must be specified if not
:version_major.
:version_major-
a number or string. Only considered and must be specified if
:versionis not.
:version_minor-
a number or string. Only considered if
:versionis not.
:version_revision-
a number or string. Only considered if
:versionis not.
:version_build-
a number or string. Only considered if
:versionis not.
:version_prefix-
optional string to prefix the version number(s).
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/clasp/cli.rb', line 271 def self.show_version specifications, = {} ||= {} raise ArgumentError, "specifications may not be nil" if specifications.nil? raise TypeError, "specifications must be an array or must respond to each, reject and select" unless ::Array === specifications || (specifications.respond_to?(:each) && specifications.respond_to?(:reject) && specifications.respond_to?(:select)) constants = CLI_helpers_::Constants specifications.each { |a| raise ::TypeError, "each element in specifications array must be one of the types #{constants::VALID_ALIAS_TYPES_STRING}" unless constants::VALID_ALIAS_TYPES.any? { |c| c === a } } stream = [:stream] || $stdout version_string = CLI_helpers_.generate_version_string_ stream.puts version_string exit_code = [:exit_code] || [:exit] exit exit_code if exit_code end |