Class | Argvector
In: | lib/supplemental/facets/argvector.rb
Parent: | Object

# Argvector

Argvector provides a very simple means of parsing command line arguments.

Unlike other more complex libs this provides only the most 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 = Argvector.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'}]

### Methods

arguments assoc_options flags format_options multi_flag new operands options parameters parameters parameters_without_duplicates parse parse_arity parse_line parse_preoptions preflags preoptions subcommand subcommand_with_arguments subcommand_with_parameters subcommand_with_preoptions

### Attributes

argv | [R] |
arity | [R] |
line | [R] |

### Public Class methods

new(line=nil, arity=nil)

Takes the command line string (or array) and options. Options have flags and end with a hash of option arity.

parameters(*args)

### Public Instance methods

arguments()

Alias for operands

assoc_options(args)

Parse flags takes the command line and transforms it such that flags (eg. -x and —x) are elemental associative arrays.

      line = "--foo hello --try=this"
      argv = Argvector.new(line)

      args = line.split(/\s/)
      argv.assoc_options(args)  #=> [ ["foo",true], "hello", ["try","this"] ]

flags()

Return flags, which are true options.

format_options(assoc_options)

Format flag options. This converts the associative array of options/flags into a hash. Repeat options will be placed in arrays.

multi_flag(args=nil)

Split single letter option groupings into separate options. ie. -xyz => -x -y -z

operands()

Returns operand array.

options()

Returns options hash.

parameters()

Returns [operands, options], which is good for plugging directly into a method.

parameters_without_duplicates()

Like parameters but without allowing for duplicate options.

parse()

Basic parser partitions the command line into options and operands. Options are converted to a hash and the two parts are returned.

      line = "--trace stamp --file=VERSION"
      argv = Argvector.new(line)

      args, keys = *argv.parse

      args #=> ["stamp"]
      keys #=> {"trace"=>true, "file"=>"VERSION"}

parse_arity(arity)

Ensure arity is uniform.

parse_line(line=nil)

First pass parser to split the command line into an array using Shellwords, if not already so divided.

parse_preoptions(args)

Parse preoptions. A "preoption" is one that occurs before the first operans (if any).

preflags()

Same as `flags` but only returns flags in the preoptions.

preoptions()

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 = Argvector.new(line)
      opts = cargs.preoptions
      opts #=> {"trace"=>true}

subcommand()

Alias for subcommand_with_parameters

subcommand_with_arguments()

Assumes the first operand is a "subcommand" and returns it and the argments following it as another Arguments object.

TODO: This probably should be called ‘subcommand’.

subcommand_with_parameters()

Assumes the first operand is a "subcommand" and returns it and the argments following it as parameters.

subcommand_with_preoptions()

[Validate]