|
5 | 5 | module VScripts |
6 | 6 | # Parses the command line arguments |
7 | 7 | class CommandLine |
8 | | - # @return [Array] all command line arguments |
9 | | - attr_accessor :arguments |
10 | 8 | # @return [Hash] the global command line arguments. |
11 | 9 | attr_reader :global |
12 | 10 | # @return [String] the command name |
13 | 11 | attr_reader :command |
| 12 | + # @return [Array] the command specific arguments. |
| 13 | + attr_reader :command_options |
14 | 14 |
|
15 | | - # Builds command line arguments |
16 | | - def initialize(argv = []) |
17 | | - @arguments ||= argv |
18 | | - @global ||= parse_cli_options |
19 | | - @command ||= verify_command |
| 15 | + # @param args [Array] the command line arguments |
| 16 | + # @return [Hash] the command line options |
| 17 | + def initialize(args = ARGV) |
| 18 | + parse_global(args) |
20 | 19 | end |
21 | 20 |
|
22 | 21 | # Specifies command line options |
23 | 22 | # This method smells of :reek:NestedIterators but ignores them |
24 | 23 | # This method smells of :reek:TooManyStatements but ignores them |
25 | 24 | def parser # rubocop:disable MethodLength |
26 | 25 | available = Commands.list.map { |cmd| cmd.to_s.downcase } |
27 | | - @parser ||= Trollop::Parser.new do |
| 26 | + Trollop::Parser.new do |
28 | 27 | version VScripts::VERSION::COPYRIGHT |
29 | 28 | banner <<-EOS |
30 | 29 | VScripts automation daemon. |
31 | 30 |
|
32 | 31 | Available commands: |
33 | 32 | #{available} |
34 | 33 |
|
35 | | -Usage: |
| 34 | +USAGE: |
36 | 35 | vscripts GLOBAL-OPTIONS COMMAND OPTIONS |
37 | 36 |
|
38 | 37 | For help on an individual command: |
39 | 38 | vscripts COMMAND --help |
40 | 39 |
|
41 | | -Global Options: |
| 40 | +GLOBAL OPTIONS: |
42 | 41 | EOS |
| 42 | + opt :config, 'Specify configuration file', |
| 43 | + type: :string, short: '-c' |
43 | 44 | stop_on available |
| 45 | + stop_on_unknown |
44 | 46 | end |
45 | 47 | end |
46 | 48 |
|
47 | | - # @return [Hash] the command line arguments |
48 | | - def parse_cli_options |
| 49 | + # @param args [Array] the command line arguments |
| 50 | + # @return [Hash] the command line options |
| 51 | + def parse_global(args) |
49 | 52 | Trollop.with_standard_exception_handling parser do |
50 | | - fail Trollop::HelpNeeded if arguments.empty? |
51 | | - parser.parse arguments |
| 53 | + @global = parser.parse args |
| 54 | + fail Trollop::HelpNeeded if args.empty? || !parse_command(args) |
52 | 55 | end |
| 56 | + @global |
53 | 57 | end |
54 | 58 |
|
55 | 59 | # Ensures command is available |
56 | 60 | # @return [String] the command name |
57 | | - def verify_command |
58 | | - command_cli = arguments.shift |
59 | | - command_cls = command_cli.capitalize.to_sym |
60 | | - if Commands.list.include?(command_cls) |
61 | | - return command_cls |
62 | | - else |
63 | | - abort "Error: unknown subcommand '#{command_cli}'\nTry --help." |
64 | | - end |
| 61 | + # @return [Array] the command specific arguments |
| 62 | + def parse_command(args) |
| 63 | + command = args.shift |
| 64 | + return unless command |
| 65 | + command_class = command.capitalize.to_sym |
| 66 | + abort "Error: unknown subcommand '#{command}'\nTry --help." \ |
| 67 | + unless Commands.list.include?(command_class) |
| 68 | + @command, @command_options = [command_class, args] |
65 | 69 | end |
66 | 70 | end # class CommandLine |
67 | 71 | end # module VScripts |
0 commit comments