Class: Pass::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pass/cli.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#exec(argv) ⇒ Object



9
10
11
12
13
14
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
# File 'lib/pass/cli.rb', line 9

def exec(argv)
  options = {}
  options[:length] = Pass::DEFAULT_PASSWORD_LENGTH

  opts = OptionParser.new

  opts.on('-c [NUMBER]', '(deprecated) specify password length') do |value|
    options[:length] = option_to_i(value)
  end

  opts.on('-l', '--length [NUMBER]', 'specify password length') do |value|
    options[:length] = option_to_i(value)
  end

  opts.on('-s', '--symbols', 'include symbols') do |value|
    options[:symbols] = value
  end

  opts.on('-e', '--exclude [CHARACTERS]', 'exclude characters') do |value|
    options[:exclude] = value
  end

  opts.on_tail('-v', '--version', 'show version') do
    puts "pass #{Pass::VERSION}"
    exit 0
  end

  opts.banner = banner

  begin
    res_argv = opts.parse!(argv)
    num_passwords = if res_argv[0]
                      option_to_i(res_argv[0])
                    else
                      Pass::DEFAULT_NUM_PASSWORDS
                    end

    pass = Pass.new(**options)

    num_passwords.times do
      puts pass.generate
    end
  rescue Pass::Error, Pass::CLI::Error, OptionParser::ParseError => e
    abort "Error: #{e.message}"
  end

  exit 0
end