Module: Climate::ParsingMethods

Included in:
Command, Parser, Script
Defined in:
lib/climate/parser.rb

Instance Method Summary collapse

Instance Method Details

#arg(*args) ⇒ Object

Raises:



5
6
7
8
9
10
# File 'lib/climate/parser.rb', line 5

def arg(*args)
  raise DefinitionError, "can not define a required argument after an " +
    "optional one" if cli_arguments.any?(&:optional?)

  cli_arguments << Argument.new(*args)
end

#check_arguments(args, command = self) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/climate/parser.rb', line 44

def check_arguments(args, command=self)

  if args.size > cli_arguments.size
    raise UnexpectedArgumentError.new(command, "#{args.size} for #{cli_arguments.size}")
  end

  cli_arguments.zip(args).map do |argument, arg_value|
    if argument.required? && (arg_value.nil? || arg_value.empty?)
      raise MissingArgumentError.new(command, argument.name)
    end

    # no arg given is different to an empty arg
    if arg_value.nil?
      {}
    else
      {argument.name => arg_value}
    end
  end.inject {|a,b| a.merge(b) } || {}
end

#cli_argumentsObject



81
# File 'lib/climate/parser.rb', line 81

def cli_arguments ; @cli_arguments ||= [] ; end

#cli_optionsObject



80
# File 'lib/climate/parser.rb', line 80

def cli_options   ; @cli_options ||= []   ; end

#has_arguments?Boolean

Returns:

  • (Boolean)


84
# File 'lib/climate/parser.rb', line 84

def has_arguments? ;   not cli_arguments.empty? ; end

#has_options?Boolean

Returns:

  • (Boolean)


83
# File 'lib/climate/parser.rb', line 83

def has_options? ;     not cli_options.empty?   ; end

#help_banner(out = $stdout) ⇒ Object



40
41
42
# File 'lib/climate/parser.rb', line 40

def help_banner(out=$stdout)
  trollop_parser.educate(out)
end

#opt(*args) ⇒ Object



12
13
14
# File 'lib/climate/parser.rb', line 12

def opt(*args)
  cli_options << Option.new(*args)
end

#parse(arguments) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/climate/parser.rb', line 64

def parse(arguments)
  parser = self.trollop_parser
  options = parser.parse(arguments)

  # it would get weird if we allowed arguments alongside options, so
  # lets keep it one or t'other
  arguments, leftovers =
    if @stop_on
      [[], parser.leftovers]
    else
      [self.check_arguments(parser.leftovers), []]
    end

  [arguments, options, leftovers]
end

#stop_on(args) ⇒ Object



16
17
18
# File 'lib/climate/parser.rb', line 16

def stop_on(args)
  @stop_on = args
end

#trollop_parserObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/climate/parser.rb', line 20

def trollop_parser
  parser = Trollop::Parser.new

  parser.stop_on @stop_on

  if cli_arguments.size > 0
    parser.banner ""
    max_length = cli_arguments.map { |h| h.name.to_s.length }.max
    cli_arguments.each do |argument|
      parser.banner("  " + argument.name.to_s.rjust(max_length) + " - #{argument.description}")
    end
  end

  parser.banner ""
  cli_options.each do |option|
    option.add_to(parser)
  end
  parser
end