class Drudge::Command

Describes a command and helps executing it

The command is defined by a name and a list of arguments (see class Param). The body of the command is a lambda that accepts exactly the arguments

Attributes

body[R]

The command’s body

desc[R]

An optional short desicription of the command

name[R]

The name of the command

params[R]

The list of parameters

Public Class Methods

new(name, params = [], body, desc: "") click to toggle source

Initializes a new command

# File lib/drudge/command.rb, line 27
def initialize(name, params = [], body, desc: "")
  @name   = name.to_sym
  @params = params
  @body   = body

  @desc   = desc
end

Public Instance Methods

argument_parser() click to toggle source

creates an argument parser for the command

# File lib/drudge/command.rb, line 43
def argument_parser
  end_of_args = eos("extra command line arguments provided")

  parser = params.reverse.reduce(end_of_args) do |rest, param|
    p = param.argument_parser

    case
    when param.optional? then ((p > rest) | rest).describe("[#{p}] #{rest}")
    when param.splatt? then (p.repeats(till: rest) > rest).describe("[#{p} ...] #{rest}")
    else p > rest
    end
  end
end
dispatch(*args) click to toggle source

runs the command

# File lib/drudge/command.rb, line 36
def dispatch(*args)
  @body.call(*args)
rescue ArgumentError => e
  raise CommandArgumentError.new(name), e.message
end