Class: Rodish::DSL
- Inherits:
-
Object
- Object
- Rodish::DSL
- Defined in:
- lib/rodish/dsl.rb
Overview
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.
Class Method Summary collapse
-
.command(command_path, &block) ⇒ Object
Create a new command with the given path and evaluate the given block in the context of a new instance using that command.
Instance Method Summary collapse
-
#args(args) ⇒ Object
Set the number of arguments supported by this command.
-
#autoload_subcommand_dir(dir) ⇒ Object
Autoload subcommands from the given directory.
-
#banner(banner) ⇒ Object
Set the banner for the command execution and subcommand usage.
-
#desc(description) ⇒ Object
Set the description for the command.
-
#initialize(command) ⇒ DSL
constructor
A new instance of DSL.
-
#on(command_name, &block) ⇒ Object
Create a new subcommand with the given name and yield to the block to configure the subcommand.
-
#options(banner, key: nil, &block) ⇒ Object
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.
-
#run(&block) ⇒ Object
Set the block to run for subcommand execution.
Constructor Details
#initialize(command) ⇒ DSL
Returns a new instance of DSL.
25 26 27 |
# File 'lib/rodish/dsl.rb', line 25 def initialize(command) @command = command end |
Class Method Details
.command(command_path, &block) ⇒ Object
Create a new command with the given path and evaluate the given block in the context of a new instance using that command.
19 20 21 22 23 |
# File 'lib/rodish/dsl.rb', line 19 def self.command(command_path, &block) command = self::Command.new(command_path) new(command).instance_exec(&block) if block command end |
Instance Method Details
#args(args) ⇒ Object
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.
59 60 61 |
# File 'lib/rodish/dsl.rb', line 59 def args(args) @command.num_args = args end |
#autoload_subcommand_dir(dir) ⇒ Object
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.
69 70 71 |
# File 'lib/rodish/dsl.rb', line 69 def autoload_subcommand_dir(dir) _autoload_subcommand_dir(@command.subcommands, dir) end |
#banner(banner) ⇒ Object
Set the banner for the command execution and subcommand usage.
35 36 37 |
# File 'lib/rodish/dsl.rb', line 35 def () @command. = end |
#desc(description) ⇒ Object
Set the description for the command.
30 31 32 |
# File 'lib/rodish/dsl.rb', line 30 def desc(description) @command.desc = description end |
#on(command_name, &block) ⇒ Object
Create a new subcommand with the given name and yield to the block to configure the subcommand.
75 76 77 |
# File 'lib/rodish/dsl.rb', line 75 def on(command_name, &block) _on(@command.subcommands, command_name, &block) end |
#options(banner, key: nil, &block) ⇒ Object
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.
49 50 51 52 53 |
# File 'lib/rodish/dsl.rb', line 49 def (, key: nil, &block) @command. = @command.option_key = key @command.option_parser = create_option_parser(&block) end |
#run(&block) ⇒ Object
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.
82 83 84 |
# File 'lib/rodish/dsl.rb', line 82 def run(&block) @command.run_block = block end |