Class: Cri::CommandDSL
- Inherits:
-
Object
- Object
- Cri::CommandDSL
- Defined in:
- lib/cri/command_dsl.rb
Overview
The command DSL is a class that is used for building and modifying commands.
Instance Method Summary collapse
-
#aliases(*args) ⇒ void
Sets the command aliases.
-
#command ⇒ Cri::Command
The built command.
-
#description(arg) ⇒ void
Sets the command description.
-
#flag(short, long, desc, &block) ⇒ void
(also: #forbidden)
Adds a new option with a forbidden argument to the command.
-
#initialize(command = nil) ⇒ CommandDSL
constructor
A new instance of CommandDSL.
-
#name(arg) ⇒ void
Sets the command name.
-
#option(short, long, desc, params = {}, &block) ⇒ void
(also: #opt)
Adds a new option to the command.
-
#optional(short, long, desc, &block) ⇒ void
Adds a new option with an optional argument to the command.
-
#required(short, long, desc, &block) ⇒ void
Adds a new option with a required argument to the command.
-
#run(&block) ⇒ void
Sets the run block to the given block.
-
#subcommand(command = nil, &block) ⇒ void
Adds a subcommand to the current command.
-
#summary(arg) ⇒ void
Sets the command summary.
-
#usage(arg) ⇒ void
Sets the command usage.
Constructor Details
#initialize(command = nil) ⇒ CommandDSL
Returns a new instance of CommandDSL.
11 12 13 |
# File 'lib/cri/command_dsl.rb', line 11 def initialize(command=nil) @command = command || Cri::Command.new end |
Instance Method Details
#aliases(*args) ⇒ void
This method returns an undefined value.
Sets the command aliases.
50 51 52 |
# File 'lib/cri/command_dsl.rb', line 50 def aliases(*args) @command.aliases = args.flatten.map { |a| a.to_s } end |
#command ⇒ Cri::Command
Returns The built command.
16 17 18 |
# File 'lib/cri/command_dsl.rb', line 16 def command @command end |
#description(arg) ⇒ void
This method returns an undefined value.
Sets the command description.
68 69 70 |
# File 'lib/cri/command_dsl.rb', line 68 def description(arg) @command.description = arg end |
#flag(short, long, desc, &block) ⇒ void Also known as: forbidden
This method returns an undefined value.
Adds a new option with a forbidden argument to the command. If a block is given, it will be executed when the option is successfully parsed.
129 130 131 |
# File 'lib/cri/command_dsl.rb', line 129 def flag(short, long, desc, &block) self.add_option(short, long, desc, :forbidden, block) end |
#name(arg) ⇒ void
This method returns an undefined value.
Sets the command name.
41 42 43 |
# File 'lib/cri/command_dsl.rb', line 41 def name(arg) @command.name = arg end |
#option(short, long, desc, params = {}, &block) ⇒ void Also known as: opt
This method returns an undefined value.
Adds a new option to the command. If a block is given, it will be executed when the option is successfully parsed.
95 96 97 98 |
# File 'lib/cri/command_dsl.rb', line 95 def option(short, long, desc, params={}, &block) requiredness = params[:argument] || :forbidden self.add_option(short, long, desc, requiredness, block) end |
#optional(short, long, desc, &block) ⇒ void
This method returns an undefined value.
Adds a new option with an optional argument to the command. If a block is given, it will be executed when the option is successfully parsed.
146 147 148 |
# File 'lib/cri/command_dsl.rb', line 146 def optional(short, long, desc, &block) self.add_option(short, long, desc, :optional, block) end |
#required(short, long, desc, &block) ⇒ void
This method returns an undefined value.
Adds a new option with a required argument to the command. If a block is given, it will be executed when the option is successfully parsed.
113 114 115 |
# File 'lib/cri/command_dsl.rb', line 113 def required(short, long, desc, &block) self.add_option(short, long, desc, :required, block) end |
#run(&block) ⇒ void
This method returns an undefined value.
Sets the run block to the given block. The given block should have two or three arguments (options, arguments, and optionally the command).
154 155 156 157 158 159 160 161 |
# File 'lib/cri/command_dsl.rb', line 154 def run(&block) unless block.arity != 2 || block.arity != 3 raise ArgumentError, "The block given to Cri::Command#run expects two or three args" end @command.block = block end |
#subcommand(command = nil, &block) ⇒ void
This method returns an undefined value.
Adds a subcommand to the current command. The command can either be given explicitly, or a block can be given that defines the command.
28 29 30 31 32 33 34 |
# File 'lib/cri/command_dsl.rb', line 28 def subcommand(command=nil, &block) if command.nil? command = Cri::Command.define(&block) end @command.add_command(command) end |
#summary(arg) ⇒ void
This method returns an undefined value.
Sets the command summary.
59 60 61 |
# File 'lib/cri/command_dsl.rb', line 59 def summary(arg) @command.summary = arg end |
#usage(arg) ⇒ void
This method returns an undefined value.
Sets the command usage. The usage should not include the “usage:” prefix, nor should it include the command names of the supercommand.
78 79 80 |
# File 'lib/cri/command_dsl.rb', line 78 def usage(arg) @command.usage = arg end |