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