Sha256: b580a099defabe5a15196e7ad4a6db7c0946de5b4460dd299310cac9ecf02705

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

class CNPJ
  class CLI
    attr_accessor :arguments
    attr_accessor :stdin
    attr_accessor :stdout
    attr_accessor :stderr

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

    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

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

    def check(cnpj)
      exit
    end

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

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

      exit
    end

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

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

    def help
      stderr << opts.to_s
      exit 1
    end

    def opts
      @opts ||= OptionParser.new
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cpf_cnpj-0.2.1 lib/cnpj/cli.rb
cpf_cnpj-0.2.0 lib/cnpj/cli.rb
cpf_cnpj-0.1.0 lib/cnpj/cli.rb