Class: CNPJ::CLI
- Inherits:
-
Object
- Object
- CNPJ::CLI
- Defined in:
- lib/cnpj/cli.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Instance Method Summary collapse
- #check(cnpj) ⇒ Object
- #format(cnpj) ⇒ Object
- #generate(options) ⇒ Object
- #help ⇒ Object
-
#initialize(arguments, stdin, stdout, stderr) ⇒ CLI
constructor
A new instance of CLI.
- #opts ⇒ Object
- #start ⇒ Object
- #strip(cnpj) ⇒ Object
- #validate(cnpj) ⇒ Object
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
#arguments ⇒ Object
Returns the value of attribute arguments.
3 4 5 |
# File 'lib/cnpj/cli.rb', line 3 def arguments @arguments end |
#stderr ⇒ Object
Returns the value of attribute stderr.
6 7 8 |
# File 'lib/cnpj/cli.rb', line 6 def stderr @stderr end |
#stdin ⇒ Object
Returns the value of attribute stdin.
4 5 6 |
# File 'lib/cnpj/cli.rb', line 4 def stdin @stdin end |
#stdout ⇒ Object
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() cnpj = CNPJ.new(Generator.generate) if [:strip] stdout << cnpj.stripped else stdout << cnpj.formatted end exit end |
#help ⇒ Object
93 94 95 96 |
# File 'lib/cnpj/cli.rb', line 93 def help stderr << opts.to_s exit 1 end |
#opts ⇒ Object
98 99 100 |
# File 'lib/cnpj/cli.rb', line 98 def opts @opts ||= OptionParser.new end |
#start ⇒ Object
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 = {} opts. = "Usage: cnpj [options] [CNPJ number]" opts.separator "" opts.separator "Specific options:" opts.on("-c", "--check", "Check if CNPJ is valid") do [:check] = true end opts.on("-g", "--generate", "Generate a new CNPJ") do [:generate] = true end opts.on("-f", "--format", "Format CNPJ with separators") do [:format] = true end opts.on("-s", "--strip", "Remove CNPJ separators") do [: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 .empty? generate() if [:generate] cnpj = CNPJ.new(arguments.first || stdin.read) validate(cnpj) format(cnpj) if [:format] strip(cnpj) if [:strip] check(cnpj) if [: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 |