1   /***
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  
24  /***
25   * <p>
26   * This is a collection of tests that test real world
27   * applications command lines focusing on options with
28   * long and short names.
29   * </p>
30   */
31  public class LongOptionWithShort extends TestCase {
32      public LongOptionWithShort(String name) {
33          super(name);
34      }
35  
36      public static Test suite() {
37          return new TestSuite(LongOptionWithShort.class);
38      }
39  
40      /***
41       *
42       */
43      public void testLongOptionWithShort() {
44          Option help = new Option("h", "help", false, "print this message");
45          Option version = new Option("v", "version", false,
46                  "print version information");
47          Option newRun = new Option("n", "new", false,
48                  "Create NLT cache entries only for new items");
49          Option trackerRun = new Option("t", "tracker", false,
50                  "Create NLT cache entries only for tracker items");
51  
52          Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
53                                          .withValueSeparator()
54                                          .withDescription("Set time limit for execution, in mintues")
55                                          .create("l");
56  
57          Option age = OptionBuilder.withLongOpt("age").hasArg()
58                                    .withValueSeparator()
59                                    .withDescription("Age (in days) of cache item before being recomputed")
60                                    .create("a");
61  
62          Option server = OptionBuilder.withLongOpt("server").hasArg()
63                                       .withValueSeparator()
64                                       .withDescription("The NLT server address")
65                                       .create("s");
66  
67          Option numResults = OptionBuilder.withLongOpt("results").hasArg()
68                                           .withValueSeparator()
69                                           .withDescription("Number of results per item")
70                                           .create("r");
71  
72          Option configFile = OptionBuilder.withLongOpt("file").hasArg()
73                                           .withValueSeparator()
74                                           .withDescription("Use the specified configuration file")
75                                           .create();
76  
77          Options options = new Options();
78          options.addOption(help);
79          options.addOption(version);
80          options.addOption(newRun);
81          options.addOption(trackerRun);
82          options.addOption(timeLimit);
83          options.addOption(age);
84          options.addOption(server);
85          options.addOption(numResults);
86          options.addOption(configFile);
87  
88          // create the command line parser
89          CommandLineParser parser = new PosixParser();
90  
91          String[] args = new String[] {
92                  "-v",
93                  "-l",
94                  "10",
95                  "-age",
96                  "5",
97                  "-file",
98                  "filename"
99              };
100 
101         try {
102             CommandLine line = parser.parse(options, args);
103             assertTrue(line.hasOption("v"));
104             assertEquals(line.getOptionValue("l"), "10");
105             assertEquals(line.getOptionValue("limit"), "10");
106             assertEquals(line.getOptionValue("a"), "5");
107             assertEquals(line.getOptionValue("age"), "5");
108             assertEquals(line.getOptionValue("file"), "filename");
109         }
110         catch (ParseException exp) {
111             fail("Unexpected exception:" + exp.getMessage());
112         }
113     }
114 }