Class: CommandLine::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/command_line/flag.rb

Overview

Argument flag handling.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition) ⇒ Flag

Initialize new Flag name The name of the flag definition The definition of the flag.



13
14
15
16
17
18
# File 'lib/command_line/flag.rb', line 13

def initialize(name, definition)
  @name     = name.to_s.gsub(/_/, '-').to_sym
  @alias    = definition[:alias].to_sym if definition[:alias]
  @required = definition.has_key?(:required) && definition[:required] == true
  @argument = definition[:expects] if definition[:expects]
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



7
8
9
# File 'lib/command_line/flag.rb', line 7

def alias
  @alias
end

#argumentObject (readonly)

Returns the value of attribute argument.



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

def argument
  @argument
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/command_line/flag.rb', line 6

def name
  @name
end

Instance Method Details

#expects_argument?Boolean

Check if flag expects an argument (Are you talking to me?)

Returns:

  • (Boolean)


46
47
48
# File 'lib/command_line/flag.rb', line 46

def expects_argument?
  !@argument.nil?
end

#has_alias?Boolean

Check if flag has an alias

Returns:

  • (Boolean)


31
32
33
# File 'lib/command_line/flag.rb', line 31

def has_alias?
  !@alias.nil?
end

#optional?Boolean

Check if flag is optional

Returns:

  • (Boolean)


36
37
38
# File 'lib/command_line/flag.rb', line 36

def optional?
  !@required
end

#required?Boolean

Check if flag is required

Returns:

  • (Boolean)


41
42
43
# File 'lib/command_line/flag.rb', line 41

def required?
  @required
end

#to_aliasObject

Argument alias representation of the flag (-f)



26
27
28
# File 'lib/command_line/flag.rb', line 26

def to_alias
  "-#{@alias}"
end

#to_argumentObject

Argument representation of the flag (–fast)



21
22
23
# File 'lib/command_line/flag.rb', line 21

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