Class: VScripts::CommandLine

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

Overview

Parses the command line arguments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ Hash

Returns the command line options.

Parameters:

  • args (Array) (defaults to: ARGV)

    the command line arguments



17
18
19
# File 'lib/vscripts/command_line.rb', line 17

def initialize(args = ARGV)
  parse_global(args)
end

Instance Attribute Details

#commandString (readonly)

Returns the command name.

Returns:

  • (String)

    the command name



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

def command
  @command
end

#command_optionsArray (readonly)

Returns the command specific arguments.

Returns:

  • (Array)

    the command specific arguments.



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

def command_options
  @command_options
end

#globalHash (readonly)

Returns the global command line arguments.

Returns:

  • (Hash)

    the global command line arguments.



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

def global
  @global
end

Instance Method Details

#parse_command(args) ⇒ String, Array

Ensures command is available

Returns:

  • (String)

    the command name

  • (Array)

    the command specific arguments



62
63
64
65
66
67
68
69
# File 'lib/vscripts/command_line.rb', line 62

def parse_command(args)
  command = args.shift
  return unless command
  command_class = command.capitalize.to_sym
  abort "Error: unknown subcommand '#{command}'\nTry --help." \
    unless Commands.list.include?(command_class)
  @command, @command_options = [command_class, args]
end

#parse_global(args) ⇒ Hash

Returns the command line options.

Parameters:

  • args (Array)

    the command line arguments

Returns:

  • (Hash)

    the command line options



51
52
53
54
55
56
57
# File 'lib/vscripts/command_line.rb', line 51

def parse_global(args)
  Trollop.with_standard_exception_handling parser do
    @global = parser.parse args
    fail Trollop::HelpNeeded if args.empty? || !parse_command(args)
  end
  @global
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
45
46
47
# File 'lib/vscripts/command_line.rb', line 24

def parser # rubocop:disable MethodLength
  available = Commands.list.map { |cmd| cmd.to_s.downcase }
  Trollop::Parser.new do
    version VScripts::VERSION::COPYRIGHT
    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
    opt :config, 'Specify configuration file',
        type: :string, short: '-c'
    stop_on available
    stop_on_unknown
  end
end