View Javadoc
1   package org.cyclopsgroup.jcli.spi;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import com.google.common.base.MoreObjects;
6   
7   /**
8    * Data that comes from command arguments
9    *
10   * @author <a href="mailto:jiaqi@cyclopsgroup.org">Jiaqi Guo</a>
11   */
12  public final class CommandLine {
13    /**
14     * One entry of option value
15     */
16    public static final class OptionValue {
17      /**
18       * Name of command line option it detects
19       */
20      public final String name;
21  
22      /**
23       * True if option name is a short name
24       */
25      public final boolean shortName;
26  
27      /**
28       * Detected value
29       */
30      public final String value;
31  
32      OptionValue(String name, String value, boolean shortName) {
33        this.name = name;
34        this.value = value;
35        this.shortName = shortName;
36      }
37  
38      @Override
39      public String toString() {
40        return MoreObjects.toStringHelper(getClass().getSimpleName()).addValue(name).addValue(value)
41            .addValue(shortName).toString();
42      }
43  
44    }
45  
46    private final List<String> arguments = new ArrayList<String>();
47  
48    private final List<OptionValue> optionValues = new ArrayList<OptionValue>();
49  
50    void addArgument(String argument) {
51      this.arguments.add(argument);
52    }
53  
54    void addOptionValue(String name, String value, boolean shortName) {
55      this.optionValues.add(new OptionValue(name, value, shortName));
56    }
57  
58    /**
59     * @return List of arguments
60     */
61    public List<String> getArguments() {
62      return arguments;
63    }
64  
65    /**
66     * @return List of option and values
67     */
68    public final List<OptionValue> getOptionValues() {
69      return optionValues;
70    }
71  
72    @Override
73    public String toString() {
74      return MoreObjects.toStringHelper(getClass()).addValue(arguments).addValue(optionValues)
75          .toString();
76    }
77  }