Module: CommandKit::Arguments
- Extended by:
- ModuleMethods
- Defined in:
- lib/command_kit/arguments.rb,
lib/command_kit/arguments/argument.rb,
lib/command_kit/arguments/argument_value.rb
Overview
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
Multi-line descriptions
argument :arg, desc: [
'line1',
'line2',
'...'
]
Defined Under Namespace
Modules: ClassMethods, ModuleMethods Classes: Argument, ArgumentValue
Constant Summary
Constants included from Printing
Instance Attribute Summary
Attributes included from CommandName
Instance Method Summary collapse
-
#help ⇒ Object
Calls the superclass'es
#help
method, if it's defined, then calls #help_arguments. -
#help_arguments ⇒ Object
Prints any defined arguments, along with the usual
--help
information. -
#main(argv = []) ⇒ Integer
Checks the minimum/maximum number of arguments, then calls the superclass'es
#main
.
Methods included from ModuleMethods
Methods included from Printing
#print_error, #print_exception
Methods included from Stdio
#abort, #gets, #initialize, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Methods included from Help::ModuleMethods
Methods included from Main
Methods included from Main::ModuleMethods
Methods included from Usage
Methods included from Usage::ModuleMethods
Methods included from CommandName
Methods included from CommandName::ModuleMethods
Instance Method Details
#help ⇒ Object
Calls the superclass'es #help
method, if it's defined, then calls
#help_arguments.
230 231 232 233 234 |
# File 'lib/command_kit/arguments.rb', line 230 def help super help_arguments end |
#help_arguments ⇒ Object
Prints any defined arguments, along with the usual --help
information.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/command_kit/arguments.rb', line 202 def help_arguments unless (arguments = self.class.arguments).empty? puts puts 'Arguments:' arguments.each_value do |arg| case arg.desc when Array arg.desc.each_with_index do |line,index| if index == 0 puts " #{arg.usage.ljust(33)}#{line}" else puts " #{' '.ljust(33)}#{line}" end end else puts " #{arg.usage.ljust(33)}#{arg.desc}" end end end end |
#main(argv = []) ⇒ Integer
Checks the minimum/maximum number of arguments, then calls the
superclass'es #main
.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/command_kit/arguments.rb', line 179 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 |