module CLI
Definitions
def self.register(name, handler, description: nil)
Register a subcommand.
Kube::CLI.register("cluster", handler)
+name+ - the subcommand name (String) +handler+ - any object that responds to .call(argv) where argv is the remaining ARGV after the subcommand +description+ - short one-line description for help output
Implementation
def self.register(name, handler, description: nil)
@commands[name.to_s] = { handler: handler, description: description }
end
def self.lookup(name)
Look up a registered subcommand handler by name.
Implementation
def self.lookup(name)
entry = @commands[name.to_s]
entry&.fetch(:handler)
end
def self.commands
All registered command names.
Implementation
def self.commands
@commands.dup
end
def self.run(argv = ARGV)
Run the CLI. Parses the first argument as the subcommand, passes the rest to the handler.
Implementation
def self.run(argv = ARGV)
subcommand = argv.shift
if subcommand.nil? || subcommand == "help" || subcommand == "--help" || subcommand == "-h"
print_help
return
end
handler = lookup(subcommand)
if handler.nil?
$stderr.puts "kube: unknown command '#{subcommand}'"
$stderr.puts
print_help($stderr)
exit 1
end
handler.call(argv)
end