Class: Evoke::Task

Inherits:
Object
  • Object
show all
Extended by:
Comment, Parameters
Defined in:
lib/evoke/task.rb

Class Method Summary collapse

Methods included from Comment

class_comment

Methods included from Parameters

optional_parameters, parameter_names, parameter_size, required_parameters, select_parameters_by_type

Class Method Details

.desc(value) ⇒ String

Note:

All descriptions end with a period. One will be added if missing.

A short, one line description of the task.

This message will be printed to the console when evoke is called without any arguments.

Parameters:

  • value (String)

    The description. Keep it short!

Returns:

  • (String)

    The supplied description.



36
37
38
39
# File 'lib/evoke/task.rb', line 36

def desc(value)
  value += '.' unless value.end_with?('.')
  @desc = value
end

Prints the syntax usage for this task to the console.



21
22
23
24
25
# File 'lib/evoke/task.rb', line 21

def print_syntax
  params = parameter_names(instance_method(:invoke)).join(' ')
  $stdout.puts "Usage: evoke #{name.underscore} #{params}"
  $stdout.puts "\n#{syntax}"
end

Prints the name and description of this task to the console.

Parameters:

  • name_col_size (Integer)

    The size of the name column.



11
12
13
14
15
16
17
# File 'lib/evoke/task.rb', line 11

def print_usage(name_col_size)
  description = "#{@desc || class_comment}".split("\n")[0]
  description ||= 'No description available.'

  $stdout.print name.underscore.ljust(name_col_size)
  $stdout.puts "# #{description}"
end

.syntax(value = nil) ⇒ String

Describes the syntax of the task.

The syntax is displayed to the user when they call ‘evoke help` for this task. This can be a detailed, multi-line description of how to use the task. By default the comment before the task’s class will be used.

Parameters:

  • value (String) (defaults to: nil)

    The details on how to use the task.

Returns:

  • (String)

    The syntax - this method is both a getter and setter.



49
50
51
52
53
54
55
56
57
# File 'lib/evoke/task.rb', line 49

def syntax(value=nil)
  if value.nil?
    @syntax ||= class_comment
  else
    @syntax = value unless value.nil?
  end

  @syntax
end

.validate_arguments(arguments) ⇒ Object

Ensures that the task’s #invoke method has the same amount of arguments as the user supplied on the command-line. If not, an error message is printed to STDERR and Evoke is terminated with an exit-status of 1.

Parameters:

  • arguments (Array)

    The arguments to validate.

Returns:

  • nil if the validation passes.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/evoke/task.rb', line 66

def validate_arguments(arguments)
  invoke_method = instance_method(:invoke)
  min, max = parameter_size(invoke_method)

  size = Array(arguments).size
  return if size >= min && size <= max

  e_size = min == max ? min : "#{min}..#{max}"

  $stderr.print 'Wrong number of arguments. '
  $stderr.print "Received #{size} instead of #{e_size}.\n"

  exit(1)
end