Class: FlatKit::Cli
- Inherits:
-
Object
- Object
- FlatKit::Cli
- Defined in:
- lib/flat_kit/cli.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #parser ⇒ Object
- #run(argv: ARGV, env: ENV) ⇒ Object
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
8 9 10 |
# File 'lib/flat_kit/cli.rb', line 8 def initialize @parser = nil end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/flat_kit/cli.rb', line 6 def @options end |
Class Method Details
.commands_banner ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/flat_kit/cli.rb', line 42 def self. sorted_commands = FlatKit::Command.children.sort_by{ |c| c.name } left_width = sorted_commands.map { |c| c.name.length }.sort.last = StringIO.new .puts .puts "Commands:" .puts sorted_commands.each do |command| .puts " #{command.name.ljust(left_width)} #{command.description}" end .string end |
Instance Method Details
#parser ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/flat_kit/cli.rb', line 12 def parser @parser ||= ::Optimist::Parser.new do version ::FlatKit::VERSION "fk v#{self.version}" <<~USAGE Usage: fk <command> [<args>...] fk [options] USAGE <<~OPTIONS Options: OPTIONS opt :verbose, "Force debug. Output lots of informtion to standard error", default: false opt :list, "List all the commands", default: false, short: :none opt :log, "Set the logger output location", default: "<stderr>", short: :none opt :help, "Show help message", short: :h opt :version, "Print version and exit", short: :none stop_on FlatKit::Command.names Cli. end end |
#run(argv: ARGV, env: ENV) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/flat_kit/cli.rb', line 56 def run(argv: ARGV, env: ENV) opts = ::Optimist::with_standard_exception_handling(parser) do parser.parse(argv) end if opts[:log_given] then ::FlatKit.log_to(opts[:log]) end if opts[:verbose] then ::FlatKit.logger.level = :debug else ::FlatKit.logger.level = :info end ::FlatKit.logger.debug opts ::FlatKit.logger.debug argv command_name = argv.shift if command_name.downcase == "help" then parser.educate exit 0 end command_klass = FlatKit::Command.for(command_name) if command_klass.nil? then $stdout.puts "ERROR: Unknown command '#{command_name}'" parser.educate exit 0 end command = command_klass.new(argv: argv, logger: ::FlatKit.logger, env: env) command.call end |