Class: UpcGen::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/upc_gen/shell.rb

Constant Summary collapse

<<~"MSG".freeze
  usage: #{$PROGRAM_NAME} <optional-number-string><optional-char>

  Prints a random UPC code

  If a number string is passed, the code before the check digit will end with those digits
  If the string of numbers is followed by a '.', the random code will start with those digits
  If the string of numbers is followed by a '-', a code will be found that ends with those digits, including check digit
MSG

Class Method Summary collapse

Class Method Details

.gather_options(argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/upc_gen/shell.rb', line 20

def self.gather_options(argv)
  options = {
    show_version: false
  }

  OptionParser.new { |parser|
    parser.banner = BANNER

    parser.on("-v", "--version", "Show version") do |version|
      options[:version] = version
    end
  }.parse! argv
  options
end

.start(argv, out: $stdout, err: $stderr) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



35
36
37
38
39
40
41
# File 'lib/upc_gen/shell.rb', line 35

def self.start(argv, out: $stdout, err: $stderr) # rubocop:disable Lint/UnusedMethodArgument
  options = gather_options(argv)

  out.puts "version: #{UpcGen::VERSION}" if options.delete(:version)

  out.puts UpcGenerator.new(argv[0]).generate
end

.usage(err: $stderr) ⇒ Object



15
16
17
18
# File 'lib/upc_gen/shell.rb', line 15

def self.usage(err: $stderr)
  err.puts BANNER
  exit 1
end