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(*args) ⇒ CommandLine

Build command line arguments



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

def initialize(*args)
  @global ||= parse(*args)
  @command ||= check_command(*args)
  @extra ||= rest(*args)
end

Instance Attribute Details

#commandString (readonly)

Returns Command name.

Returns:

  • (String)

    Command name



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

def command
  @command
end

#extraArray (readonly)

Returns Extra arguments passed to the command.

Returns:

  • (Array)

    Extra arguments passed to the command



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

def extra
  @extra
end

#globalHash (readonly)

Returns Global command line arguments.

Returns:

  • (Hash)

    Global command line arguments



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

def global
  @global
end

Instance Method Details

#check_command(args) ⇒ String

Ensure command is available

Returns:

  • (String)

    Command name



56
57
58
59
60
61
62
63
64
65
# File 'lib/vscripts/command_line.rb', line 56

def check_command(args)
  command_cli = args.shift
  command_cls = command_cli.capitalize.to_sym
  if command_cli && Command.list.include?(command_cls)
    return command_cls
  else
    puts "Error: Unknown subcommand '#{command_cli}'\nTry --help for help."
    exit 1
  end
end

#parse(*args) ⇒ Object

Parses command line arguments



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

def parse(*args)
  Trollop.with_standard_exception_handling parser do
    fail Trollop::HelpNeeded if args.empty?
    parser.parse args
  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
  Trollop::Parser.new do
    version VScripts::VERSION_C
    banner <<-EOS
VScripts automation daemon.

Available commands:
    #{available.map { |cmd| cmd.to_s.downcase }}

Usage:
  vscripts GLOBAL-OPTIONS COMMAND OPTIONS

For help on an individual command:
  vscripts COMMAND --help

Global Options:
EOS
    stop_on available
  end
end

#rest(args) ⇒ Array

Returns The rest of the arguments.

Returns:

  • (Array)

    The rest of the arguments



68
69
70
# File 'lib/vscripts/command_line.rb', line 68

def rest(args)
  args
end