Class: Rundoc::CLIArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rundoc/cli_argument_parser.rb

Overview

This class is responsible for parsing the command line arguments and generating a Cli instance

Example:

cli = CLIArgumentParser.new(argv: ARGV).to_cli
cli.call

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv:, io: $stderr, env: ENV, exit_obj: Kernel) ⇒ CLIArgumentParser

Returns a new instance of CLIArgumentParser.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rundoc/cli_argument_parser.rb', line 17

def initialize(
  argv:,
  io: $stderr,
  env: ENV,
  exit_obj: Kernel
)
  @io = io
  @env = env
  @argv = argv
  @options = {}
  @exit_obj = exit_obj
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



15
16
17
# File 'lib/rundoc/cli_argument_parser.rb', line 15

def argv
  @argv
end

#envObject (readonly)

Returns the value of attribute env.



15
16
17
# File 'lib/rundoc/cli_argument_parser.rb', line 15

def env
  @env
end

#exit_objObject (readonly)

Returns the value of attribute exit_obj.



15
16
17
# File 'lib/rundoc/cli_argument_parser.rb', line 15

def exit_obj
  @exit_obj
end

#ioObject (readonly)

Returns the value of attribute io.



15
16
17
# File 'lib/rundoc/cli_argument_parser.rb', line 15

def io
  @io
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/rundoc/cli_argument_parser.rb', line 15

def options
  @options
end

Instance Method Details

#callObject



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
# File 'lib/rundoc/cli_argument_parser.rb', line 30

def call
  source_file = argv.first
  if source_file.nil? || source_file == "help"
    parser.parse! ["--help"]
    return
  else
    parser.parse! argv
    return if options[:exit]
  end

  source_path = Pathname(source_file)
  if !source_path.exist?
    @io.puts "No such file `#{source_path.expand_path}`"
    exit_obj.exit(1)
    return
  elsif !source_path.file?
    @io.puts "Path is not a file. Expected `#{source_path.expand_path}` to be a file, but it was not."
    exit_obj.exit(1)
    return
  end

  options[:io] = io
  options[:source_path] = source_path

  self
end