1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.cli; |
|
27 |
|
28 /** |
|
29 * The <code>OptionMap</code> class provides a 1x1 mapping between a |
|
30 * <code>ParsedOption</code> and an <code>OptionElement</code>. |
|
31 */ |
|
32 public class OptionMap { |
|
33 // |
|
34 // Instance data |
|
35 // |
|
36 |
|
37 private ParsedOption parsed; |
|
38 private OptionElement option; |
|
39 |
|
40 // |
|
41 // Constructors |
|
42 // |
|
43 |
|
44 public OptionMap(ParsedOption parsed, OptionElement option) { |
|
45 setParsed(parsed); |
|
46 setOption(option); |
|
47 } |
|
48 |
|
49 // |
|
50 // OptionMap methods |
|
51 // |
|
52 |
|
53 public void setParsed(ParsedOption parsed) { |
|
54 this.parsed = parsed; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Gets the <code>ParsedOption</code> that was parsed from |
|
59 * the command line. |
|
60 */ |
|
61 public ParsedOption getParsed() { |
|
62 return parsed; |
|
63 } |
|
64 |
|
65 public void setOption(OptionElement option) { |
|
66 this.option = option; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Gets the <code>OptionElement</code> that matches the parsed |
|
71 * option. |
|
72 */ |
|
73 public OptionElement getOption() { |
|
74 return option; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Gets a formatted representation of this option. |
|
79 */ |
|
80 public String getFormatted(OptionFormatter formatter) { |
|
81 return getFormatted(parsed, option, formatter); |
|
82 } |
|
83 |
|
84 // |
|
85 // Static methods |
|
86 // |
|
87 |
|
88 /** |
|
89 * Gets a formatted representation of this option. |
|
90 */ |
|
91 public static String getFormatted(ParsedOption parsed, OptionElement option, |
|
92 OptionFormatter formatter) { |
|
93 |
|
94 // Return the formatted option if one was specified |
|
95 String opt = parsed.getOpt(); |
|
96 if (opt != null && !opt.isEmpty()) { |
|
97 return formatter.getFormatted(opt); |
|
98 } |
|
99 |
|
100 // Return the option argument if one was specified |
|
101 String optArg = parsed.getOptArg(); |
|
102 if (optArg != null && !optArg.isEmpty()) { |
|
103 return "\"" + optArg + "\""; |
|
104 } |
|
105 |
|
106 // Return the formatted option from the option definition |
|
107 return formatter.getFormatted(option, false); |
|
108 } |
|
109 } |
|