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: * <p>Wraps an option specification, and allows callers to specify whether the option
10: * accepts arguments (required or optional).</p>
11: *
12: * <p>Instances are returned from {@link OptionParser#accepts(String)} to allow the
13: * formation of parser directives as sentences in a domain-specific language. For
14: * example:</p>
15: *
16: * <pre>
17: * OptionParser parser = new OptionParser();
18: * parser.accepts( "c" ).<strong>withRequiredArg()</strong>.ofType( Integer.class );
19: * </pre>
20: *
21: * <p>If no methods are invoked on an instance of this class, then that instance's option
22: * will accept no argument.</p>
23: *
24: * @since 2.0
25: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
26: * @version $Id: OptionSpecBuilder.java,v 1.16 2007/04/10 20:06:25 pholser Exp $
27: */
28: public class OptionSpecBuilder {
29: private final OptionParser parser;
30: private final String option;
31: private final String description;
32:
33: OptionSpecBuilder(OptionParser parser, String option,
34: String description) {
35: this .parser = parser;
36: this .option = option;
37: this .description = description;
38:
39: parser.recognize(new NoArgumentOptionSpec(option, description));
40: }
41:
42: /**
43: * Informs an option parser that this builder's option requires an argument.
44: *
45: * @return a specification for the option
46: */
47: public ArgumentAcceptingOptionSpec withRequiredArg() {
48: ArgumentAcceptingOptionSpec newSpec = new RequiredArgumentOptionSpec(
49: option, description);
50: parser.recognize(newSpec);
51:
52: return newSpec;
53: }
54:
55: /**
56: * Informs an option parser that this builder's option accepts an optional argument.
57: *
58: * @return a specification for the option
59: */
60: public ArgumentAcceptingOptionSpec withOptionalArg() {
61: ArgumentAcceptingOptionSpec newSpec = new OptionalArgumentOptionSpec(
62: option, description);
63: parser.recognize(newSpec);
64:
65: return newSpec;
66: }
67: }
|