lib/ratch/consoleutils.rb in ratch-0.2.2 vs lib/ratch/consoleutils.rb in ratch-0.2.3

- old
+ new

@@ -1,5 +1,28 @@ +# TITLE: +# +# ConsoleUtils +# +# 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 <http://www.gnu.org/licenses/>. + module Ratch module ConsoleUtils # Convenient method to get simple console reply. @@ -37,52 +60,86 @@ end class Array + # Not empty? + + def not_empty? + !empty? + end + # Convert an array into command line parameters. # The array is accepted in the format of Ruby # method arguments --ie. [arg1, arg2, ..., hash] - def to_params + def to_console flags = (Hash===last ? pop : {}) - flags = flags.collect do |f,v| + flags = flags.to_console + flags + ' ' + join(" ") + end + alias_method :to_params, :to_console + +# def to_console +# flags = (Hash===last ? pop : {}) +# flags = flags.collect do |f,v| +# m = f.to_s.size == 1 ? '-' : '--' +# case v +# when Array +# v.collect{ |e| "#{m}#{f} '#{e}'" }.join(' ') +# when true +# "#{m}#{f}" +# when false, nil +# '' +# else +# "#{m}#{f} '#{v}'" +# end +# end +# return (flags + self).join(" ") +# end + +end + + +class Hash + + # Convert an array into command line parameters. + # The array is accepted in the format of Ruby + # method arguments --ie. [arg1, arg2, ..., hash] + + def to_console + flags = collect do |f,v| m = f.to_s.size == 1 ? '-' : '--' case v when Array - v.collect{ |e| "#{m}#{f} '#{e}'" }.join(' ') + v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ') when true "#{m}#{f}" when false, nil '' else - "#{m}#{f} '#{v}'" + "#{m}#{f}='#{v}'" end end - return (flags + self).join(" ") + flags.join(" ") end - # Not empty? - - def not_empty? - !empty? - end - -end - - -class Hash - - # Setup a console arguments vector from a set of config options. - - def command_vector(args_field=nil) + # Turn a hash into arguments. + # + # h = { :list => [1,2], :base => "HI" } + # h.argumentize #=> [ [], { :list => [1,2], :base => "HI" } ] + # h.argumentize(:list) #=> [ [1,2], { :base => "HI" } ] + # + def argumentize(args_field=nil) config = dup if args_field args = [config.delete(args_field)].flatten.compact else args = [] end args << config return args end + + alias_method :command_vector, :argumentize end