Class: Argvector

Class | Argvector
In: | lib/more/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 flags new operands options parameters parameters parameters_without_duplicates 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.

[Source]

    # File lib/more/facets/argvector.rb, line 81
      def initialize(line=nil, arity=nil)
        @line, @argv  = parse_line(line)
        @arity = parse_arity(arity||{})
        parse
      end

parameters(*args)

[Source]

    # File lib/more/facets/argvector.rb, line 67
      def self.parameters(*args)
        new.parameters(*args)
      end

### Public Instance methods

arguments()

Alias for operands

flags()

Return flags, which are true options.

[Source]

    # File lib/more/facets/argvector.rb, line 113
      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

operands()

Returns operand array.

[Source]

    # File lib/more/facets/argvector.rb, line 91
      def operands
        @operands
      end

options()

Returns options hash.

[Source]

    # File lib/more/facets/argvector.rb, line 100
      def options
        @options
      end

parameters()

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

[Source]

    # File lib/more/facets/argvector.rb, line 107
      def parameters
        return @operands, @options
      end

parameters_without_duplicates()

Like parameters but without allowing for duplicate options.

[Source]

    # File lib/more/facets/argvector.rb, line 188
      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

preflags()

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

[Source]

    # File lib/more/facets/argvector.rb, line 175
      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

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}

[Source]

    # File lib/more/facets/argvector.rb, line 167
      def preoptions
        preopts, remainder = *parse_preoptions(argv)
        return preopts
      end

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’.

[Source]

    # File lib/more/facets/argvector.rb, line 151
      def subcommand_with_arguments
        opts, args = *parse_preoptions(argv)
        cmd = args.shift
        subargs = self.class.new(args, @arity)
        return cmd, subargs
      end

subcommand_with_parameters()

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

[Source]

    # File lib/more/facets/argvector.rb, line 127
      def subcommand_with_parameters
        opts, args = *parse_preoptions(argv)
        cmd = args.shift
        subargs = self.class.new(args, @arity)
        return [cmd, *subargs.parameters]
      end

subcommand_with_preoptions()

[Source]

    # File lib/more/facets/argvector.rb, line 137
      def subcommand_with_preoptions
        pre, args = *parse_preoptions(argv)
        cmd = args.shift
        subargs = self.class.new(args, @arity)
        args, opts = *subargs.parameters
        return [cmd, args, pre.merge(opts)]
      end

[Validate]