module Helpema

Constants

VERSION

Public Instance Methods

classify(hash: Hash.new{|h,k|h[k]=[]}) click to toggle source
# File lib/helpema/helpema.rb, line 25
def classify(hash: Hash.new{|h,k|h[k]=[]})
  self.each{|v| hash[v.class] << v}
  return hash
end
define_command(name, cmd: name.to_s, version: nil, v: nil, usage: nil, synonyms: nil, mode: 'r',exception: nil, **popt, &forward_pass) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
satisfies?(*reqs) click to toggle source
# File lib/helpema/helpema.rb, line 3
def satisfies?(*reqs)
  Gem::Requirement.new(*reqs).satisfied_by? Gem::Version.new(self)
end
to_arg() click to toggle source
# File lib/helpema/helpema.rb, line 29
def to_arg
  Helpema.to_arg(*self)
end
to_args(usage:nil, synonyms:nil) click to toggle source
# File lib/helpema/helpema.rb, line 8
def to_args(usage:nil, synonyms:nil)
  # create separate args from self with the translated synonyms
  args = self.transform_keys(synonyms.to_h)
  # pad usage's defaults to args
  usage&.each{|key,default| args[key]=default unless args.has_key? key}
  # order might be important so enforce usage
  args = usage&.map{|k,v|[k,args[k]]} || args.to_a
  # convert key,value tuples to final list of args
  args.map!(&:to_arg)
  # get rid of nil
  args.compact!
  # ...and finally flatten!
  args.flatten!
  return args
end
to_flag() click to toggle source
# File lib/helpema/helpema.rb, line 34
def to_flag
  return nil if self[-1].match?(/\d/) # like :arg0
  (self.length > 1)? "--#{self}": "-#{self}" # like --verbose or -V
end