Module: CommandKit::Arguments

Extended by:
ModuleMethods
Includes:
Help, Main, Printing, Usage
Included in:
Command, Options
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

Printing::EOL

Instance Attribute Summary

Attributes included from CommandName

#command_name

Instance Method Summary collapse

Methods included from ModuleMethods

included

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

#included

Methods included from Main

#run

Methods included from Main::ModuleMethods

#included

Methods included from Usage

#help_usage, #usage

Methods included from Usage::ModuleMethods

#included

Methods included from CommandName

#initialize

Methods included from CommandName::ModuleMethods

#included

Instance Method Details

#helpObject

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_argumentsObject

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.

Parameters:

  • argv (Array<String>) (defaults to: [])

    The arguments passed to the program.

Returns:

  • (Integer)

    The exit status code. If too few or too many arguments are given, then an error message is printed and 1 is returned.



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