Module: Helpema::Piper
Defined Under Namespace
Modules: Refinements
Class Method Summary collapse
-
.run_command(cmd, options = {}, script: nil, usage: nil, synonyms: nil, mode: 'r', exception: nil, forward_pass: nil, **popt, &blk) ⇒ Object
Assume caller knows what it’s doing(no dummy proofing here).
- .to_arg(key, value) ⇒ Object
- .validate_command(cmd, version, flag = '--version') ⇒ Object
Instance Method Summary collapse
-
#define_command(name, cmd: name.to_s.chomp('?').chomp('!'), version: nil, v: nil, usage: nil, synonyms: nil, mode: 'r', exception: nil, default: nil, **popt, &forward_pass) ⇒ Object
Note: popt is IO.popen’s options(think of it as “pipe’s options”).
Class Method Details
.run_command(cmd, options = {}, script: nil, usage: nil, synonyms: nil, mode: 'r', exception: nil, forward_pass: nil, **popt, &blk) ⇒ Object
Assume caller knows what it’s doing(no dummy proofing here)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/helpema/piper.rb', line 78 def Piper.run_command(cmd, ={}, script:nil, usage:nil, synonyms:nil, mode:'r', exception:nil, forward_pass:nil, **popt, &blk) args,ret = .to_args(usage:usage,synonyms:synonyms),nil $stderr.puts "#{cmd} #{args.join(' ')}" if $DEBUG IO.popen([cmd, *args], mode, **popt) do |pipe| pipe.write script if script if forward_pass ret = forward_pass.call(pipe, , blk) elsif blk ret = blk.call(pipe, ) elsif mode=='r' ret = pipe.read elsif mode=='w+' pipe.close_write ret = pipe.read end end # False exception instead of nil or "an error message" # flags the expected behavior as defined: success = ($?.exitstatus==0) raise(exception) if exception and not success return success if exception==false return ret end |
.to_arg(key, value) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/helpema/piper.rb', line 63 def Piper.to_arg(key,value) # keep only keys with value(no false or nil) return nil unless value # assume nil0/--long/-short key = key.to_flag if key return key if value==true # a flag return "#{key}#{value}" if key[-1]=='=' # joined key=value return [key,value.to_s] end # It's a Nth arg, like arg0... return value.to_s end |
.validate_command(cmd, version, flag = '--version') ⇒ Object
34 35 36 37 |
# File 'lib/helpema/piper.rb', line 34 def Piper.validate_command(cmd, version, flag='--version') raise "`#{cmd} #{flag}` !~ #{version}" unless `#{cmd} #{flag}`.strip.match?(version) end |
Instance Method Details
#define_command(name, cmd: name.to_s.chomp('?').chomp('!'), version: nil, v: nil, usage: nil, synonyms: nil, mode: 'r', exception: nil, default: nil, **popt, &forward_pass) ⇒ Object
Note: popt is IO.popen’s options(think of it as “pipe’s options”).
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/helpema/piper.rb', line 40 def define_command(name, cmd:name.to_s.chomp('?').chomp('!'), version:nil, v:nil, usage:nil, synonyms:nil, mode:'r', exception:nil, default:nil, **popt, &forward_pass) raise "bad name or cmd" unless name=~/^\w+[?!]?$/ and cmd=~/^[\w.\-]+$/ Piper.validate_command(cmd, version) if version Piper.validate_command(cmd, v, '-v') if v define_method(name) do |script=default, **, &blk| if mode[0]=='w' raise 'need script to write' unless script or blk or forward_pass else raise 'cannot write script' if script end Piper.run_command(cmd, , script:script, usage:usage, synonyms:synonyms, mode:mode, exception:exception, forward_pass:forward_pass, **popt, &blk) end end |