Class: Commander
- Inherits:
-
Object
- Object
- Commander
- Defined in:
- lib/nub/commander.rb
Overview
An implementation of git like command syntax for ruby applications: see github.com/phR0ze/ruby-nub
Defined Under Namespace
Classes: OptionMatch
Instance Attribute Summary collapse
-
#banner ⇒ String
readonly
Returns banner string.
-
#cmds ⇒ Object
Returns the value of attribute cmds.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash like accessor for checking if a command or option is set.
-
#add(cmd, desc, nodes: [], examples: nil) ⇒ Object
Add a command to the command list.
-
#add_global(key, desc, type: nil, required: false, allowed: {}) ⇒ Object
Add global options (any option coming before all commands).
-
#help ⇒ String
Return the app’s help string.
-
#initialize(app: nil, version: nil, examples: nil) ⇒ Commander
constructor
Initialize the commands for your application.
-
#key?(key) ⇒ Boolean
Test if the key exists.
-
#parse! ⇒ Object
Construct the command line parser and parse.
Constructor Details
#initialize(app: nil, version: nil, examples: nil) ⇒ Commander
Initialize the commands for your application
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/nub/commander.rb', line 149 def initialize(app:nil, version:nil, examples:nil) @k = OpenStruct.new({ global: 'global' }) @app = app @app_default = Sys.caller_filename @version = version @examples = examples @just = 40 # Regexps @short_regex = /^(-\w).*$/ @long_regex = /(--[\w\-]+)(=.+)*$/ @value_regex = /.*=(.*)$/ # Command line expression results # {command_name => {}} @cmds = {} # Configuration - ordered list of commands @config = [] # Configure default global options add_global('-h|--help', 'Print command/options help') end |
Instance Attribute Details
#banner ⇒ String (readonly)
Returns banner string
229 230 231 |
# File 'lib/nub/commander.rb', line 229 def @banner end |
#cmds ⇒ Object
Returns the value of attribute cmds.
143 144 145 |
# File 'lib/nub/commander.rb', line 143 def cmds @cmds end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
141 142 143 |
# File 'lib/nub/commander.rb', line 141 def config @config end |
Instance Method Details
#[](key) ⇒ Object
Hash like accessor for checking if a command or option is set
177 178 179 |
# File 'lib/nub/commander.rb', line 177 def [](key) return @cmds[key] if @cmds[key] end |
#add(cmd, desc, nodes: [], examples: nil) ⇒ Object
Add a command to the command list
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/nub/commander.rb', line 191 def add(cmd, desc, nodes:[], examples:nil) Log.die("'#{@k.global}' is a reserved command name") if cmd == @k.global Log.die("'#{cmd}' already exists") if @config.any?{|x| x.name == cmd} Log.die("'help' is a reserved option name") if nodes.any?{|x| x.class == Option && !x.key.nil? && x.key.include?('help')} Log.die("command names must be pure lowercase letters or hypen") if cmd =~ /[^a-z-]/ # Validate sub command key words validate_subcmd = ->(subcmd){ subcmd.nodes = [] if !subcmd.nodes Log.die("'#{@k.global}' is a reserved command name") if subcmd.name == @k.global Log.die("'help' is a reserved option name") if subcmd.nodes.any?{|x| x.class == Option && !x.key.nil? && x.key.include?('help')} Log.die("command names must be pure lowercase letters or hypen") if subcmd.name =~ /[^a-z-]/ subcmd.nodes.select{|x| x.class != Option}.each{|x| validate_subcmd.(x)} } nodes.select{|x| x.class != Option}.each{|x| validate_subcmd.(x)} @config << add_cmd(cmd, desc, nodes, examples:examples) end |
#add_global(key, desc, type: nil, required: false, allowed: {}) ⇒ Object
Add global options (any option coming before all commands)
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/nub/commander.rb', line 216 def add_global(key, desc, type:nil, required:false, allowed:{}) = [Option.new(key, desc, type:type, required:required, allowed:allowed)] # Aggregate global options if (global = @config.find{|x| x.name == @k.global}) global.nodes.each{|x| << x} @config.reject!{|x| x.name == @k.global} end @config << add_cmd(@k.global, 'Global options:', ) end |
#help ⇒ String
Return the app’s help string
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/nub/commander.rb', line 237 def help # Global help help = @app.nil? ? "" : "#{}\n" if !@examples.nil? && !@examples.empty? newline = @examples.strip_color[-1] != "\n" ? "\n" : "" help += "Examples:\n#{@examples}\n#{newline}" end app = @app || @app_default help += "Usage: ./#{app} [commands] [options]\n" help += @config.find{|x| x.name == @k.global}.help help += "COMMANDS:\n" @config.select{|x| x.name != @k.global}.each{|x| help += " #{x.name.ljust(@just)}#{x.desc}\n" } help += "\nsee './#{app} COMMAND --help' for specific command help\n" return help end |
#key?(key) ⇒ Boolean
Test if the key exists
182 183 184 |
# File 'lib/nub/commander.rb', line 182 def key?(key) return @cmds.key?(key) end |
#parse! ⇒ Object
Construct the command line parser and parse
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/nub/commander.rb', line 256 def parse! # Clear out the previous run every time, in case run more than once @cmds = {} # Set help if nothing was given ARGV.clear and ARGV << '-h' if ARGV.empty? # Parse commands recursively move_globals_to_front! while (cmd = @config.find{|x| x.name == ARGV.first}) ARGV.shift && parse_commands(cmd, nil, @config.select{|x| x.name != cmd.name}, ARGV, @cmds) end # Ensure specials (global) are always set @cmds[:global] = {} if !@cmds[:global] # Ensure all options were consumed Log.die("invalid options #{ARGV}") if ARGV.any? # Print banner on success puts() if @app end |