# = TITLE: # # Argv DSL # # = COPYING: # # Copyright (c) 2007 Psi T Corp. # # This file is part of the ProUtils' Ratch program. # # Ratch is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ratch is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ratch. If not, see . module Ratch module Dsl # No fuss access to ARGV. This shows up as #commandline in the ratch api. # # Ratch uses '=' for parameterized flags b/c this make parsing stupid simple # and that's a good thing!!! However you can use value! if need be. module Argv def commandline @commandline ||= ArgVector.new(ARGV) end alias_method :argument_vector, :commandline # class ArgVector attr :argv def initialize(argv) @argv = argv.dup end def arguments @arguments ||= argv.select{ |e| e !~ /^-/ && e !~ /=/ } end def options @options ||= ( pms = {} argv.select{ |e| /[=]/ =~ e }.each do |e| pms.store(*e.split('=')) end pms ) end def flags @flags ||= argv.select{ |e| e =~ /^-/ || e !~ /=/ } end # Have specific flag? def flag?(flag) flags.include?(flag) end # def [](x) case x when Integer arguments[x] else options[x.to_s] end end # You can use this if you want to use parameterized # flags w/o the '=', however be aware that the # parameter value will also be listed amoung the # bare arguments list. For example: # # $ foo tom --say hello # # argv.value('--say') #=> "hello" # argv.arguments #=> ["tom", "hello"] # def value(flag) argv.fetch(index(flag)+1) end # def value!(flag) @values ||= {} @values[flag] ||= ( argv.delete(index(flag)) argv.delete(index(flag)) ) end end end end end