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 Attribute Summary collapse
-
#command ⇒ Cri::Command
readonly
The built command.
Instance Method Summary collapse
-
#aliases(*args) ⇒ void
Sets the command aliases.
-
#be_hidden ⇒ void
Marks the command as hidden.
-
#default_subcommand(name) ⇒ void
Sets the name of the default subcommand, i.e.
-
#description(arg) ⇒ void
Sets the command description.
-
#flag(short, long, desc, params = {}, &block) ⇒ void
(also: #forbidden)
Adds a new option with a forbidden argument to the command.
-
#initialize(command = nil) ⇒ CommandDSL
constructor
Creates a new DSL, intended to be used for building a single command.
-
#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, params = {}, &block) ⇒ void
Adds a new option with an optional argument to the command.
-
#required(short, long, desc, params = {}, &block) ⇒ void
Adds a new option with a required argument to the command.
-
#run {|opts, args| ... } ⇒ void
Sets the run block to the given block.
-
#runner(klass) ⇒ void
Defines the runner class for this command.
-
#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
Creates a new DSL, intended to be used for building a single command. A Cri::CommandDSL instance is not reusable; create a new instance if you want to build another command.
14 15 16 |
# File 'lib/cri/command_dsl.rb', line 14 def initialize(command = nil) @command = command || Cri::Command.new end |
Instance Attribute Details
#command ⇒ Cri::Command (readonly)
Returns The built command
6 7 8 |
# File 'lib/cri/command_dsl.rb', line 6 def command @command end |
Instance Method Details
#aliases(*args) ⇒ void
This method returns an undefined value.
Sets the command aliases.
59 60 61 |
# File 'lib/cri/command_dsl.rb', line 59 def aliases(*args) @command.aliases = args.flatten.map(&:to_s) end |
#be_hidden ⇒ void
This method returns an undefined value.
Marks the command as hidden. Hidden commands do not show up in the list of
subcommands of the parent command, unless --verbose is passed (or
:verbose => true
is passed to the Cri::Command#help method). This can
be used to mark commands as deprecated.
97 98 99 |
# File 'lib/cri/command_dsl.rb', line 97 def be_hidden @command.hidden = true end |
#default_subcommand(name) ⇒ void
This method returns an undefined value.
Sets the name of the default subcommand, i.e. the subcommand that will
be executed if no subcommand is explicitly specified. This is nil
by
default, and will typically only be set for the root command.
41 42 43 |
# File 'lib/cri/command_dsl.rb', line 41 def default_subcommand(name) @command.default_subcommand_name = name end |
#description(arg) ⇒ void
This method returns an undefined value.
Sets the command description.
77 78 79 |
# File 'lib/cri/command_dsl.rb', line 77 def description(arg) @command.description = arg end |
#flag(short, long, desc, params = {}, &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.
188 189 190 191 |
# File 'lib/cri/command_dsl.rb', line 188 def flag(short, long, desc, params = {}, &block) params = params.merge(argument: :forbidden) option(short, long, desc, params, &block) end |
#name(arg) ⇒ void
This method returns an undefined value.
Sets the command name.
50 51 52 |
# File 'lib/cri/command_dsl.rb', line 50 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.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cri/command_dsl.rb', line 120 def option(short, long, desc, params = {}, &block) requiredness = params.fetch(:argument, :forbidden) multiple = params.fetch(:multiple, false) hidden = params.fetch(:hidden, false) default = params.fetch(:default, nil) if short.nil? && long.nil? raise ArgumentError, 'short and long options cannot both be nil' end if default && requiredness == :forbidden raise ArgumentError, 'a default value cannot be specified for flag options' end @command.option_definitions << { short: short.nil? ? nil : short.to_s, long: long.nil? ? nil : long.to_s, desc: desc, argument: requiredness, multiple: multiple, block: block, hidden: hidden, default: default, } end |
#optional(short, long, desc, params = {}, &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.
212 213 214 215 |
# File 'lib/cri/command_dsl.rb', line 212 def optional(short, long, desc, params = {}, &block) params = params.merge(argument: :optional) option(short, long, desc, params, &block) end |
#required(short, long, desc, params = {}, &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.
165 166 167 168 |
# File 'lib/cri/command_dsl.rb', line 165 def required(short, long, desc, params = {}, &block) params = params.merge(argument: :required) option(short, long, desc, params, &block) end |
#run {|opts, args| ... } ⇒ void
229 230 231 232 233 234 235 236 |
# File 'lib/cri/command_dsl.rb', line 229 def run(&block) unless [2, 3].include?(block.arity) raise ArgumentError, 'The block given to Cri::Command#run expects two or three args' end @command.block = block end |
#runner(klass) ⇒ void
246 247 248 249 250 |
# File 'lib/cri/command_dsl.rb', line 246 def runner(klass) run do |opts, args, cmd| klass.new(opts, args, cmd).call end 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.
26 27 28 29 30 31 32 |
# File 'lib/cri/command_dsl.rb', line 26 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.
68 69 70 |
# File 'lib/cri/command_dsl.rb', line 68 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.
87 88 89 |
# File 'lib/cri/command_dsl.rb', line 87 def usage(arg) @command.usage = arg end |