Class: TBM::CommandLineInterface
- Inherits:
-
Object
- Object
- TBM::CommandLineInterface
- 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
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
Class Method Summary collapse
-
.parse_and_run ⇒ Object
Parse the configuration and command-line arguments and run the tunnel boring machine if both are valid.
Instance Method Summary collapse
-
#initialize(config) ⇒ CommandLineInterface
constructor
Initialize the CLI with a parsed config.
-
#parse ⇒ Object
Parses the command-line arguments by seeking targets, and print errors/usage if necessary.
-
#valid? ⇒ Boolean
The CLI is valid if no errors were found during parsing, and that are known target tunnels to bore.
Constructor Details
#initialize(config) ⇒ CommandLineInterface
Initialize the CLI with a parsed config.
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
#targets ⇒ Object (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_run ⇒ Object
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
#parse ⇒ Object
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.
31 32 33 |
# File 'lib/TBM/cli.rb', line 31 def valid? !@targets.nil? end |