Module: Helpema

Extended by:
Helpema
Included in:
Helpema, FFMPEG, GPG, SSSS, YouTubeDL, ZBar
Defined in:
lib/helpema.rb,
lib/helpema/gpg.rb,
lib/helpema/ssss.rb,
lib/helpema/zbar.rb,
lib/helpema/ffmpeg.rb,
lib/helpema/helpema.rb,
lib/helpema/youtubedl.rb

Defined Under Namespace

Modules: FFMPEG, GPG, SSSS, YouTubeDL, ZBar

Constant Summary collapse

VERSION =
'3.2.210924'

Instance Method Summary collapse

Instance Method Details

#define_command(name, cmd: name.to_s, version: nil, v: nil, usage: nil, synonyms: nil, mode: 'r', exception: nil, **popt, &forward_pass) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/helpema/helpema.rb', line 68

def define_command(name,
  cmd: name.to_s, version: nil, v: nil,
  usage: nil, synonyms: nil,
  mode: 'r',exception: nil,
  **popt, &forward_pass)

  # which version? --version or -v
  if version and not `#{cmd} --version`.strip.match?(version)
    raise "`#{cmd} --version` !~ #{version}"
  end
  if v and not `#{cmd} -v`.strip.match?(v)
    raise "`#{cmd} -v` !~ #{v}"
  end

  define_method(name) do |**options, &blk|
    run_command(cmd, options,
      usage:usage, synonyms:synonyms, mode:mode,
      exception:exception, forward_pass:forward_pass, **popt, &blk)
  end
end

#requires(*list) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/helpema/helpema.rb', line 89

def requires(*list)
  loaded = []
  list.each do |gems|
    gems.lines.each do |gemname_reqs|
      gemname, *reqs = gemname_reqs.split
      next unless gemname
      unless reqs.empty?
        case gemname
        when 'helpema'
          unless VERSION.satisfies?(*reqs)
            raise "helpema #{VERSION} not #{reqs.join(', ')}"
          end
          next
        when 'ruby'
          unless RUBY_VERSION.satisfies?(*reqs)
            raise "ruby #{RUBY_VERSION} not #{reqs.join(', ')}"
          end
          next
        else
          gem gemname, *reqs
        end
      end
      require gemname and loaded.push gemname
    end
  end
  return loaded
end

#run_command(cmd, options = {}, usage: nil, synonyms: nil, mode: 'r', exception: nil, forward_pass: nil, **popt, &blk) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/helpema/helpema.rb', line 55

def run_command(cmd, options={},
  usage:nil, synonyms:nil, mode:'r',
  exception:nil, forward_pass:nil, **popt, &blk)
  args,ret = options.to_args(usage:usage,synonyms:synonyms),nil
  $stderr.puts "#{cmd} #{args.join(' ')}" if $DEBUG
  IO.popen([cmd, *args], mode, **popt) do |pipe|
    ret = forward_pass ? forward_pass.call(pipe, options, blk) :
                   blk ? blk.call(pipe) :
                         pipe.read
  end
  (exception.nil? or $?.exitstatus==0)? ret : raise(exception)
end

#to_arg(key, value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/helpema/helpema.rb', line 41

def 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