Class: CommandLine::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/command_line_arguments.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition = {}) ⇒ Option

Initialize new CommandLine::Option name The name of the flag definition The definition of the flag.



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

def initialize(name, definition = {})
  @name            = CommandLine::Option.rewrite(name)
  @alias           = definition[:alias] ? definition[:alias].to_sym : nil
  @required        = definition.has_key?(:required) && definition[:required] == true
  @parameter_count = definition[:parameters] || 1
  @multiple        = definition[:multiple]   || false
  @default_value   = definition[:default]    || false
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias



5
6
7
# File 'lib/cli/command_line_arguments.rb', line 5

def alias
  @alias
end

#default_valueObject (readonly)

Returns the value of attribute default_value



7
8
9
# File 'lib/cli/command_line_arguments.rb', line 7

def default_value
  @default_value
end

#nameObject (readonly)

Returns the value of attribute name



5
6
7
# File 'lib/cli/command_line_arguments.rb', line 5

def name
  @name
end

#parameter_countObject (readonly)

Returns the value of attribute parameter_count



6
7
8
# File 'lib/cli/command_line_arguments.rb', line 6

def parameter_count
  @parameter_count
end

Class Method Details

.rewrite(sym) ⇒ Object

Rewrites a command line keyword by replacing the underscores with dashes sym The symbol to rewrite



11
12
13
# File 'lib/cli/command_line_arguments.rb', line 11

def self.rewrite(sym)
  sym.to_s.gsub(/_/, '-').to_sym
end

Instance Method Details

#=~(test) ⇒ Object



51
52
53
# File 'lib/cli/command_line_arguments.rb', line 51

def =~(test)
  [@name, @alias].include?(CommandLine::Option.rewrite(test))
end

#has_alias?Boolean

Check if flag has an alias



66
67
68
# File 'lib/cli/command_line_arguments.rb', line 66

def has_alias?
  !@alias.nil?
end

#has_default?Boolean



84
85
86
# File 'lib/cli/command_line_arguments.rb', line 84

def has_default?
  !@default_value.nil?
end

#multiple?Boolean



80
81
82
# File 'lib/cli/command_line_arguments.rb', line 80

def multiple?
  @multiple
end

#optional?Boolean

Check if flag is optional



76
77
78
# File 'lib/cli/command_line_arguments.rb', line 76

def optional?
  !@required
end

#parse(arguments_parser) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/command_line_arguments.rb', line 27

def parse(arguments_parser)
  if @parameter_count == 0
    return true
  elsif @parameter_count == 1
    parameter = arguments_parser.next_parameter
    raise CommandLine::ParameterExpected, self if parameter.nil?
    return parameter
  elsif @parameter_count == :any
    parameters = []
    while parameter = arguments_parser.next_parameter && parameter != '--'
      parameters << parameter
    end
    return parameters
  else
    parameters = []
    @parameter_count.times do |n|
      parameter = arguments_parser.next_parameter
      raise CommandLine::ParameterExpected, self if parameter.nil?
      parameters << parameter
    end
    return parameters
  end
end

#required?Boolean

Check if flag is required



71
72
73
# File 'lib/cli/command_line_arguments.rb', line 71

def required?
  @required
end

#to_aliasObject

Argument alias representation of the flag (-f)



61
62
63
# File 'lib/cli/command_line_arguments.rb', line 61

def to_alias
  "-#{@alias}"
end

#to_optionObject

Argument representation of the flag (–fast)



56
57
58
# File 'lib/cli/command_line_arguments.rb', line 56

def to_option
  "--#{@name}"
end