Class: Clin::Argument
- Inherits:
-
Object
- Object
- Clin::Argument
- Defined in:
- lib/clin/argument.rb
Overview
Command line positional argument(not option)
Instance Attribute Summary collapse
-
#multiple ⇒ Object
If the argument accept multiple values.
-
#name ⇒ Object
The name extracted without the brackets and arrows.
-
#optional ⇒ Object
If the argument is optional.
-
#original ⇒ Object
Original name specified in the command.
-
#variable ⇒ Object
If the argument is a fixed argument(User value need to match the name).
Instance Method Summary collapse
-
#check_multiple(argument) ⇒ Object
Check if the argument is multiple(i.e arg…).
-
#check_optional(argument) ⇒ Object
Check if the argument is optional(i.e [arg]).
-
#check_variable(argument) ⇒ Object
Check if the argument is variable(i.e <arg>).
-
#initialize(argument) ⇒ Argument
constructor
Create a new argument from string
argument
will be used to deduce the name, if it’s fixed, optional, accept multiple values If the argument is a simple string(e.g. install) then it will be a fixed argument For the argument to accept variable values it must be surrounded with <> (e.g. <command>) For the argument to be optional it must be surrounded with [] (e.g. [<value>]) For the argument to accept multiple value it must be suffixed with … -
#parse(argv) ⇒ Object
Given a list of arguments extract the list of arguments that are matched.
Constructor Details
#initialize(argument) ⇒ Argument
Create a new argument from string argument
will be used to deduce the name, if it’s fixed, optional, accept multiple values If the argument is a simple string(e.g. install) then it will be a fixed argument For the argument to accept variable values it must be surrounded with <> (e.g. <command>) For the argument to be optional it must be surrounded with [] (e.g. [<value>]) For the argument to accept multiple value it must be suffixed with … (e.g. <commands>…)
28 29 30 31 32 33 34 35 36 |
# File 'lib/clin/argument.rb', line 28 def initialize(argument) @original = argument @optional = false @multiple = false @variable = false argument = check_optional(argument) argument = check_multiple(argument) @name = check_variable(argument) end |
Instance Attribute Details
#multiple ⇒ Object
If the argument accept multiple values
12 13 14 |
# File 'lib/clin/argument.rb', line 12 def multiple @multiple end |
#name ⇒ Object
The name extracted without the brackets and arrows. This will be the key in the params when initializing a command
19 20 21 |
# File 'lib/clin/argument.rb', line 19 def name @name end |
#optional ⇒ Object
If the argument is optional
9 10 11 |
# File 'lib/clin/argument.rb', line 9 def optional @optional end |
#original ⇒ Object
Original name specified in the command
6 7 8 |
# File 'lib/clin/argument.rb', line 6 def original @original end |
#variable ⇒ Object
If the argument is a fixed argument(User value need to match the name)
15 16 17 |
# File 'lib/clin/argument.rb', line 15 def variable @variable end |
Instance Method Details
#check_multiple(argument) ⇒ Object
Check if the argument is multiple(i.e arg…)
48 49 50 51 52 53 54 |
# File 'lib/clin/argument.rb', line 48 def check_multiple(argument) if argument.end_with? '...' @multiple = true return argument[0...-3] end argument end |
#check_optional(argument) ⇒ Object
Check if the argument is optional(i.e [arg])
39 40 41 42 43 44 45 |
# File 'lib/clin/argument.rb', line 39 def check_optional(argument) if check_between(argument, '[', ']') @optional = true return argument[1...-1] end argument end |
#check_variable(argument) ⇒ Object
Check if the argument is variable(i.e <arg>)
57 58 59 60 61 62 63 |
# File 'lib/clin/argument.rb', line 57 def check_variable(argument) if check_between(argument, '<', '>') @variable = true return argument[1...-1] end argument end |
#parse(argv) ⇒ Object
Given a list of arguments extract the list of arguments that are matched
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/clin/argument.rb', line 66 def parse(argv) return handle_empty if argv.empty? if @multiple ensure_fixed(argv) unless @variable [argv, []] else ensure_fixed(argv[0]) unless @variable [argv[0], argv[1..-1]] end end |