class Object
Public Instance Methods
_fmtargs(args, block_given?) { yield }
merge args with any block results provide default format string if only one argument
# File lib/arg-utils.rb, line 45 def _fmtargs args, flag args.concat([yield].flatten) if flag args.unshift('%s') if args.size < 2 args end
_msgargs(args, block_given?) { yield }
merge args with any block results
# File lib/arg-utils.rb, line 34 def _msgargs args, flag args.concat([yield].flatten) if flag args end
_prefix_arg0
( args, prefix )
Prefix args if the prefix is not already there, and if the args value is not nill.
# File lib/talk-utils.rb, line 194 def _prefix_arg0(args, prefix) if args.size > 0 && !args[0].nil? && !args[0].include?(prefix) args[0] = prefix + args[0] end args end
# File lib/run-utils.rb, line 67 def cmd_run *args args = _msgargs(args, block_given?) { yield } if $norun nrvtalk(args.first) elsif args.size > 0 safe_run(*args) end end
dtalk - “debug talk” Print msg on STDERR only if `$debug` is set
# File lib/talk-utils.rb, line 58 def dtalk *args if $debug && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end
# File lib/talk-utils.rb, line 64 def dtalkf *args if $debug && (args.size> 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield }) end end
error – print an error message on STDERR, and then exit.
Code defaults to 1 if not given.
# File lib/error-utils.rb, line 29 def error *args args = _msgargs(args, block_given?) { yield } code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1 $stderr.puts(*args) $stderr.flush exit code end
errorf – print a formatted message on STDERR, and then exit
# File lib/error-utils.rb, line 46 def errorf *args args = _fmtargs(args, block_given?) { yield } # default the error code to 1 unless the first argument is a Fixnum code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1 $stderr.printf(*args) $stderr.flush exit code end
# File lib/lookup.rb, line 37 def key_lookup list, key, err_notfound="%s not found\n", err_ambig="%s is ambiguous\n" keylist = list.is_a?(Hash) ? list.keys : list if exact = keylist.grep(/^#{key}$/i) # exact match? return exact.shift if exact && exact.size == 1 end keys = keylist.grep(/^#{key}/i) case keys.size when 0 unless err_notfound.nil? raise LookupNotFoundError, sprintf(err_notfound, key) end return nil when 1 return keys[0] else unless err_ambig.nil? raise LookupAmbigError, sprintf(err_ambig, key) end return keys end end
nrtalk – “no run” talk Print msg, prefixed with “(norun) ”, on STDERR only if `$norun` is set
# File lib/talk-utils.rb, line 147 def nrtalk *args if $norun && (args.size > 0 || block_given?) $stderr.puts(*_prefix_arg0(_msgargs(args, block_given?){yield},'(norun) ')) end end
# File lib/talk-utils.rb, line 153 def nrtalkf *args if $norun && (args.size > 0 || block_given?) $stderr.printf(*_prefix_arg0(_fmtargs(args, block_given?){yield},'(norun) ')) end end
nrvtalk – “no run” or verbose talk
Print msg, possibly prefixed with “(norun)” or “>>” (verbose), on STDERR only if $norun or $verbose are set.
# File lib/talk-utils.rb, line 171 def nrvtalk *args _args = _msgargs(args, block_given?) { yield } if $norun && _args.size > 0 nrtalk(*_args) elsif $verbose && _args.size > 0 vtalk(*_prefix_arg0(_args, '>> ')) end end
# File lib/talk-utils.rb, line 180 def nrvtalkf *args _args = _fmtargs(args, block_given?) { yield } if $norun && _args.size > 0 nrtalkf(*_args) elsif $verbose && _args.size > 0 vtalkf(*_prefix_arg0(_args, '>> ')) end end
nvtalk – “non-verbose” talk Print msg on STDERR unless `$verbose` is set
# File lib/talk-utils.rb, line 124 def nvtalk *args unless $verbose && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end
# File lib/talk-utils.rb, line 130 def nvtalkf *args unless $verbose && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end
qtalk - “quiet talk” print msg on STDERR only if `$quiet` is set
# File lib/talk-utils.rb, line 81 def qtalk *args if $quiet && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end
# File lib/talk-utils.rb, line 87 def qtalkf *args if $quiet && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end
run – run a command with support for testing, diagnostics and verbosity safe_run
– run a command with support for diagnostics and verbosity
Both may be given optional `errmsg` and `okmsg`, which are printed if given for the corresponding condition.
safe_run cmd safe_run cmd, errmsg safe_run cmd, errmsg, okmsg safe_run { cmd } safe_run { [cmd, errmsg] } safe_run { [cmd, errmsg, okmsg] }
if `$norun` is set, print `(norun) ` followed by `cmd` on `STDERR`, and return.
if `$verbose` is set, print `>> ` followed by `cmd` on `STDERR`.
Invoke the `cmd` with the `system()` call.
If there is an error, show the command (preceded by `>> `) if `$verbose` is not set, then show the error code, followed by the given `errmsg` or the default error message.
The `cmd` can be given either as an argument, or as the returned value from a block. Important: the block should return a string value to be passed to the system call.
# File lib/run-utils.rb, line 49 def safe_run *args args = _msgargs(args, block_given?) { yield } cmd, errmsg, okmsg = args vtalkf ">> %s\n", cmd if cmd if system cmd # invoke the command talk okmsg if okmsg return true else # an error occured qtalkf ">> %s\n", cmd erm = sprintf(errmsg ? errmsg : "Command failed with code %d", $?>>8) talk erm $stderr.flush raise SystemCallError, erm # instead of exit, use raise end end end
talk - Print msg on STDERR unless `$quiet` is set
# File lib/talk-utils.rb, line 35 def talk *args if !$quiet && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end
# File lib/talk-utils.rb, line 41 def talkf *args if !$quiet && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end
vtalk - “verbose talk” Print msg on STDERR if `$verbose` is set
# File lib/talk-utils.rb, line 104 def vtalk *args if $verbose && (args.size > 0 || block_given?) $stderr.puts(*_msgargs(args, block_given?) { yield }) end end
# File lib/talk-utils.rb, line 110 def vtalkf *args if $verbose && (args.size > 0 || block_given?) $stderr.printf(*_fmtargs(args, block_given?) { yield } ) end end