Class: TBM::CommandLineInterface

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

Overview

The command-line interface for TBM, this parses the supplied arguments and orchestrates the rest of the classes based on those supplied arguments.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CommandLineInterface

Initialize the CLI with a parsed config.

Parameters:

  • config (Config)

    the parsed configuration



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

def initialize( config )
	@config = config
	@targets = nil
	@cancelled = false
end

Instance Attribute Details

#targetsObject (readonly)

Returns the value of attribute targets.



6
7
8
# File 'lib/TBM/cli.rb', line 6

def targets
  @targets
end

Class Method Details

.parse_and_runObject

Parse the configuration and command-line arguments and run the tunnel boring machine if both are valid.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/TBM/cli.rb', line 36

def self.parse_and_run
	config = ConfigParser.parse
	if config.valid?
		cli = CommandLineInterface.new( config )
		cli.parse
		if cli.valid?
			machine = Machine.new( cli.targets )
			machine.bore
		end
	else
		joined_errors = config.errors.join("\n\t")
		puts "Cannot parse configuration:\n\t#{joined_errors}"
	end
end

Instance Method Details

#parseObject

Parses the command-line arguments by seeking targets, and print errors/usage if necessary.



18
19
20
21
22
23
24
25
26
# File 'lib/TBM/cli.rb', line 18

def parse
	if ARGV.empty?
		print_targets "SYNTAX: tbm <targets>\n\nWhere <targets> is a comma-separated list of:"
	elsif ARGV == ['--version']
		puts "Tunnel Boring Machine v#{VERSION}"
	else
		parse_targets( ARGV )
	end
end

#valid?Boolean

The CLI is valid if no errors were found during parsing, and that are known target tunnels to bore.

Returns:

  • (Boolean)

    true if there are targets defined, which will only happen if there were no parsing errors



31
32
33
# File 'lib/TBM/cli.rb', line 31

def valid?
	!@targets.nil?
end