Class: CommandLine::Option
- Inherits:
-
Object
- Object
- CommandLine::Option
- Defined in:
- lib/cli/command_line_arguments.rb
Instance Attribute Summary (collapse)
-
- (Object) alias
readonly
Returns the value of attribute alias.
-
- (Object) default_value
readonly
Returns the value of attribute default_value.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) parameter_count
readonly
Returns the value of attribute parameter_count.
Class Method Summary (collapse)
-
+ (Object) rewrite(sym)
Rewrites a command line keyword by replacing the underscores with dashes sym The symbol to rewrite.
Instance Method Summary (collapse)
- - (Object) =~(test)
-
- (Boolean) has_alias?
Check if flag has an alias.
- - (Boolean) has_default?
-
- (Option) initialize(name, definition = {})
constructor
Initialize new CommandLine::Option name The name of the flag definition The definition of the flag.
- - (Boolean) multiple?
-
- (Boolean) optional?
Check if flag is optional.
- - (Object) parse(arguments_parser)
-
- (Boolean) required?
Check if flag is required.
-
- (Object) to_alias
Argument alias representation of the flag (-f).
-
- (Object) to_option
Argument representation of the flag (–fast).
Constructor Details
- (Option) initialize(name, definition = {})
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
- (Object) alias (readonly)
Returns the value of attribute alias
5 6 7 |
# File 'lib/cli/command_line_arguments.rb', line 5 def alias @alias end |
- (Object) default_value (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 |
- (Object) name (readonly)
Returns the value of attribute name
5 6 7 |
# File 'lib/cli/command_line_arguments.rb', line 5 def name @name end |
- (Object) parameter_count (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
+ (Object) rewrite(sym)
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
- (Object) =~(test)
51 52 53 |
# File 'lib/cli/command_line_arguments.rb', line 51 def =~(test) [@name, @alias].include?(CommandLine::Option.rewrite(test)) end |
- (Boolean) has_alias?
Check if flag has an alias
66 67 68 |
# File 'lib/cli/command_line_arguments.rb', line 66 def has_alias? !@alias.nil? end |
- (Boolean) has_default?
84 85 86 |
# File 'lib/cli/command_line_arguments.rb', line 84 def has_default? !@default_value.nil? end |
- (Boolean) multiple?
80 81 82 |
# File 'lib/cli/command_line_arguments.rb', line 80 def multiple? @multiple end |
- (Boolean) optional?
Check if flag is optional
76 77 78 |
# File 'lib/cli/command_line_arguments.rb', line 76 def optional? !@required end |
- (Object) parse(arguments_parser)
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 |
- (Boolean) required?
Check if flag is required
71 72 73 |
# File 'lib/cli/command_line_arguments.rb', line 71 def required? @required end |
- (Object) to_alias
Argument alias representation of the flag (-f)
61 62 63 |
# File 'lib/cli/command_line_arguments.rb', line 61 def to_alias "-#{@alias}" end |
- (Object) to_option
Argument representation of the flag (–fast)
56 57 58 |
# File 'lib/cli/command_line_arguments.rb', line 56 def to_option "--#{@name}" end |