class Rodish::DSL

  1. lib/rodish/dsl.rb
  2. lib/rodish/plugins/help_order.rb
  3. lib/rodish/processor.rb
  4. show all
Superclass: Object

The Rodish::DSL class implements Rodish’s DSL. Blocks passed to Rodish.processor and on/run_on blocks inside those blocks evaluated in the context of an instance of Rodish::DSL.

Each Rodish::DSL instance is bound to a single Rodish::Command and allows the DSL to modify the state of the command.

Methods

Public Class

  1. command
  2. new

Public Instance

  1. args
  2. autoload_subcommand_dir
  3. banner
  4. desc
  5. on
  6. options
  7. run

Public Class methods

command(command_path, &block)

Create a new command with the given path and evaluate the given block in the context of a new instance using that command.

[show source]
   # File lib/rodish/dsl.rb
19 def self.command(command_path, &block)
20   command = self::Command.new(command_path)
21   new(command).instance_exec(&block) if block
22   command
23 end
new(command)
[show source]
   # File lib/rodish/dsl.rb
25 def initialize(command)
26   @command = command
27 end

Public Instance methods

args(args)

Set the number of arguments supported by this command. The default is 0. To support a fixed number of arguments, pass an Integer. To support a variable number of arguments, pass a Range.

[show source]
   # File lib/rodish/dsl.rb
59 def args(args)
60   @command.num_args = args
61 end
autoload_subcommand_dir(dir)

Autoload subcommands from the given directory. Filenames ending in .rb in this directory should be valid subcommands, and requiring the related file should load the subcommand.

You can use this so that your argv parser does not need to load code not needed to support processing the command.

[show source]
   # File lib/rodish/dsl.rb
69 def autoload_subcommand_dir(dir)
70   _autoload_subcommand_dir(@command.subcommands, dir)
71 end
banner(banner)

Set the banner for the command execution and subcommand usage.

[show source]
   # File lib/rodish/dsl.rb
35 def banner(banner)
36   @command.banner = banner
37 end
desc(description)

Set the description for the command.

[show source]
   # File lib/rodish/dsl.rb
30 def desc(description)
31   @command.desc = description
32 end
on(command_name, &block)

Create a new subcommand with the given name and yield to the block to configure the subcommand.

[show source]
   # File lib/rodish/dsl.rb
75 def on(command_name, &block)
76   _on(@command.subcommands, command_name, &block)
77 end
options(banner, key: nil, &block)

Set the option parser for the command to based on the provided block, which is executed in the context of a new instance of Rodish::OptionParser. These options are parsed for execuction of both subcommands and the current command.

The banner argument is required and sets the usage string for the command.

If key is given, parsed options will be placed in a subhash using that key.

[show source]
   # File lib/rodish/dsl.rb
49 def options(banner, key: nil, &block)
50   @command.banner = banner
51   @command.option_key = key
52   @command.option_parser = create_option_parser(&block)
53 end
run(&block)

Set the block to run for subcommand execution. Commands should have subcommands and/or a run block, otherwise it is not possible to use the command successfully.

[show source]
   # File lib/rodish/dsl.rb
82 def run(&block)
83   @command.run_block = block
84 end