1414/**
1515 * An ArgumentSuite is an ArgumentParser that supports "modes".
1616 *
17- * A mode is a required parameter that causes a fully separate argument parser to
18- * be used to parse the remaining arguments. This allows for finer control over
19- * mutually exclusive parameters in both the documentation and the validation, as
20- * well as wider support for traditional use cases.
17+ * A mode is a required parameter that causes a fully separate argument parser to be used to parse the remaining
18+ * arguments. This allows for finer control over mutually exclusive parameters in both the documentation and the
19+ * validation, as well as wider support for traditional use cases.
2120 *
2221 */
2322public class ArgumentSuite {
@@ -26,65 +25,67 @@ public class ArgumentSuite {
2625 private Map <String , String > aliases ;
2726 private String description ;
2827
29- public ArgumentSuite (){
28+ public ArgumentSuite () {
3029 suite = new LinkedHashMap <String , ArgumentParser >();
3130 aliases = new LinkedHashMap <String , String >();
3231 }
3332
3433 /**
35- * Adds a new mode. A mode name may contain dashes, which would look like normal
36- * argument flags, but would actually be a mode. This is useful especially for a
37- * --help command, which shows the ArgumentSuite's help.
34+ * Adds a new mode. A mode name may contain dashes, which would look like normal argument flags, but would actually
35+ * be a mode. This is useful especially for a --help command, which shows the ArgumentSuite's help.
36+ *
3837 * @param modeName The name of this mode. This may not contain spaces.
3938 * @param mode The sub-ArgumentParser that will be used when in this mode.
4039 * @throws IllegalArgumentException if the name of the mode contains spaces
4140 */
42- public ArgumentSuite addMode (String modeName , ArgumentParser mode ){
41+ public ArgumentSuite addMode (String modeName , ArgumentParser mode ) {
4342 validateModeName (modeName );
4443 suite .put (modeName , mode );
4544 return this ;
4645 }
4746
4847 /**
49- * Adds a mode alias. This is the recommended behavior instead of adding the
50- * same mode with a different name, because the built description is aware
51- * of the difference between an alias and the real mode. All the same rules
52- * apply to aliases that apply to mode names. The realModeName doesn't
53- * strictly need to exist yet.
48+ * Adds a mode alias. This is the recommended behavior instead of adding the same mode with a different name,
49+ * because the built description is aware of the difference between an alias and the real mode. All the same rules
50+ * apply to aliases that apply to mode names. The realModeName doesn't strictly need to exist yet.
51+ *
5452 * @param alias
5553 * @param realModeName
5654 * @return
5755 */
58- public ArgumentSuite addModeAlias (String alias , String realModeName ){
56+ public ArgumentSuite addModeAlias (String alias , String realModeName ) {
5957 validateModeName (alias );
6058 aliases .put (alias , realModeName );
6159 return this ;
6260 }
6361
64- private void validateModeName (String modeName ){
65- if (modeName .contains (" " )){
62+ private void validateModeName (String modeName ) {
63+ if (modeName .contains (" " )) {
6664 throw new IllegalArgumentException ("The mode name may not contain a space." );
6765 }
6866 }
6967
7068 /**
71- * Adds a description, which is used in {@see #getBuiltDescription}
69+ * Adds a description, which is used in {
70+ *
71+ * @see #getBuiltDescription}
7272 * @param description
7373 * @return
7474 */
75- public ArgumentSuite addDescription (String description ){
75+ public ArgumentSuite addDescription (String description ) {
7676 this .description = description ;
7777 return this ;
7878 }
7979
8080 /**
8181 * Returns a mode that was previously registered.
82+ *
8283 * @param name The name of the mode to get
8384 * @return The mode registered under the provided name
8485 * @throws IllegalArgumentException if the mode is not registered
8586 */
86- public ArgumentParser getMode (String name ){
87- if (suite .containsKey (name )){
87+ public ArgumentParser getMode (String name ) {
88+ if (suite .containsKey (name )) {
8889 return suite .get (name );
8990 } else {
9091 throw new IllegalArgumentException ("No mode by the name \" " + name + "\" has been registered." );
@@ -93,28 +94,28 @@ public ArgumentParser getMode(String name){
9394
9495 /**
9596 * Selects the appropriate mode, and calls match on that ArgumentParser.
97+ *
9698 * @param args The pre-parsed arguments
9799 * @param defaultMode The default mode, which will be used only if no arguments were passed in.
98100 * @return
99- * @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser
100- * throws an exception.
101+ * @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser throws an exception.
101102 */
102- public ArgumentSuiteResults match (String [] args , String defaultMode ) throws ResultUseException , ValidationException {
103- String [] nonModeArgs = ArrayUtils .EMPTY_STRING_ARRAY ;
103+ public ArgumentSuiteResults match (String [] args , String defaultMode ) throws ResultUseException , ValidationException {
104+ String [] nonModeArgs = ArrayUtils .EMPTY_STRING_ARRAY ;
104105 String mode ;
105- if (args .length > 1 ){
106+ if (args .length > 1 ) {
106107 mode = args [0 ];
107108 nonModeArgs = ArrayUtils .cast (ArrayUtils .slice (args , 1 , args .length - 1 ), String [].class );
108- } else if (args .length == 1 ){
109+ } else if (args .length == 1 ) {
109110 mode = args [0 ];
110111 } else {
111112 //0 argsm, use the defaultMode.
112113 mode = defaultMode ;
113114 }
114- if (aliases .containsKey (mode )){
115+ if (aliases .containsKey (mode )) {
115116 mode = aliases .get (mode );
116117 }
117- if (suite .containsKey (mode )){
118+ if (suite .containsKey (mode )) {
118119 return new ArgumentSuiteResults (mode , suite .get (mode ), suite .get (mode ).match (nonModeArgs ));
119120 } else {
120121 throw new ResultUseException ("Mode " + mode + " was not found." );
@@ -123,11 +124,11 @@ public ArgumentSuiteResults match(String [] args, String defaultMode) throws Res
123124
124125 /**
125126 * Selects the appropriate mode, and calls match on that ArgumentParser.
127+ *
126128 * @param args The unparsed arguments
127129 * @param defaultMode The default mode, which will be used only if no arguments were passed in.
128130 * @return
129- * @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser
130- * throws an exception.
131+ * @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser throws an exception.
131132 */
132133 public ArgumentSuiteResults match (String args , String defaultMode ) throws ResultUseException , ValidationException {
133134 //We're going to use ArgumentParser's parse method to get a string list, then
@@ -136,27 +137,28 @@ public ArgumentSuiteResults match(String args, String defaultMode) throws Result
136137 }
137138
138139 /**
139- * Returns a built description of this ArgumentSuite, which would be appropriate
140- * to display if no arguments are passed in (or the mode name is help, -help, --help, etc)
140+ * Returns a built description of this ArgumentSuite, which would be appropriate to display if no arguments are
141+ * passed in (or the mode name is help, -help, --help, etc)
142+ *
141143 * @return
142144 */
143- public String getBuiltDescription (){
145+ public String getBuiltDescription () {
144146 StringBuilder b = new StringBuilder ();
145- if (description != null ){
147+ if (description != null ) {
146148 b .append (description ).append ("\n \n " );
147149 }
148150 b .append ("Modes: (a mode must be the first argument) \n " );
149- for (String mode : suite .keySet ()){
151+ for (String mode : suite .keySet ()) {
150152 b .append ("\t " ).append (TermColors .BOLD ).append (mode );
151- if (aliases .containsValue (mode )){
153+ if (aliases .containsValue (mode )) {
152154 List <String > keys = new ArrayList <>();
153- for (String alias : aliases .keySet ()){
154- if (aliases .get (alias ).equals (mode )){
155+ for (String alias : aliases .keySet ()) {
156+ if (aliases .get (alias ).equals (mode )) {
155157 keys .add (alias );
156158 }
157159 }
158160 b .append (TermColors .RESET ).append (" (Alias" );
159- if (keys .size () != 1 ){
161+ if (keys .size () != 1 ) {
160162 b .append ("es" );
161163 }
162164 b .append (": " ).append (StringUtils .Join (keys , ", " )).append (")" );
@@ -167,84 +169,88 @@ public String getBuiltDescription(){
167169 }
168170
169171 /**
170- * A convenience method to get the real mode name registered for this
171- * alias, or null if no such alias exists. If the alias is actually a mode,
172- * it is simply returned. Useful for perhaps a help mode, to resolve the actual
173- * mode named. If {@code alias} is null, null is returned.
172+ * A convenience method to get the real mode name registered for this alias, or null if no such alias exists. If the
173+ * alias is actually a mode, it is simply returned. Useful for perhaps a help mode, to resolve the actual mode
174+ * named. If {@code alias} is null, null is returned.
175+ *
174176 * @param alias
175177 * @return
176178 */
177- public String getModeFromAlias (String alias ){
178- if (alias == null ){
179+ public String getModeFromAlias (String alias ) {
180+ if (alias == null ) {
179181 return null ;
180182 }
181- if (suite .containsKey (alias )){
183+ if (suite .containsKey (alias )) {
182184 return alias ;
183- } else if (aliases .containsKey (alias )){
185+ } else if (aliases .containsKey (alias )) {
184186 return aliases .get (alias );
185187 } else {
186188 return null ;
187189 }
188190 }
189191
190192 /**
191- * A convenience method to get the underlying ArgumentParser
192- * based on the mode name given. Aliases will not suffice, but you
193- * may call getModeFromAlias to resolve the mode name first. Null
194- * is returned if no mode exists with that name. Useful for perhaps
195- * a help mode, to generically display a mode's help. If {@see mode}
196- * is null, null is returned.
193+ * A convenience method to get the underlying ArgumentParser based on the mode name given. Aliases will not suffice,
194+ * but you may call getModeFromAlias to resolve the mode name first. Null is returned if no mode exists with that
195+ * name. Useful for perhaps a help mode, to generically display a mode's help. If {
196+ *
197+ * @see mode} is null, null is returned.
197198 * @param mode
198199 * @return
199200 */
200- public ArgumentParser getModeFromName (String mode ){
201- if (mode == null ){
201+ public ArgumentParser getModeFromName (String mode ) {
202+ if (mode == null ) {
202203 return null ;
203204 }
204- if (suite .containsKey (mode )){
205+ if (suite .containsKey (mode )) {
205206 return suite .get (mode );
206207 } else {
207208 return null ;
208209 }
209210 }
210211
211- public static class ArgumentSuiteResults {
212+ public static class ArgumentSuiteResults {
213+
212214 private ArgumentParser mode ;
213215 private ArgumentParserResults results ;
214216 private String modeName ;
215- private ArgumentSuiteResults (String modeName , ArgumentParser mode , ArgumentParserResults results ){
217+
218+ private ArgumentSuiteResults (String modeName , ArgumentParser mode , ArgumentParserResults results ) {
216219 this .modeName = modeName ;
217220 this .mode = mode ;
218221 this .results = results ;
219222 }
220223
221224 /**
222225 * Returns the name of the mode that was selected. (Not the alias)
226+ *
223227 * @return
224228 */
225- public String getModeName (){
229+ public String getModeName () {
226230 return modeName ;
227231 }
228232
229233 /**
230- * The ArgumentParser for the given mode. This will be a reference
231- * to the mode passed in, so you can do == on it.
234+ * The ArgumentParser for the given mode. This will be a reference to the mode passed in, so you can do == on
235+ * it.
236+ *
232237 * @return
233238 */
234- public ArgumentParser getMode (){
239+ public ArgumentParser getMode () {
235240 return mode ;
236241 }
237242
238243 /**
239244 * The ArgumentParserResults for the ArgumentParser mode
245+ *
240246 * @return
241247 */
242- public ArgumentParserResults getResults (){
248+ public ArgumentParserResults getResults () {
243249 return results ;
244250 }
245251 }
246252
247- public static void main (String [] args ){
253+ public static void main (String [] args ) {
248254 ArgumentSuite suite = new ArgumentSuite ();
249255 ArgumentParser mode1 = ArgumentParser .GetParser ();
250256 ArgumentParser mode2 = ArgumentParser .GetParser ();
@@ -267,21 +273,21 @@ public static void main(String [] args){
267273 ArgumentSuiteResults results = suite .match ("wat" , "help" );
268274 ArgumentParser mode = results .getMode ();
269275 ArgumentParserResults modeResults = results .getResults ();
270- if (mode == help ){
276+ if (mode == help ) {
271277 String modeHelp = modeResults .getStringArgument ();
272278 ArgumentParser selectedMode = suite .getModeFromName (suite .getModeFromAlias (modeHelp ));
273- if (selectedMode != null ){
279+ if (selectedMode != null ) {
274280 StreamUtils .GetSystemOut ().println (selectedMode .getBuiltDescription ());
275281 System .exit (0 );
276282 } else {
277283 showHelp (suite );
278284 }
279- } else if (mode == mode1 || mode == mode2 ){
285+ } else if (mode == mode1 || mode == mode2 ) {
280286 String argument ;
281- if ((argument = modeResults .getStringArgument ("arg1" )) != null ){
287+ if ((argument = modeResults .getStringArgument ("arg1" )) != null ) {
282288 StreamUtils .GetSystemOut ().println ("You selected " + argument + " for arg1" );
283289 }
284- if ((argument = modeResults .getStringArgument ("arg2" )) != null ){
290+ if ((argument = modeResults .getStringArgument ("arg2" )) != null ) {
285291 StreamUtils .GetSystemOut ().println ("You selected " + argument + " for arg2" );
286292 }
287293 System .exit (0 );
@@ -293,7 +299,7 @@ public static void main(String [] args){
293299 }
294300 }
295301
296- private static void showHelp (ArgumentSuite suite ){
302+ private static void showHelp (ArgumentSuite suite ) {
297303 StreamUtils .GetSystemOut ().println (suite .getBuiltDescription ());
298304 System .exit (1 );
299305 }
0 commit comments