author | Stephen Talley <stephen.talley@oracle.com> |
Mon, 28 Mar 2011 10:53:34 -0400 | |
changeset 685 | 767674b0a2fb |
parent 219 | 57841c113efe |
permissions | -rw-r--r-- |
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
1 |
/* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
2 |
* CDDL HEADER START |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
3 |
* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
4 |
* The contents of this file are subject to the terms of the |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
5 |
* Common Development and Distribution License (the "License"). |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
6 |
* You may not use this file except in compliance with the License. |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
7 |
* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
9 |
* or http://www.opensolaris.org/os/licensing. |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
10 |
* See the License for the specific language governing permissions |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
11 |
* and limitations under the License. |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
12 |
* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
18 |
* |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
19 |
* CDDL HEADER END |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
20 |
*/ |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
21 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
22 |
/* |
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
23 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
24 |
*/ |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
25 |
|
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
79
diff
changeset
|
26 |
package org.opensolaris.os.vp.util.cli; |
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
27 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
28 |
public class PosixOptionFormatter extends OptionFormatter { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
29 |
// |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
30 |
// Static data |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
31 |
// |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
32 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
33 |
public static final String PREFIX_SHORT_OPTION = "-"; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
34 |
public static final String PREFIX_LONG_OPTION = "--"; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
35 |
public static final String OPT_ARG_LONG_SEPARATOR = "="; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
36 |
public static final String OPT_ARG_SHORT_SEPARATOR = " "; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
37 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
38 |
// |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
39 |
// OptionFormatter methods |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
40 |
// |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
41 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
42 |
@Override |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
43 |
public String getFormatted(String[] opts) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
44 |
return getFormatted(opts, null); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
45 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
46 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
47 |
@Override |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
48 |
public String getFormatted(String[] opts, String optArg) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
49 |
String separator = ""; |
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
50 |
StringBuilder buffer = new StringBuilder(); |
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
51 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
52 |
for (int i = 0; i < opts.length; i++) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
53 |
String opt = opts[i]; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
54 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
55 |
if (i != 0) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
56 |
buffer.append(OPT_NAME_DELIM); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
57 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
58 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
59 |
// Is this a non-option opt? |
79
d22c901edeff
3496 code cleanup/best practices/nits
Stephen Talley <stephen.talley@sun.com>
parents:
78
diff
changeset
|
60 |
if (opt.isEmpty()) { |
78
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
61 |
separator = ""; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
62 |
} else |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
63 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
64 |
// Is this a short opt? |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
65 |
if (opt.length() == 1) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
66 |
separator = OPT_ARG_SHORT_SEPARATOR; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
67 |
buffer.append(PREFIX_SHORT_OPTION); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
68 |
} else { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
69 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
70 |
// This is a long opt |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
71 |
separator = OPT_ARG_LONG_SEPARATOR; |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
72 |
buffer.append(PREFIX_LONG_OPTION); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
73 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
74 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
75 |
buffer.append(opt); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
76 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
77 |
if (optArg != null) { |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
78 |
buffer.append(separator).append(OPT_ARG_ENCLOSE_LEFT). |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
79 |
append(optArg).append(OPT_ARG_ENCLOSE_RIGHT); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
80 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
81 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
82 |
|
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
83 |
return buffer.toString(); |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
84 |
} |
6336fcea8db0
2521 Formalize CLI option parsing and document CLI syntax
Stephen Talley <stephen.talley@sun.com>
parents:
diff
changeset
|
85 |
} |