Module: Puppetserver::Ca::Utils

Defined in:
lib/puppetserver/ca/utils.rb

Class Method Summary collapse

Class Method Details

.handle_errors(log, errors, usage = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppetserver/ca/utils.rb', line 52

def self.handle_errors(log, errors, usage = nil)
  unless errors.empty?
    log.err 'Error:'
    errors.each {|e| log.err e }

    if usage
      log.err ''
      log.err usage
    end

    return true
  else
    return false
  end
end

.parse_with_errors(parser, args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppetserver/ca/utils.rb', line 40

def self.parse_with_errors(parser, args)
  errors = []

  _, non_flags, malformed_flags, unknown_flags = parse_without_raising(parser, args)

  malformed_flags.each {|f| errors << "    Missing argument to flag `#{f}`" }
  unknown_flags.each   {|f| errors << "    Unknown flag or argument `#{f}`" }
  non_flags.each       {|f| errors << "    Unknown input `#{f}`" }

  errors
end

.parse_without_raising(parser, args) ⇒ Object



4
5
6
7
8
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
# File 'lib/puppetserver/ca/utils.rb', line 4

def self.parse_without_raising(parser, args)
  all, not_flags, malformed_flags, unknown_flags = [], [], [], []

  begin
    # OptionParser calls this block when it finds a value that doesn't
    # start with one or two dashes and doesn't follow a flag that
    # consumes a value.
    parser.order!(args) do |not_flag|
      not_flags << not_flag
      all << not_flag
    end
  rescue OptionParser::MissingArgument => e
    malformed_flags += e.args
    all += e.args

    retry
  rescue OptionParser::ParseError => e
    flag = e.args.first
    unknown_flags << flag
    all << flag

    if does_not_contain_argument(flag) &&
        args.first &&
        next_arg_is_not_another_flag(args.first)

      value = args.shift
      unknown_flags << value
      all << value
    end

    retry
  end

  return all, not_flags, malformed_flags, unknown_flags
end