Class: Hobo::Cli

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hobo/cli.rb

Overview

Main application class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(opts = {}) ⇒ Cli

:help and :host_check are only used by tests. :slop is used to ensure low-level args parsed in bin/hobo are propagated to the application

Parameters:

  • Initialization (Hash)

    accepts several options in a hash:

    • :slop - Slop instance

    • :help - Help formatter instance

    • :host_check - Host check invocation class



24
25
26
27
28
29
30
# File 'lib/hobo/cli.rb', line 24

def initialize opts = {}
  @opts = opts
  @slop = opts[:slop] || Slop.new
  @help_formatter = opts[:help] || Hobo::HelpFormatter.new(@slop)
  @help_opts = {}
  @host_check = opts[:host_check] || Hobo::Lib::HostCheck
end

Instance Attribute Details

#help_formatterObject

Returns the value of attribute help_formatter.



15
16
17
# File 'lib/hobo/cli.rb', line 15

def help_formatter
  @help_formatter
end

#slopObject

Returns the value of attribute slop.



15
16
17
# File 'lib/hobo/cli.rb', line 15

def slop
  @slop
end

Instance Method Details

#show_help(opts = {}) ⇒ Object

Display help and exit Options are mostly used for filtering

Parameters:

  • Options (Hash)

    to apss to help formatter.



75
76
77
78
# File 'lib/hobo/cli.rb', line 75

def show_help(opts = {})
  Hobo.ui.info @help_formatter.help(@help_opts.merge(opts))
  halt
end

#start(args = ARGV) ⇒ Object

entry point for application

Parameters:

  • Arguments (Array)

    from ARGV. Defaults to ARGV



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hobo/cli.rb', line 34

def start args = ARGV
  load_user_config
  load_builtin_tasks
  load_hobofiles
  load_project_config

  tasks = structure_tasks Hobo::..keys
  define_global_opts @slop

  begin
    # Parse out global args first
    @slop.parse! args
    opts = @slop.to_hash

    perform_host_checks unless opts[:'skip-host-checks']

    @help_opts[:all] = opts[:all]

    @slop.add_callback :empty do
      show_help
    end

    # Necessary to make command level help work
    args.push "--help" if @slop.help?

    @help_formatter.command_map = define_tasks(tasks, @slop)

    remaining = @slop.parse! args
    raise Hobo::InvalidCommandOrOpt.new remaining.join(" "), self if remaining.size > 0

    show_help if @slop.help?
  rescue Halt
    # NOP
  end

  return 0
end