Class: CNPJ::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cnpj/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/cnpj/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/cnpj/cli.rb', line 3

def arguments
  @arguments
end

#stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



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

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Instance Method Details

#check(cnpj) ⇒ Object



67
68
69
# File 'lib/cnpj/cli.rb', line 67

def check(cnpj)
  exit
end

#format(cnpj) ⇒ Object



83
84
85
86
# File 'lib/cnpj/cli.rb', line 83

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

#generate(options) ⇒ Object



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

def generate(options)
  cnpj = CNPJ.new(Generator.generate)

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

  exit
end

#helpObject



93
94
95
96
# File 'lib/cnpj/cli.rb', line 93

def help
  stderr << opts.to_s
  exit 1
end

#optsObject



98
99
100
# File 'lib/cnpj/cli.rb', line 98

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
# File 'lib/cnpj/cli.rb', line 15

def start
  options = {}

  opts.banner = "Usage: cnpj [options] [CNPJ number]"
  opts.separator ""
  opts.separator "Specific options:"

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

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

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

  opts.on("-s", "--strip", "Remove CNPJ 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 and exit(1) if options.empty?
  generate(options) if options[:generate]

  cnpj = CNPJ.new(arguments.first || stdin.read)
  validate(cnpj)
  format(cnpj) if options[:format]
  strip(cnpj) if options[:strip]
  check(cnpj) if options[:check]
end

#strip(cnpj) ⇒ Object



88
89
90
91
# File 'lib/cnpj/cli.rb', line 88

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

#validate(cnpj) ⇒ Object



61
62
63
64
65
# File 'lib/cnpj/cli.rb', line 61

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