Module: Kube::CLI
- Defined in:
- lib/kube/cli.rb
Class Method Summary collapse
-
.commands ⇒ Object
All registered command names.
-
.lookup(name) ⇒ Object
Look up a registered subcommand handler by name.
- .print_help(io = $stdout) ⇒ Object
-
.register(name, handler, description: nil) ⇒ Object
Register a subcommand.
-
.run(argv = ARGV) ⇒ Object
Run the CLI.
Class Method Details
.commands ⇒ Object
All registered command names.
26 27 28 |
# File 'lib/kube/cli.rb', line 26 def self.commands @commands.dup end |
.lookup(name) ⇒ Object
Look up a registered subcommand handler by name.
20 21 22 23 |
# File 'lib/kube/cli.rb', line 20 def self.lookup(name) entry = @commands[name.to_s] entry&.fetch(:handler) end |
.print_help(io = $stdout) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kube/cli.rb', line 52 def self.print_help(io = $stdout) io.puts "Usage: kube <command> [args...]" io.puts io.puts "Commands:" max_width = @commands.keys.map(&:length).max || 0 @commands.sort_by { |name, _| name }.each do |name, entry| if entry[:description] io.puts " %-#{max_width}s %s" % [name, entry[:description]] else io.puts " #{name}" end end io.puts io.puts "Run 'kube <command> --help' for more information on a command." end |
.register(name, handler, description: nil) ⇒ Object
15 16 17 |
# File 'lib/kube/cli.rb', line 15 def self.register(name, handler, description: nil) @commands[name.to_s] = { handler: handler, description: description } end |
.run(argv = ARGV) ⇒ Object
Run the CLI. Parses the first argument as the subcommand, passes the rest to the handler.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kube/cli.rb', line 32 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 |