module CommandKit::Arguments
Provides a thin DSL for defining arguments as attributes.
## Examples
include CommandKit::Arguments argument :output, desc: 'The output file' argument :input, desc: 'The input file(s)' def run(output,input) end
### Optional Arguments
argument :dir, required: false, desc: 'Can be omitted' def run(dir=nil) end
### Repeating Arguments
argument :files, repeats: true, desc: 'Can be repeated one or more times' def run(*files) end
### Optional Repeating Arguments
argument :files, required: true, repeats: true, desc: 'Can be repeated one or more times' def run(*files) end
Public Instance Methods
help()
click to toggle source
Calls the superclass'es `#help` method, if it's defined, then calls {#help_arguments}.
@api public
Calls superclass method
CommandKit::Help#help
# File lib/command_kit/arguments.rb, line 201 def help super help_arguments end
help_arguments()
click to toggle source
Prints any defined arguments, along with the usual `–help` information.
@api semipublic
# File lib/command_kit/arguments.rb, line 184 def help_arguments unless (arguments = self.class.arguments).empty? puts puts 'Arguments:' arguments.each_value do |arg| puts " #{arg.usage.ljust(33)}#{arg.desc}" end end end
main(argv=[])
click to toggle source
Checks the minimum/maximum number of arguments, then calls the superclass'es `#main`.
@param [Array<String>] argv
The arguments passed to the program.
@return [Integer]
The exit status code. If too few or too many arguments are given, then an error message is printed and `1` is returned.
@api public
Calls superclass method
CommandKit::Main#main
# File lib/command_kit/arguments.rb, line 161 def main(argv=[]) required_args = self.class.arguments.each_value.count(&:required?) optional_args = self.class.arguments.each_value.count(&:optional?) has_repeats_arg = self.class.arguments.each_value.any?(&:repeats?) if argv.length < required_args print_error("insufficient number of arguments.") help_usage return 1 elsif argv.length > (required_args + optional_args) && !has_repeats_arg print_error("too many arguments given") help_usage return 1 end super(argv) end