1 /*
2  * Sample2.java
3  *
4  * jcmdline Rel. @VERSION@ $Id: Sample2.java,v 1.3 2009/08/06 14:31:35 lglawrence Exp $
5  *
6  * Classes:
7  *   public   Sample2
8  *
9  * ***** BEGIN LICENSE BLOCK *****
10  * Version: MPL 1.1
11  *
12  * The contents of this file are subject to the Mozilla Public License Version
13  * 1.1 (the "License"); you may not use this file except in compliance with
14  * the License. You may obtain a copy of the License at
15  * http://www.mozilla.org/MPL/
16  *
17  * Software distributed under the License is distributed on an "AS IS" basis,
18  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
19  * for the specific language governing rights and limitations under the
20  * License.
21  *
22  * The Original Code is the Java jcmdline (command line management) package.
23  *
24  * The Initial Developer of the Original Code is Lynne Lawrence.
25  *
26  * Portions created by the Initial Developer are Copyright (C) 2002
27  * the Initial Developer. All Rights Reserved.
28  *
29  * Contributor(s):  Lynne Lawrence <lgl@visuallink.com>
30  *
31  * ***** END LICENSE BLOCK *****
32  */
33 
34 package jcmdline;
35 
36 import java.util.Collection;
37 import java.util.List;
38 
39 /**
40  * A "junk class" used to test CmdLineParser.
41  *
42  * @author Lynne Lawrence
43  * @version jcmdline Rel. @VERSION@ $Id: Sample2.java,v 1.2 2002/12/07 14:30:49
44  *          lglawrence Exp $
45  */
46 public class Sample2 {
47 
48 	/**
49 	 * The arguments this program takes
50 	 */
51 	private static final Parameter<?>[] arguments = new Parameter<?>[] {
52 			new FileParam("old_file",
53 					"the name of the file to copy - this file"
54 							+ " must already exist, be readable, and end "
55 							+ "with '.html'", FileParam.IS_FILE
56 							& FileParam.IS_READABLE, FileParam.REQUIRED),
57 			new FileParam("new_file", "the name of the file to copy to",
58 					FileParam.REQUIRED),
59 			new FileParam("copies",
60 					"optional copies to be made of old_file, in addition "
61 							+ "to new_file; files may not already exist",
62 					FileParam.DOESNT_EXIST, FileParam.OPTIONAL,
63 					FileParam.MULTI_VALUED) };
64 
65 	/**
66 	 * The help text for this program
67 	 */
68 	private static final String helpText = "This command is used to copy an html file, old_file, to a new file, "
69 			+ "new_file.\n\n"
70 			+ "Optionally, if the '-delete' option is specified, the original file"
71 			+ " will be deleted.  It is also possible to preserve the contents of "
72 			+ "the destination file.  To preserve the destination file, if it "
73 			+ "exists, use the '-save' option\n\n"
74 			+ "Additional copies of old_file can be made by specifying <copies>. "
75 			+ "Files associated with filenames specified as copies must not "
76 			+ "exist\n\n"
77 			+ "Permissions for the new file may be specified using the '-perms' "
78 			+ "option.  If '-perms' is not specified, the permissions will be set "
79 			+ "to the same as the source file.\n\nIn order to test the "
80 			+ "functionality of lots of different options, the following options "
81 			+ "are supported:"
82 			+ "\n  -int1"
83 			+ "\n  -int2"
84 			+ "\n  -int3"
85 			+ "\n  -int4"
86 			+ "\n  -int5"
87 			+ "\n  -bool1"
88 			+ "\n  -bool2"
89 			+ "\n  -string1"
90 			+ "\n  -string2"
91 			+ "\n  -string3"
92 			+ "\n  -file1"
93 			+ "\n  -file2" + "\n  -file3";
94 
95 	/**
96 	 * The options available for this program
97 	 */
98 	private static final Parameter<?>[] opts = new Parameter<?>[] {
99 			new IntParam("int1", "an optional int param"),
100 			new IntParam("int2",
101 					"an optional int param with min and max values"
102 							+ " of -3 and 50, respectively", -3, 50),
103 			new IntParam("int3", "an optional, multivalued int param",
104 					IntParam.OPTIONAL, IntParam.MULTI_VALUED),
105 			new IntParam("int4", "an optional, multivalued, hidden int param",
106 					IntParam.OPTIONAL, IntParam.MULTI_VALUED, IntParam.HIDDEN),
107 			new IntParam("int5", "a required int param", FileParam.REQUIRED),
108 			new BooleanParam("bool1", "an optional boolean parameter"),
109 			new BooleanParam("bool2", "an optional, hidden, boolean parameter",
110 					BooleanParam.HIDDEN),
111 			new StringParam("string1", "an optional string option"),
112 			new StringParam("string2",
113 					"a required string option with a set of acceptable "
114 							+ "values that include str1, str2, and str3",
115 					new String[] { "str1", "str2", "str3" },
116 					StringParam.REQUIRED),
117 			new StringParam("string3",
118 					"an optional string option that wants a string with "
119 							+ "min and max lengths of 3 and 5, respectively",
120 					3, 5),
121 			new FileParam("file1", "an optional file parameter"),
122 			new FileParam("file2",
123 					"an optional file parameter that must be a directory",
124 					FileParam.IS_DIR),
125 			new FileParam("file3", "an optional, hidden file parameter",
126 					FileParam.NO_ATTRIBUTES, FileParam.OPTIONAL,
127 					FileParam.SINGLE_VALUED, FileParam.HIDDEN),
128 			new DateParam("startDate", "the start date for the report"),
129 			new DateTimeParam("dateTime", "the report date and time"),
130 			new TimeParam("time", "the time of the report"), };
131 
132 	/**
133 	 * main - doesn't actually do anything but process command line paramters
134 	 * and print them out.
135 	 *
136 	 * @param args
137 	 *            command line arguments
138 	 */
main(String[] args)139 	public static void main(String[] args) {
140 		/*
141 		 * CmdLineParser cl = new CmdLineParser( "Sample2",
142 		 * "a class to test command line parameters", "V 5.2", helpText, opts,
143 		 * arguments);
144 		 */
145 		CmdLineHandler cl = new VersionCmdLineHandler("V 5.2",
146 				new HelpCmdLineHandler(helpText, "Sample2",
147 						"a class to test command line parameters", opts,
148 						arguments));
149 		cl.parse(args);
150 		FileParam oldfile = (FileParam) cl.getArg("old_file");
151 		if (!oldfile.getValue().getPath().endsWith(".html")) {
152 			cl.exitUsageError("<old_file> must end with '.html'");
153 		}
154 
155 		Collection<Parameter<?>> nopts = cl.getOptions();
156 		Collection<Parameter<?>> nargs = cl.getArgs();
157 		String valString;
158 
159 		System.out.println("\n\nArgs:\n");
160 		for (Parameter<?> p : nargs) {
161 			List<?> vals = p.getValues();
162 			valString = "";
163 			for (Object o : vals) {
164 				valString += " '" + o.toString() + "'";
165 			}
166 			System.out.println("  " + p.getTag() + ": " + valString);
167 		}
168 		System.out.println("\n\nOptions:\n");
169 		for (Parameter<?> p : nopts) {
170 			List<?> vals = p.getValues();
171 			valString = "";
172 			for (Object o : vals) {
173 				valString += " '" + o.toString() + "'";
174 			}
175 			System.out.println("  " + p.getTag() + ": " + valString);
176 		}
177 	}
178 }
179