Class: Bogo::Cli::Parser::Command
- Inherits:
-
Object
- Object
- Bogo::Cli::Parser::Command
- Defined in:
- lib/bogo-cli/parser.rb
Instance Attribute Summary collapse
-
#callable ⇒ Proc
readonly
Callable to be executed.
-
#commands ⇒ Array<Command>
readonly
List of subcommands.
-
#flags ⇒ Array<Flag>
readonly
Flags for command.
-
#name ⇒ String
readonly
Name of command.
-
#options ⇒ OpenStruct
readonly
Flag option values.
-
#parser ⇒ OptionParser
readonly
Command parser.
Instance Method Summary collapse
-
#command(name, &block) ⇒ Object
Add a new command.
-
#description(v = UNSET) ⇒ String
Description of command.
-
#generate(parents = [], add_help: true) ⇒ Hash<string,OptParser>
Generate command parsers.
-
#help ⇒ String
Help output.
-
#initialize(name) ⇒ Command
constructor
Create a new command.
-
#load(&block) ⇒ Object
Load a command configuration block.
-
#on(short, long, description = UNSET, opts = {}, &block) ⇒ Object
Add a new flag.
-
#parse(arguments) ⇒ OpenStruct, Array<String>
Parse the arguments.
-
#run(&block) ⇒ Object
Register callable for command.
- #to_hash ⇒ Object
Constructor Details
#initialize(name) ⇒ Command
Create a new command
115 116 117 118 119 120 121 |
# File 'lib/bogo-cli/parser.rb', line 115 def initialize(name) @name = name.to_sym @commands = [] @flags = [] @callable = nil @options = OptionValues.new end |
Instance Attribute Details
#callable ⇒ Proc (readonly)
Returns callable to be executed.
105 106 107 |
# File 'lib/bogo-cli/parser.rb', line 105 def callable @callable end |
#commands ⇒ Array<Command> (readonly)
Returns list of subcommands.
99 100 101 |
# File 'lib/bogo-cli/parser.rb', line 99 def commands @commands end |
#flags ⇒ Array<Flag> (readonly)
Returns flags for command.
103 104 105 |
# File 'lib/bogo-cli/parser.rb', line 103 def flags @flags end |
#name ⇒ String (readonly)
Returns name of command.
101 102 103 |
# File 'lib/bogo-cli/parser.rb', line 101 def name @name end |
#options ⇒ OpenStruct (readonly)
Returns flag option values.
107 108 109 |
# File 'lib/bogo-cli/parser.rb', line 107 def @options end |
#parser ⇒ OptionParser (readonly)
Returns command parser.
109 110 111 |
# File 'lib/bogo-cli/parser.rb', line 109 def parser @parser end |
Instance Method Details
#command(name, &block) ⇒ Object
Add a new command
156 157 158 159 160 |
# File 'lib/bogo-cli/parser.rb', line 156 def command(name, &block) Command.new(name).load(&block).tap do |c| @commands << c end end |
#description(v = UNSET) ⇒ String
Returns description of command.
163 164 165 166 |
# File 'lib/bogo-cli/parser.rb', line 163 def description(v=UNSET) @description = v unless v == UNSET @description end |
#generate(parents = [], add_help: true) ⇒ Hash<string,OptParser>
Generate command parsers
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bogo-cli/parser.rb', line 207 def generate(parents=[], add_help: true) Hash.new.tap { |cmds| @parser = OptionParser.new full_name = parents + [name] parser.program_name = full_name.join(' ') parser. = description unless description == UNSET if add_help parser.on('-h', '--help', "Display help information") do $stderr.puts parser.help exit end end flags.each { |f| if !f.boolean? && f.long_name.end_with?('=') short = "-#{f.short_name}" if f.short_name long = "--#{f.long_name[0, f.long_name.size - 1]} VALUE" else short = "-#{f.short_name}" if f.short_name long = "--#{f.long_name}" end parser.on(short, long, f.description, &f.callable) } commands.map { |c| c.generate(full_name, add_help: add_help) }.inject(cmds) { |memo, list| memo.merge!(list) } parser.subcommands = commands cmds[full_name.join(' ')] = self } end |
#help ⇒ String
Returns help output.
180 181 182 |
# File 'lib/bogo-cli/parser.rb', line 180 def help parser.help end |
#load(&block) ⇒ Object
Load a command configuration block
174 175 176 177 |
# File 'lib/bogo-cli/parser.rb', line 174 def load(&block) instance_exec(&block) self end |
#on(short, long, description = UNSET, opts = {}, &block) ⇒ Object
Add a new flag
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bogo-cli/parser.rb', line 129 def on(short, long, description=UNSET, opts={}, &block) if short.to_s.size > 1 if description == UNSET description = long long = short short = nil elsif description.is_a?(Hash) opts = description description = long long = short short = nil end end Flag.new( short_name: short, long_name: long, description: description, default: opts[:default], callable: block, ).tap do |f| @flags << f end end |
#parse(arguments) ⇒ OpenStruct, Array<String>
Parse the arguments
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/bogo-cli/parser.rb', line 188 def parse(arguments) raise "Must call #generate before #parse" if parser.nil? flags.each do |f| next if f.default.nil? .set_default(f.option_name, f.default) end init = OpenStruct.new parser.parse!(arguments, into: init) init.each_pair do |k, v| .assign(k, v) end [, arguments] end |
#run(&block) ⇒ Object
Register callable for command
169 170 171 |
# File 'lib/bogo-cli/parser.rb', line 169 def run(&block) @callable = block end |
#to_hash ⇒ Object
243 244 245 |
# File 'lib/bogo-cli/parser.rb', line 243 def to_hash .to_h end |