Class: Lucie::Core::CommandLineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lucie/command_line_parser.rb

Overview

Command line parser extracts the controller name and action from the application input and prepares the params hash for the controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ CommandLineParser

Initializes a CommandLineParser

Parameters:

  • parameters (String|Array)

    Input that needs to be processed



18
19
20
21
22
23
24
25
# File 'lib/lucie/command_line_parser.rb', line 18

def initialize(parameters)
  @params_str = parameters.class == Array ? parameters.join(" ") : parameters
  @options = {}
  @options[:args] = []
  @latest_option = nil

  parse_options()
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/lucie/command_line_parser.rb', line 12

def options
  @options
end

Instance Method Details

#[](name) ⇒ Object

Gets the value of an option

For “–option value” the self returns “value”

Parameters:

  • name (Symbol)

    Name of the option



32
33
34
# File 'lib/lucie/command_line_parser.rb', line 32

def [](name)
  @options[name]
end

#argsObject

Get the values which has no options.

Arguments are values without any option or tag. When the option name is undefined, the value gets placed in args.



74
75
76
# File 'lib/lucie/command_line_parser.rb', line 74

def args
  @options[:args].dup
end

#has_arg?(option) ⇒ Boolean

Checks whether the value is in the args.

Parameters:

  • option (String)

    Name of the argument.

Returns:

  • (Boolean)


82
83
84
# File 'lib/lucie/command_line_parser.rb', line 82

def has_arg?(option)
  @options[:args].include?(option)
end

#has_option?(option) ⇒ Boolean

Is the option defined?

Parameters:

  • option (Symbol)

    Name of the option

Returns:

  • (Boolean)


65
66
67
# File 'lib/lucie/command_line_parser.rb', line 65

def has_option?(option)
  @options[remove_dashes(option).to_sym] || false
end

#pair(short, long) ⇒ Object

Pairs short and long options.

When -o is a shortened version of –option, then params must return the same value as params. This method pairs these options.

Parameters:

  • short (Symbol)

    Short version of the option

  • long (Symbol)

    Long version of the option



51
52
53
54
55
56
57
58
59
60
# File 'lib/lucie/command_line_parser.rb', line 51

def pair(short, long)
  short_p = remove_dashes(short).to_sym
  long_p = remove_dashes(long).to_sym

  if [String, TrueClass].include?(@options[short_p].class)
    @options[long_p] = @options[short_p]
  else
    @options[short_p] = @options[long_p]
  end
end

#shiftObject

Shift the parameter list. Useful for removing the controller’s name from the beginning.



39
40
41
# File 'lib/lucie/command_line_parser.rb', line 39

def shift
  @options[:args].shift
end