01: /*
02: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
03: Licensed under the Academic Free License version 3.0
04: */
05:
06: package joptsimple;
07:
08: /**
09: * Specification of an option that accepts an optional argument.
10: *
11: * @since 1.0
12: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
13: * @version $Id: OptionalArgumentOptionSpec.java,v 1.15 2007/04/10 20:06:25 pholser Exp $
14: */
15: class OptionalArgumentOptionSpec extends ArgumentAcceptingOptionSpec {
16: OptionalArgumentOptionSpec(String option) {
17: super (option, false);
18: }
19:
20: OptionalArgumentOptionSpec(String option, String description) {
21: super (option, false, description);
22: }
23:
24: /**
25: * {@inheritDoc}
26: */
27: protected void handleOptionFurther(OptionParser parser,
28: ArgumentList arguments, OptionSet detectedOptions) {
29:
30: if (arguments.hasMore()) {
31: String nextArgument = arguments.peek();
32:
33: if (!parser.looksLikeAnOption(nextArgument))
34: handleOptionArgument(parser, detectedOptions, arguments);
35: else if (isArgumentOfNumberType()
36: && canConvertArgument(nextArgument))
37: detectedOptions.addWithArgument(option(),
38: convert(arguments.next()));
39: else
40: detectedOptions.add(option());
41: } else
42: detectedOptions.add(option());
43: }
44:
45: private void handleOptionArgument(OptionParser parser,
46: OptionSet detectedOptions, ArgumentList arguments) {
47:
48: if (parser.isPosixlyCorrect()) {
49: detectedOptions.add(option());
50: parser.noMoreOptions();
51: } else
52: detectedOptions.addWithArgument(option(), convert(arguments
53: .next()));
54: }
55:
56: void accept(OptionSpecVisitor visitor) {
57: visitor.visit(this);
58: }
59: }
|