class Rbkb::Cli::Len

len prepends a binary length number in front of its input and outputs raw on STDOUT

Public Class Methods

new(*args) { |this| ... } click to toggle source
Calls superclass method Rbkb::Cli::Executable::new
# File lib/rbkb/cli/len.rb, line 10
def initialize(*args)
  # endianness pair. index 0 is always the default
  @endpair = [:big, :little]

  super(*args) do |this|
    {
      :nudge => 0, 
      :size => 4, 
      :endian => @endpair[0],
    }.each {|k,v| this.opts[k] ||= v}

    yield this if block_given?
  end
end

Public Instance Methods

go(*args) click to toggle source
Calls superclass method Rbkb::Cli::Executable#go
# File lib/rbkb/cli/len.rb, line 64
def go(*args)
  super(*args)
  unless len=@opts[:static]
    len = @opts[:indat].size
    len += @opts[:size] if @opts[:tot]
    len += @opts[:nudge]
  end
  @stdout << len.to_bytes(@opts[:endian], @opts[:size]) << @opts[:indat]
  self.exit(0)
end
make_parser() click to toggle source
Calls superclass method Rbkb::Cli::Executable#make_parser
# File lib/rbkb/cli/len.rb, line 25
def make_parser()
  super()
  add_std_file_opt(:indat)
  arg = @oparse
  arg.banner += " <data | blank for stdin>"

  arg.on("-n", "--nudge INT", Numeric, "Add integer to length") do |n|
    @opts[:nudge] += n
  end

  arg.on("-s", "--size=SIZE", Numeric, 
         "Size of length field in bytes") do |s|
    bail("Size must be greater than 0") unless (@opts[:size] = s) > 0
  end

  arg.on("-x", "--[no-]swap", 
         "Swap endianness. Default=#{@opts[:endian]}") do |x|
    @opts[:endian] = @endpair[(x)? 1 : 0]
  end

  arg.on("-t", "--[no-]total", "Include size word in size") do |t|
    @opts[:tot]=t
  end

  arg.on("-l", "--length=LEN", Numeric, 
         "Ignore all other flags and use static LEN") do |l|
    @opts[:static]=l
  end
end
parse(*args) click to toggle source
Calls superclass method Rbkb::Cli::Executable#parse
# File lib/rbkb/cli/len.rb, line 56
def parse(*args)
  super(*args)
  @opts[:indat] ||= @argv.shift
  parse_catchall()
  @opts[:indat] ||= @stdin.read
end