Class: Rbkb::Cli::Hexify

Inherits:
Executable show all
Defined in:
lib/rbkb/cli/hexify.rb

Overview

Copyright 2009 emonti at matasano.com See README.rdoc for license information

The hexify command converts a string or raw data to hex characters. Input can be supplied via stdin, a string argument, or a file (with -f).

Instance Attribute Summary

Attributes inherited from Executable

#argv, #exit_status, #oparse, #opts, #stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Executable

#bail, #bail_args, #exit, #initialize, run

Constructor Details

This class inherits a constructor from Rbkb::Cli::Executable

Instance Method Details

#go(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbkb/cli/hexify.rb', line 47

def go(*args)
  super(*args)

  # Default to standard input
  @opts[:indat] ||= @stdin.read() 

  indat = @opts.delete(:indat)
  len = (@opts.delete(:len) || indat.length)

  bail("Length must be greater than zero") unless(len > 0)

  until (m = indat.slice!(0..len-1)).empty?
    @stdout << m.hexify(@opts)
    @stdout.puts((opts[:delim] and ! indat.empty?)? opts[:delim] : "\n")
  end
  self.exit(0)
end

#make_parserObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbkb/cli/hexify.rb', line 9

def make_parser
  super()
  add_std_file_opt(:indat)
  arg = @oparse

  # Add local options
  arg.banner += " <data | blank for stdin>"

  arg.on("-l", "--length LEN", Numeric, "Output lines of LEN bytes") do |l|
    @opts[:len] = l
  end

  arg.on("-d", "--delim=DELIMITER", "DELIMITER between each byte") do |d|
    @opts[:delim] = d
  end

  arg.on("-p", "--prefix=PREFIX", "PREFIX before each byte") do |p|
    @opts[:prefix] = p
  end

  arg.on("-s", "--suffix=SUFFIX", "SUFFIX after each byte") do |s|
    @opts[:suffix] = s
  end
end

#parse(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbkb/cli/hexify.rb', line 34

def parse(*args)
  super(*args)

  # blackbag-style space delimiter compatability
  if @argv[0] == "+" and @opts[:delim].nil?
    @opts[:delim]=" "
    @argv.shift
  end

  parse_string_argument(:indat)
  parse_catchall()
end