Class: RBWatch::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout:, stderr:, stdin:) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
15
16
# File 'lib/rbwatch/cli.rb', line 11

def initialize(argv, stdout:, stderr:, stdin:)
  @argv = argv.dup
  @stdout = stdout
  @stderr = stderr
  @stdin = stdin
end

Class Method Details

.start(argv = ARGV, stdout: $stdout, stderr: $stderr, stdin: $stdin) ⇒ Object



7
8
9
# File 'lib/rbwatch/cli.rb', line 7

def self.start(argv = ARGV, stdout: $stdout, stderr: $stderr, stdin: $stdin)
  new(argv, stdout: stdout, stderr: stderr, stdin: stdin).run
end

Instance Method Details

#parseObject



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
# File 'lib/rbwatch/cli.rb', line 41

def parse
  options = default_options
  parser = build_parser(options)
  parser.parse!(@argv)

  if options.delete(:help_requested)
    @show_help = true
    return :help
  end

  configuration = Configuration.from_sources(
    cli_options: options,
    command: @argv,
    stdin: @stdin,
    stdout: @stdout,
    stderr: @stderr
  )

  if configuration.command.empty?
    @stderr.puts parser
    @show_help = false
    return :help
  end

  configuration
ensure
  @parser = parser
end

#parserObject



70
71
72
# File 'lib/rbwatch/cli.rb', line 70

def parser
  @parser || build_parser(default_options)
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbwatch/cli.rb', line 18

def run
  configuration = parse
  if configuration == :help
    @stdout.puts parser if @show_help
    return 0
  end

  Runner.new(configuration: configuration).run
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
  @stderr.puts e.message
  @stderr.puts
  @stderr.puts parser
  1
rescue Theme::UnknownThemeError => e
  @stderr.puts e.message
  1
rescue ArgumentError => e
  @stderr.puts e.message
  @stderr.puts
  @stderr.puts parser
  1
end