class Geny::CLI

Attributes

column[R]

The column width for help information @return [Integer]

description[R]

A description for your program @return [String]

program_name[R]

The name of your program @return [String]

registry[R]

The registry used for locating commands @return [Registry]

version[R]

The version of your program @return [String]

Public Class Methods

new( registry: Registry.new, version: VERSION, program_name: "geny", description: nil, column: 20 ) click to toggle source

Create a new CLI @param registry [Registry] @param version [String] @param program_name [String] @param description [String] @param column [Integer]

# File lib/geny/cli.rb, line 35
def initialize(
  registry: Registry.new,
  version: VERSION,
  program_name: "geny",
  description: nil,
  column: 20
)
  @registry = registry
  @version = version
  @program_name = program_name
  @description = description
  @column = column
end

Public Instance Methods

abort!(message) click to toggle source

Print an error and abort the program. @param message [String] error message @raise [SystemExit]

# File lib/geny/cli.rb, line 62
def abort!(message)
  color = Pastel.new(enabled: $stdout.tty?)
  ui = Actions::UI.new(color: color)
  ui.abort!(message)
end
run(argv) click to toggle source

Parse arguments and invoke a command @param argv [Array<String>]

# File lib/geny/cli.rb, line 51
def run(argv)
  opts = parser.parse(argv, strategy: :order)
  help! unless opts.command?

  command = registry.find!(opts.command)
  command.run(opts.unused_args)
end

Private Instance Methods

help!() click to toggle source
# File lib/geny/cli.rb, line 87
def help!
  help = parser.help(column: column)
  puts help
  puts help.section("COMMANDS")

  registry.scan.each do |cmd|
    puts help.entry(cmd.name, desc: cmd.description)
  end

  exit
end
parser() click to toggle source
# File lib/geny/cli.rb, line 70
def parser
  @parser ||= Argy.new do |o|
    o.usage "#{program_name} [COMMAND]"
    o.description description
    o.argument :command, desc: "generator to run"

    o.on "-v", "--version", "print version and exit" do
      puts version
      exit
    end

    o.on "-h", "--help", "show this help and exit" do
      help!
    end
  end
end