View Javadoc
1   package org.djutils.cli;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.djutils.exceptions.Throw;
6   import org.junit.Test;
7   
8   import picocli.CommandLine;
9   import picocli.CommandLine.Command;
10  import picocli.CommandLine.ITypeConverter;
11  import picocli.CommandLine.Option;
12  
13  /**
14   * Program to test the CLI. <br>
15   * <br>
16   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
17   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
18   * source code and binary code of this software is proprietary information of Delft University of Technology.
19   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
20   */
21  public class TestCLIRegisterConverter
22  {
23      /** lat/lon object that can be parsed. */
24      public static class LatLon
25      {
26          /** lat. */
27          private final double lat;
28  
29          /** lon. */
30          private final double lon;
31  
32          /**
33           * @param lat lat
34           * @param lon lon
35           */
36          public LatLon(final double lat, final double lon)
37          {
38              this.lat = lat;
39              this.lon = lon;
40          }
41  
42          /**
43           * @return lat
44           */
45          public double getLat()
46          {
47              return this.lat;
48          }
49  
50          /**
51           * @return lon
52           */
53          public double getLon()
54          {
55              return this.lon;
56          }
57  
58          /**
59           * parse a string of the form (lat, lon).
60           * @param latlon the string to parse
61           * @return the LatLon object
62           * @throws RuntimeException on error
63           */
64          public static LatLon of(final String latlon) throws RuntimeException
65          {
66              String ll = latlon;
67              Throw.when(!ll.startsWith("("), RuntimeException.class, "string does not contain '('");
68              Throw.when(!ll.endsWith(")"), RuntimeException.class, "string does not contain ')'");
69              ll = ll.replaceAll("\\(", "").replaceAll("\\)", "");
70              String[] llArr = ll.split("\\,");
71              Throw.when(llArr.length != 2, RuntimeException.class, "string does not contain one ','");
72              double lat = Double.parseDouble(llArr[0].trim());
73              double lon = Double.parseDouble(llArr[1].trim());
74              return new LatLon(lat, lon);
75          }
76      }
77  
78      /** */
79      @Command(description = "Test program for CLI", name = "Program", mixinStandardHelpOptions = true, version = "1.0")
80      public static class Options
81      {
82          /** */
83          @Option(names = {"-l", "--latlon"}, description = "Latitude and longitude of the form '(lat, lon)'")
84          private LatLon latLon;
85  
86          /** @return the lat/lon */
87          public LatLon getLatLon()
88          {
89              return this.latLon;
90          }
91      }
92  
93      /**
94       * Convert a LatLon string to a LatLon object.
95       */
96      public static class LatLonConverter implements ITypeConverter<LatLon>
97      {
98          /** {@inheritDoc} */
99          @Override
100         public LatLon convert(final String value) throws Exception
101         {
102             return LatLon.of(value);
103         }
104     }
105 
106     /**
107      * Test the CliUtil methods for registering user-defined converters.
108      * @throws CliException on error
109      * @throws IllegalAccessException on error
110      * @throws IllegalArgumentException on error
111      * @throws NoSuchFieldException on error
112      */
113     @Test
114     public void testCliRegisterConverters()
115             throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, CliException
116     {
117         // test CliUtil with CommandLine
118         String[] args = new String[] {"-l", "(50.2,71.3)"};
119         Options options = new Options();
120         CommandLine cmd = new CommandLine(options);
121         cmd.registerConverter(LatLon.class, new LatLonConverter());
122         CliUtil.execute(cmd, args);
123         assertEquals(50.2, options.getLatLon().getLat(), 0.001);
124         assertEquals(71.3, options.getLatLon().getLon(), 0.001);
125     }
126 }