Class: VScripts::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/vscripts/command_line.rb

Overview

Global Command Line

Instance Attribute Summary collapse

Instance Method Summary collapse

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    ||= parse_cli_options
  @command   ||= verify_command
end

Instance Attribute Details

#argumentsArray

Returns All the command line arguments.

Returns:

  • (Array)

    All the command line arguments



8
9
10
# File 'lib/vscripts/command_line.rb', line 8

def arguments
  @arguments
end

#commandString (readonly)

Returns Command name.

Returns:

  • (String)

    Command name



12
13
14
# File 'lib/vscripts/command_line.rb', line 12

def command
  @command
end

#globalHash (readonly)

Returns Global command line arguments.

Returns:

  • (Hash)

    Global command line arguments



10
11
12
# File 'lib/vscripts/command_line.rb', line 10

def global
  @global
end

Instance Method Details

#parse_cli_optionsObject

Parses command line arguments



47
48
49
50
51
52
# File 'lib/vscripts/command_line.rb', line 47

def parse_cli_options
  Trollop.with_standard_exception_handling parser do
    fail Trollop::HelpNeeded if arguments.empty?
    parser.parse arguments
  end
end

#parserObject

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
    banner <<-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_commandString

Ensure command is available

Returns:

  • (String)

    Command name



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