Module: ShellHelpers::ShConfig

Extended by:
ShConfig
Included in:
ShConfig
Defined in:
lib/shell_helpers/sh.rb

Overview

this modules deal with default options, paths and arg handling for different programs, while letting the user control Exemples: ShConfig.launch(:ls, "/", config: {default_opts: ["-l"]}) => ["ls", "-l", "/", {}] ShConfig.launch(:ls, "/", config: {default_opts: ["-l"]}, method: :sh) ShConfig.launch(:ls, "/", config: {default_opts: ["-l"]}) { |*args| SH.sh(*args) } ShConfig.launch(:ls, "/", config: {ls: {wrap: ->(cmd,*args, &b) { b.call(cmd, '-l', *args) } }}, method: :sh)

Instance Method Summary collapse

Instance Method Details

#launch(*args, opts: [], cmd_prepend: [], cmd_postpone: [], config: self.sh_config, default_opts: true, method: nil, **keywords, &b) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/shell_helpers/sh.rb', line 415

def launch(*args, opts: [], cmd_prepend: [], cmd_postpone: [], config: self.sh_config, default_opts: true, method: nil, **keywords, &b)
  if args.length == 1 and (arg=args.first).is_a?(String)
    args=arg.shellsplit
    args[0]=args[0][1..-1].to_sym if args[0][0]==':'
  end
  opts=opts.shellsplit if opts.is_a?(String)
  default_opts=default_opts.shellsplit if default_opts.is_a?(String)
  cmd_prepend=cmd_prepend.shellsplit if cmd_prepend.is_a?(String)
  cmd_postpone=cmd_postpone.shellsplit if cmd_postpone.is_a?(String)
  dopts = default_opts.is_a?(Array) ? default_opts : []
  cmd, *args=args
  if cmd.is_a?(Symbol)
    if config.key?(cmd)
      c=config[cmd]
      cmd=c[:bin] || cmd.to_s
      dopts += (Array(c[:default_opts])||[]) if default_opts
      wrap=c[:wrap]
    else
      cmd=cmd.to_s
    end
  end
  cargs=Array(cmd_prepend) + [cmd] + dopts + Array(opts) + args + Array(cmd_postpone)
  if !b
    if method
      b=lambda do |*args, **kw|
        SH.public_send(method, *args, **kw)
      end
    else
      b=lambda do |*args|
        return *args
      end
    end
  end
  if wrap
    wrap.call(*cargs, **keywords, &b)
  else
    b.call(*cargs, **keywords)
  end
end

#sh_configObject



455
456
457
# File 'lib/shell_helpers/sh.rb', line 455

def sh_config
  {}
end