Module: CommandKit::Arguments
- Extended by:
- ModuleMethods
- Included in:
- Command
- 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
Defined Under Namespace
Modules: ClassMethods, ModuleMethods Classes: Argument, ArgumentValue
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 Help::ModuleMethods
Methods included from Main
Methods included from Main::ModuleMethods
Instance Method Details
#help ⇒ Object
Calls the superclass'es #help
method, if it's defined, then calls
#help_arguments.
201 202 203 204 205 |
# File 'lib/command_kit/arguments.rb', line 201 def help super help_arguments end |
#help_arguments ⇒ Object
Prints any defined arguments, along with the usual --help
information.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/command_kit/arguments.rb', line 184 def help_arguments unless (arguments = self.class.arguments).empty? puts puts 'Arguments:' self.class.arguments.each_value do |arg| puts " #{arg.usage.ljust(33)}#{arg.desc}" end end end |
#main(argv = []) ⇒ Integer
Checks the minimum/maximum number of arguments, then calls the
superclass'es #main
.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# 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 |