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
112 113 114 115 116 117 118 |
# File 'lib/bogo-cli/parser.rb', line 112 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.
102 103 104 |
# File 'lib/bogo-cli/parser.rb', line 102 def callable @callable end |
#commands ⇒ Array<Command> (readonly)
Returns list of subcommands.
96 97 98 |
# File 'lib/bogo-cli/parser.rb', line 96 def commands @commands end |
#flags ⇒ Array<Flag> (readonly)
Returns flags for command.
100 101 102 |
# File 'lib/bogo-cli/parser.rb', line 100 def flags @flags end |
#name ⇒ String (readonly)
Returns name of command.
98 99 100 |
# File 'lib/bogo-cli/parser.rb', line 98 def name @name end |
#options ⇒ OpenStruct (readonly)
Returns flag option values.
104 105 106 |
# File 'lib/bogo-cli/parser.rb', line 104 def @options end |
#parser ⇒ OptionParser (readonly)
Returns command parser.
106 107 108 |
# File 'lib/bogo-cli/parser.rb', line 106 def parser @parser end |
Instance Method Details
#command(name, &block) ⇒ Object
Add a new command
153 154 155 156 157 |
# File 'lib/bogo-cli/parser.rb', line 153 def command(name, &block) Command.new(name).load(&block).tap do |c| @commands << c end end |
#description(v = UNSET) ⇒ String
Returns description of command.
160 161 162 163 |
# File 'lib/bogo-cli/parser.rb', line 160 def description(v=UNSET) @description = v unless v == UNSET @description end |
#generate(parents = [], add_help: true) ⇒ Hash<string,OptParser>
Generate command parsers
204 205 206 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 |
# File 'lib/bogo-cli/parser.rb', line 204 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.
177 178 179 |
# File 'lib/bogo-cli/parser.rb', line 177 def help parser.help end |
#load(&block) ⇒ Object
Load a command configuration block
171 172 173 174 |
# File 'lib/bogo-cli/parser.rb', line 171 def load(&block) instance_exec(&block) self end |
#on(short, long, description = UNSET, opts = {}, &block) ⇒ Object
Add a new flag
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bogo-cli/parser.rb', line 126 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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/bogo-cli/parser.rb', line 185 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
166 167 168 |
# File 'lib/bogo-cli/parser.rb', line 166 def run(&block) @callable = block end |
#to_hash ⇒ Object
240 241 242 |
# File 'lib/bogo-cli/parser.rb', line 240 def to_hash .to_h end |