Class: Console::Arguments

Inherits:
Object show all
Defined in:
lib/more/facets/arguments.rb

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.preoptions    #=> {'a'=>true}
cargs.preflags      #=> ['a']
cargs.subcommand    #=> ['foo',{'b'=>'2'}]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argvObject (readonly)

Returns the value of attribute argv.



52
53
54
# File 'lib/more/facets/arguments.rb', line 52

def argv
  @argv
end

#arityObject (readonly)

Returns the value of attribute arity.



53
54
55
# File 'lib/more/facets/arguments.rb', line 53

def arity
  @arity
end

#lineObject (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

#flagsObject

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 = []
  @options.each do |k, v|
    if TrueClass===v or FalseClass===v  # not that it's ever false
      f << k
    end
  end
  return f
end

#operandsObject

Returns operand array.



71
72
73
# File 'lib/more/facets/arguments.rb', line 71

def operands
  @operands
end

#optionsObject

Returns options hash.



77
78
79
# File 'lib/more/facets/arguments.rb', line 77

def options
  @options
end

#parametersObject

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, @options
end

#parameters_without_duplicatesObject

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 = {}
  @options.each do |k,v|
    if Array===v
      opts[k] = v[0]
    else
      opts[k] =  v
    end
  end
  return @operands, opts
end

#preflagsObject

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 = *parse_preoptions(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

#preoptionsObject

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.preoptions
opts #=> {"trace"=>true}


133
134
135
136
# File 'lib/more/facets/arguments.rb', line 133

def preoptions
  preopts, remainder = *parse_preoptions(argv)
  return preopts
end

#subcommand_with_argumentsObject

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 = *parse_preoptions(argv)
  cmd = args.shift
  subargs = self.class.new(args, @arity)
  return cmd, subargs
end

#subcommand_with_parametersObject 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 = *parse_preoptions(argv)
  cmd = args.shift
  subargs = self.class.new(args, @arity)
  return cmd, subargs.parameters
end