|
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 import java.util.ArrayList; |
|
29 import com.oracle.solaris.vp.util.misc.TextUtil; |
|
30 |
|
31 public abstract class OptionFormatter { |
|
32 // |
|
33 // Static data |
|
34 // |
|
35 |
|
36 public static final String OPT_LIST_DELIM = " "; |
|
37 public static final String OPT_NAME_DELIM = ","; |
|
38 public static final String OPT_CHOICE_DELIM = " | "; |
|
39 public static final String OPT_REQUIRED_ENCLOSE_LEFT = "["; |
|
40 public static final String OPT_REQUIRED_ENCLOSE_RIGHT = "]"; |
|
41 public static final String OPT_GROUP_ENCLOSE_LEFT = "("; |
|
42 public static final String OPT_GROUP_ENCLOSE_RIGHT = ")"; |
|
43 public static final String OPT_ARG_ENCLOSE_LEFT = "<"; |
|
44 public static final String OPT_ARG_ENCLOSE_RIGHT = ">"; |
|
45 public static final String OPT_REPEAT = "..."; |
|
46 |
|
47 // |
|
48 // OptionFormatter methods |
|
49 // |
|
50 |
|
51 public String getFormatted(Option opt, boolean allNames) { |
|
52 return getFormatted(opt, allNames, false, true); |
|
53 } |
|
54 |
|
55 public String getFormatted(String opt) { |
|
56 return getFormatted(new String[] {opt}); |
|
57 } |
|
58 |
|
59 public String getFormatted(String opt, String optArg) { |
|
60 return getFormatted(new String[] {opt}, optArg); |
|
61 } |
|
62 |
|
63 public abstract String getFormatted(String[] opts); |
|
64 |
|
65 public abstract String getFormatted(String[] opts, String optArg); |
|
66 |
|
67 // |
|
68 // Private methods |
|
69 // |
|
70 |
|
71 private String getFormatted(Option opt, boolean allNames, |
|
72 boolean showGroup, boolean required) { |
|
73 |
|
74 StringBuilder buffer = new StringBuilder(); |
|
75 |
|
76 if (!required) { |
|
77 buffer.append(OPT_REQUIRED_ENCLOSE_LEFT); |
|
78 showGroup = false; |
|
79 } |
|
80 |
|
81 if (opt instanceof OptionElement) { |
|
82 OptionElement element = (OptionElement)opt; |
|
83 |
|
84 buffer.append(allNames ? |
|
85 getFormatted(element.getNames(), element.getArgName()) : |
|
86 getFormatted(element.getName(), element.getArgName())); |
|
87 |
|
88 if (element.getUseLimit() < 0) { |
|
89 buffer.append(OPT_REPEAT); |
|
90 } |
|
91 } else |
|
92 |
|
93 if (opt instanceof OptionGroup) { |
|
94 ArrayList<String> list = new ArrayList<String>(); |
|
95 String delim = opt instanceof OptionChoiceGroup ? |
|
96 OPT_CHOICE_DELIM : OPT_LIST_DELIM; |
|
97 |
|
98 for (Option subOpt : ((OptionGroup)opt).getOptions()) { |
|
99 if (subOpt.isDocumented()) { |
|
100 list.add(getFormatted(subOpt, allNames, showGroup, |
|
101 opt instanceof OptionChoiceGroup ? |
|
102 true : subOpt.isRequired())); |
|
103 } |
|
104 } |
|
105 |
|
106 if (showGroup) { |
|
107 buffer.append(OPT_GROUP_ENCLOSE_LEFT); |
|
108 } |
|
109 |
|
110 buffer.append(TextUtil.join(delim, list)); |
|
111 |
|
112 if (showGroup) { |
|
113 buffer.append(OPT_GROUP_ENCLOSE_RIGHT); |
|
114 } |
|
115 } |
|
116 |
|
117 if (!required) { |
|
118 buffer.append(OPT_REQUIRED_ENCLOSE_RIGHT); |
|
119 } |
|
120 |
|
121 return buffer.toString(); |
|
122 } |
|
123 } |