Class: CTioga2::Commands::Parsers::CommandLineParser

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/commands/parsers/command-line.rb

Overview

This class is in charge of parsing a command-line against a list of known commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initialize(commands, default = nil) ⇒ CommandLineParser

Creates a CommandLineParser that will understand the given commands



57
58
59
60
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 57

def initialize(commands, default = nil)
  @commands = commands
  prepare_option_hashes(default)
end

Instance Attribute Details

#commandsObject (readonly)

The list of commands



49
50
51
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 49

def commands
  @commands
end

#default_commandObject (readonly)

A [number of args, Command] for the default command, ie the one that applies on non-command files.



53
54
55
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 53

def default_command
  @default_command
end

#long_optionsObject (readonly)

A hash ‘long-option-name’ => [number of args, Command]



46
47
48
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 46

def long_options
  @long_options
end

#short_optionsObject (readonly)

A hash ‘short-option-letter’ => [number of args, Command]



43
44
45
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 43

def short_options
  @short_options
end

Instance Method Details

#parse_command_line(argv, interpreter) ⇒ Object

Takes an argv array representing the command-line and a target intepreter, and runs the commands found on the command line. Yields arguments which are not part of a command, or feed them to the #default_command if it was specified.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 68

def parse_command_line(argv, interpreter)
  # We duplicate the original array
  argv = argv.dup
  options = nil         # currently never used.
  number = 0
  while argv.size > 0
    current = argv.shift
    if current =~ /^--(.*)/ # a long option
      if @long_options.key?($1)
        command, arguments, options = 
          extract_command_arguments(argv, @long_options[$1])

        number += 1
        interpreter.context.parsing_option(current, number)
        interpreter.run_command(command, arguments, options)
      else
        raise OptionUnkown, "Long option #{current} is not known"
      end
    elsif current =~ /^-(.*)/ # Short options
      # We do the same as above, but splitting into letters first:
      short_options = $1.split('')
      for short in short_options
        if @short_options.key?(short)
          command, arguments, options = 
            extract_command_arguments(argv, @short_options[short])
          number += 1
          interpreter.context.parsing_option("-#{short}", number)
          interpreter.run_command(command, arguments, options)
        else
          raise OptionUnkown, "Short option -#{short} is not known"
        end
      end
    else
      if @default_command
        argv.unshift current
        command, arguments, options = 
          extract_command_arguments(argv, @default_command)
        number += 1
        interpreter.context.parsing_option("(default)", number)
        interpreter.run_command(command, arguments, options)
      else
        yield current
      end
    end
  end
end