Class: Rangu::CLI

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

Constant Summary collapse

SUCCESS =
0
ERROR =
1
USAGE_ERROR =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CLI.



16
17
18
19
20
# File 'lib/rangu/cli.rb', line 16

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

Class Method Details

.run(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ Object



12
13
14
# File 'lib/rangu/cli.rb', line 12

def self.run(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr)
  new(stdin:, stdout:, stderr:).run(arguments)
end

Instance Method Details

#run(arguments) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rangu/cli.rb', line 22

def run(arguments)
  options = {}
  parser = option_parser(options)
  positional = parser.parse(arguments.dup)
  return print_help(parser) if options[:help]
  return print_version if options[:version]

  source = resolve_source(options, positional, parser)
  source ? execute(source) : USAGE_ERROR
rescue OptionParser::ParseError => e
  report_usage(e.message, parser)
  USAGE_ERROR
end