Class: Console::Arguments
Overview
Console Arguments
Console Arguments provide a very simple means of parsing command line arguments.
Unlike other more complex libs (like Facets’ own Console::Command) Arguments provides only the most basic basic and standard parsing functionality. In many cases that’s all one really needs.
Usage is straight foward. Simply instantiate the class and query it for the particular “views” of the command line you want.
cargs = Console::Arguments.new("-a foo -b=2")
cargs.parameters #=> [['foo'],{'a'=>true,'b'=>'2'}]
cargs.flags #=> ['a']
cargs. #=> {'a'=>true}
cargs.preflags #=> ['a']
cargs.subcommand #=> ['foo',{'b'=>'2'}]
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
Class Method Summary collapse
Instance Method Summary collapse
-
#flags ⇒ Object
Return flags, which are true options.
-
#initialize(line = nil, arity = nil) ⇒ Arguments
constructor
Takes the command line string (or array) and options.
-
#operands ⇒ Object
Returns operand array.
-
#options ⇒ Object
Returns options hash.
-
#parameters ⇒ Object
Returns [operands, options], which is good for plugging directly into a method.
-
#parameters_without_duplicates ⇒ Object
Like parameters but without allowing for duplicate options.
-
#preflags ⇒ Object
Same as
flagsbut only returns flags in the preoptions. -
#preoptions ⇒ Object
Returns a hash of options that occur before the first operand.
-
#subcommand_with_arguments ⇒ Object
Assumes the first operand is a “subcommand” and returns it and the argments following it as another Arguments object.
-
#subcommand_with_parameters ⇒ Object
(also: #subcommand)
Assumes the first operand is a “subcommand” and returns it and the argments following it as parameters.
Constructor Details
#initialize(line = nil, arity = nil) ⇒ Arguments
Takes the command line string (or array) and options. Options have flags and end with a hash of option arity.
61 62 63 64 65 |
# File 'lib/more/facets/arguments.rb', line 61 def initialize(line=nil, arity=nil) @line, @argv = parse_line(line) @arity = parse_arity(arity||{}) parse end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
52 53 54 |
# File 'lib/more/facets/arguments.rb', line 52 def argv @argv end |
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
53 54 55 |
# File 'lib/more/facets/arguments.rb', line 53 def arity @arity end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
51 52 53 |
# File 'lib/more/facets/arguments.rb', line 51 def line @line end |
Class Method Details
.parameters(*args) ⇒ Object
47 48 49 |
# File 'lib/more/facets/arguments.rb', line 47 def self.parameters(*args) new.parameters(*args) end |
Instance Method Details
#flags ⇒ Object
Return flags, which are true options.
90 91 92 93 94 95 96 97 98 |
# File 'lib/more/facets/arguments.rb', line 90 def flags f = [] .each do |k, v| if TrueClass===v or FalseClass===v # not that it's ever false f << k end end return f end |
#operands ⇒ Object
Returns operand array.
71 72 73 |
# File 'lib/more/facets/arguments.rb', line 71 def operands @operands end |
#options ⇒ Object
Returns options hash.
77 78 79 |
# File 'lib/more/facets/arguments.rb', line 77 def end |
#parameters ⇒ Object
Returns [operands, options], which is good for plugging directly into a method.
84 85 86 |
# File 'lib/more/facets/arguments.rb', line 84 def parameters return @operands, end |
#parameters_without_duplicates ⇒ Object
Like parameters but without allowing for duplicate options.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/more/facets/arguments.rb', line 154 def parameters_without_duplicates opts = {} .each do |k,v| if Array===v opts[k] = v[0] else opts[k] = v end end return @operands, opts end |
#preflags ⇒ Object
Same as flags but only returns flags in the preoptions.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/more/facets/arguments.rb', line 141 def preflags preopts, remainder = *(argv) f = [] preopts.each do |k, v| if TrueClass===v or FalseClass===v # not that it's ever false f << k end end return f end |
#preoptions ⇒ Object
Returns a hash of options that occur before the first operand. This works well with subcommand to get the main command’s options.
line = "--trace stamp --file VERSION"
cargs = Console::Arguments.new(line)
opts = cargs.
opts #=> {"trace"=>true}
133 134 135 136 |
# File 'lib/more/facets/arguments.rb', line 133 def preopts, remainder = *(argv) return preopts end |
#subcommand_with_arguments ⇒ Object
Assumes the first operand is a “subcommand” and returns it and the argments following it as another Arguments object.
117 118 119 120 121 122 |
# File 'lib/more/facets/arguments.rb', line 117 def subcommand_with_arguments opts, args = *(argv) cmd = args.shift = self.class.new(args, @arity) return cmd, end |
#subcommand_with_parameters ⇒ Object Also known as: subcommand
Assumes the first operand is a “subcommand” and returns it and the argments following it as parameters.
104 105 106 107 108 109 |
# File 'lib/more/facets/arguments.rb', line 104 def subcommand_with_parameters opts, args = *(argv) cmd = args.shift = self.class.new(args, @arity) return cmd, .parameters end |