Class: Hem::Cli

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hem/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/hem 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
31
# File 'lib/hem/cli.rb', line 24

def initialize opts = {}
  @opts = opts
  @slop = opts[:slop] || Slop.new
  @help_formatter = opts[:help] || Hem::HelpFormatter.new(@slop)
  @help_opts = {}
  @host_check = opts[:host_check] || Hem::Lib::HostCheck
  @pre_task_namespaces = ['shell-init', 'plugin']
end

Instance Attribute Details

#help_formatterObject

Returns the value of attribute help_formatter.



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

def help_formatter
  @help_formatter
end

#slopObject

Returns the value of attribute slop.



15
16
17
# File 'lib/hem/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.



76
77
78
79
# File 'lib/hem/cli.rb', line 76

def show_help(opts = {})
  Hem.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



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
71
# File 'lib/hem/cli.rb', line 35

def start args = ARGV
  load_user_config
  load_builtin_tasks
  load_hemfiles
  load_project_config

  define_global_opts @slop

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

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

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

    include_pre_tasks
    remaining = @slop.parse! args.dup
    remaining.push "--help" if @slop.help?

    if remaining == args
      include_post_tasks opts

      remaining = @slop.parse! remaining
    end

    raise Hem::InvalidCommandOrOpt.new remaining.join(" ") if remaining.size > 0

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

  return 0
end