Class: Molder::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/molder/cli.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
21
22
23
24
# File 'lib/molder/cli.rb', line 16

def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
  @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel

  self.options                 = Hashie::Mash.new
  self.options[:max_processes] = Etc.nprocessors - 2
  self.argv                    = argv.dup
  self.original_argv           = argv.dup
  self.class.instance          = self
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



13
14
15
# File 'lib/molder/cli.rb', line 13

def instance
  @instance
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



9
10
11
# File 'lib/molder/cli.rb', line 9

def argv
  @argv
end

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/molder/cli.rb', line 9

def command
  @command
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/molder/cli.rb', line 9

def config
  @config
end

#kernelObject (readonly)

Returns the value of attribute kernel.



10
11
12
# File 'lib/molder/cli.rb', line 10

def kernel
  @kernel
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/molder/cli.rb', line 9

def options
  @options
end

#original_argvObject

Returns the value of attribute original_argv.



9
10
11
# File 'lib/molder/cli.rb', line 9

def original_argv
  @original_argv
end

#stderrObject (readonly)

Returns the value of attribute stderr.



10
11
12
# File 'lib/molder/cli.rb', line 10

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



10
11
12
# File 'lib/molder/cli.rb', line 10

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



10
11
12
# File 'lib/molder/cli.rb', line 10

def stdout
  @stdout
end

Instance Method Details

#execute!Object



26
27
28
29
30
31
32
33
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
# File 'lib/molder/cli.rb', line 26

def execute!

  self.argv << '-h' if argv.empty?

  parser.parse!(self.argv)

  if options.help
    @kernel.exit(0)
    return
  end

  exit_code = begin
    $stderr = @stderr
    $stdin  = @stdin
    $stdout = @stdout

    parse_args!

    App.new(config: config, options: options, command_name: command).execute!

    0
  rescue StandardError => e
    report_error(exception: e)
    1
  rescue SystemExit => e
    e.status
  ensure
    $stderr = STDERR
    $stdin  = STDIN
    $stdout = STDOUT
  end
  @kernel.exit(exit_code)
end