Class: ChRad::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/chrad/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CLI



8
9
10
11
12
# File 'lib/chrad/cli.rb', line 8

def initialize(argv = ARGV)
  @argv = argv.map do |x|
    x.to_s
  end
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



6
7
8
# File 'lib/chrad/cli.rb', line 6

def argv
  @argv
end

Instance Method Details

#inputObject



14
15
16
# File 'lib/chrad/cli.rb', line 14

def input
  @input ||= ChRad::InputManager.new
end

#optparseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chrad/cli.rb', line 22

def optparse
  @optparse ||= OptionParser.new do |opts|
    opts.banner = "Converts integers into a different base (radix)."
    opts.define_head "Usage: #{$0} [options] -i <input_base> -o <output_base> <number> [...]"
    opts.summary_width = 24
    opts.summary_indent = '  '

    opts.separator ''
    opts.separator 'REQUIRED OPTIONS'
    opts.separator ''

    opts.on('-iBASE', '--input=BASE',
            'Interpret input strings integers in base <BASE>'
           ) do |base|
      input.base = base
    end

    opts.on('-oBASE', '--output=BASE',
            'Print converted numbers as integers in base <BASE>'
           ) do |base|
      output.base = base
    end


    opts.separator ''
    opts.separator 'INPUT/OUTPUT FORMAT'
    opts.separator ''

    opts.on('--input-list',
            'Input values as delimiter separated lists'
           ) do
      input.mode = :list
    end

    opts.on('--output-list',
            'Output values as delimiter separated lists'
           ) do
      output.mode = :list
    end

    opts.on('-l', '--list',
            'Both input and output values as delimiter separated lists',
            '(Shorthand for using both --input-list and --output-list)'
           ) do
      input.mode = :list
      output.mode = :list
    end

    opts.on('--input-sep=CHAR',
            'Use <CHAR> as the delimiter between places',
            'in input vales when using --input-list mode'
           ) do |sep|
      input.separator = sep
    end

    opts.on('--output-sep=CHAR',
            'Use <CHAR> as the delimiter between places',
            'in output vales when using --output-list mode'
           ) do |sep|
      output.separator = sep
    end

    opts.on('-s', '--separator=CHAR',
            'Use <CHAR> as the delimiter between places in both',
            'input and output list modes. (shothand for using',
            'both --input-separator=CHAR and --output-separator=CHAR)'
           ) do |sep|
      input.separator = sep
      output.separator = sep
    end

    opts.separator ''
    opts.separator 'DIGIT ALPHABET'
    opts.separator ''

    opts.on('--input-digits=STR',
            'Use the given <STR> as the set of chars to use',
            'when interpreting input integers.',
            '(Ignored when using--input-list mode.)'
           ) do |str|
      input.alphabet = str
    end

    opts.on('--output-digits=STR',
            'Use the given <STR> as the set of chars to use',
            'when printing number in the output base.',
            '(Ignored when using--input-list mode.)'
           ) do |str|
      output.alphabet = str
    end

    opts.on('-d', '--digits=STR',
            'Use the given <STR> as the set of chars to use',
            'for both input and output. (shorthand for using',
            'both --input-digits=STR and --output-digits=STR)'
           ) do |str|
      input.alphabet = str
      output.alphabet = str
    end


    opts.separator ''

    opts.on('--stdin',
            'Read one number per line from STDIN instead',
            'of ARGV. Equivalent to using the single',
            'character "-" as the first arg.'
           ) do
      input.force_stdin = true 
    end

    opts.on('--list-named-digits',
            'List the built-in named digit sets. These names',
            'can be passed to any of --digits, --input-digits,',
            'or --output-digits using the \"name:\" prefix.',
            '(example: \"--digits=base64\")'
           ) do
      Alphabet.all.each_pair do |name, digits|
        puts "name:#{name}\t#{digits.join('')}"
      end
      exit
    end

    opts.on('-h', '--help', 'Show this help message') do
      puts opts
      exit
    end

    opts.on('--version', 'Show version') do
      puts ChRad::VERSION
      exit
    end
  end
end

#outputObject



18
19
20
# File 'lib/chrad/cli.rb', line 18

def output
  @output ||= ChRad::OutputManager.new
end

#run!Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/chrad/cli.rb', line 157

def run!
  optparse.parse!(argv)

  if argv.length > 0
    unless argv.first == '-'
      unless input.force_stdin
        input.stream = argv            
      end
    end
  else
    unless input.force_stdin
      puts optparse
      exit 1
    end
  end

  input.setup_base_digits!
  output.setup_base_digits!

  input.stream.each do |arg|
    arg = arg.chomp
    number = input.parse(arg)
    output.stream_puts(number)
  end
end