module CommandLine
# Argument flag handling.
class Flag
attr_reader :name
attr_reader :alias
attr_reader :argument
# Initialize new Flag
# name The name of the flag
# definition The definition of the flag.
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
# Argument representation of the flag (--fast)
def to_argument
"--#{@name}"
end
# Argument alias representation of the flag (-f)
def to_alias
"-#{@alias}"
end
# Check if flag has an alias
def has_alias?
!@alias.nil?
end
# Check if flag is optional
def optional?
!@required
end
# Check if flag is required
def required?
@required
end
# Check if flag expects an argument (Are you talking to me?)
def expects_argument?
!@argument.nil?
end
end
end