Class: VScripts::CommandLine
- Inherits:
-
Object
- Object
- VScripts::CommandLine
- Defined in:
- lib/vscripts/command_line.rb
Overview
Global Command Line
Instance Attribute Summary collapse
-
#arguments ⇒ Array
All the command line arguments.
-
#command ⇒ String
readonly
Command name.
-
#global ⇒ Hash
readonly
Global command line arguments.
Instance Method Summary collapse
-
#initialize(argv = []) ⇒ CommandLine
constructor
Build command line arguments.
-
#parse_cli_options ⇒ Object
Parses command line arguments.
-
#parser ⇒ Object
Specifies command line options This method smells of :reek:NestedIterators but ignores them This method smells of :reek:TooManyStatements but ignores them.
-
#verify_command ⇒ String
Ensure command is available.
Constructor Details
#initialize(argv = []) ⇒ CommandLine
Build command line arguments
15 16 17 18 19 |
# File 'lib/vscripts/command_line.rb', line 15 def initialize(argv = []) @arguments ||= argv @global ||= @command ||= verify_command end |
Instance Attribute Details
#arguments ⇒ Array
Returns All the command line arguments.
8 9 10 |
# File 'lib/vscripts/command_line.rb', line 8 def arguments @arguments end |
#command ⇒ String (readonly)
Returns Command name.
12 13 14 |
# File 'lib/vscripts/command_line.rb', line 12 def command @command end |
#global ⇒ Hash (readonly)
Returns Global command line arguments.
10 11 12 |
# File 'lib/vscripts/command_line.rb', line 10 def global @global end |
Instance Method Details
#parse_cli_options ⇒ Object
Parses command line arguments
47 48 49 50 51 52 |
# File 'lib/vscripts/command_line.rb', line 47 def Trollop.with_standard_exception_handling parser do fail Trollop::HelpNeeded if arguments.empty? parser.parse arguments end end |
#parser ⇒ Object
Specifies command line options This method smells of :reek:NestedIterators but ignores them This method smells of :reek:TooManyStatements but ignores them
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vscripts/command_line.rb', line 24 def parser # rubocop:disable MethodLength available = Command.list.map { |cmd| cmd.to_s.downcase } @parser ||= Trollop::Parser.new do version VScripts::VERSION_C <<-EOS VScripts automation daemon. Available commands: #{available} Usage: vscripts GLOBAL-OPTIONS COMMAND OPTIONS For help on an individual command: vscripts COMMAND --help Global Options: EOS stop_on available end end |
#verify_command ⇒ String
Ensure command is available
56 57 58 59 60 61 62 63 64 |
# File 'lib/vscripts/command_line.rb', line 56 def verify_command command_cli = arguments.shift command_cls = command_cli.capitalize.to_sym if Command.list.include?(command_cls) return command_cls else abort "Error: Unknown subcommand '#{command_cli}'\nTry --help." end end |