Class: CPF::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, stdin, stdout, stderr) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(arguments, stdin, stdout, stderr)
  @arguments = arguments
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/cpf/cli.rb', line 3

def arguments
  @arguments
end

#stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



4
5
6
# File 'lib/cpf/cli.rb', line 4

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



5
6
7
# File 'lib/cpf/cli.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#check(cpf) ⇒ Object



68
69
70
# File 'lib/cpf/cli.rb', line 68

def check(cpf)
  exit
end

#format(cpf) ⇒ Object



84
85
86
87
# File 'lib/cpf/cli.rb', line 84

def format(cpf)
  stdout << cpf.formatted
  exit
end

#generate(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cpf/cli.rb', line 72

def generate(options)
  cpf = CPF.new(Generator.generate)

  if options[:strip]
    stdout << cpf.stripped
  else
    stdout << cpf.formatted
  end

  exit
end

#helpObject



94
95
96
97
# File 'lib/cpf/cli.rb', line 94

def help
  stderr << opts.to_s
  exit 1
end

#optsObject



99
100
101
# File 'lib/cpf/cli.rb', line 99

def opts
  @opts ||= OptionParser.new
end

#startObject



15
16
17
18
19
20
21
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
# File 'lib/cpf/cli.rb', line 15

def start
  options = {}

  opts.banner = "Usage: cpf [options] [CPF number]"
  opts.separator ""
  opts.separator "Specific options:"

  opts.on("-c", "--check", "Check if CPF is valid") do
    options[:check] = true
  end

  opts.on("-g", "--generate", "Generate a new CPF") do
    options[:generate] = true
  end

  opts.on("-f", "--format", "Format CPF with separators") do
    options[:format] = true
  end

  opts.on("-s", "--strip", "Remove CPF separators") do
    options[:strip] = true
  end

  opts.on("-v", "--version", "Show version") do
    stdout << VERSION
    exit
  end

  opts.on_tail("-h", "--help", "Show help") do
    help
    exit
  end

  opts.parse!(arguments)
  opts.permute!(arguments)

  help if options.empty?
  generate(options) if options[:generate]

  cpf = CPF.new(arguments.first || stdin.read)

  validate(cpf)
  format(cpf) if options[:format]
  strip(cpf) if options[:strip]
  check(cpf) if options[:check]
end

#strip(cpf) ⇒ Object



89
90
91
92
# File 'lib/cpf/cli.rb', line 89

def strip(cpf)
  stdout << cpf.stripped
  exit
end

#validate(cpf) ⇒ Object



62
63
64
65
66
# File 'lib/cpf/cli.rb', line 62

def validate(cpf)
  return if cpf.valid?
  stderr << "Error: Invalid number\n"
  exit 1
end