Skip to content

Commit 34e12a8

Browse files
authored
new: added CliArgs.parseWith (#3)
1 parent 24f3809 commit 34e12a8

30 files changed

+4063
-20
lines changed

src/main/java/com/github/sttk/cliargs/CliArgs.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
*/
66
package com.github.sttk.cliargs;
77

8+
import com.github.sttk.reasonedexception.ReasonedException;
9+
import java.util.List;
10+
811
/**
912
* Provides the static methods to parse and print command line arguments.
1013
*/
1114
public class CliArgs {
1215

16+
/// Exception reasons on parsing command line arguments.
17+
1318
/**
1419
* Is implemented by records that indicate exception reasons, and provides a
1520
* method to retrieve an option name.
@@ -32,6 +37,101 @@ public interface InvalidOption {
3237
public record OptionHasInvalidChar(String option)
3338
implements InvalidOption {}
3439

40+
/**
41+
* Is the exception reason which indicates that there is no configuration
42+
* about the input option.
43+
*
44+
* @param option An option name.
45+
*/
46+
public record UnconfiguredOption(String option)
47+
implements InvalidOption {}
48+
49+
/**
50+
* Is the exception reason which indicates that an option is input with no
51+
* option argument though its option configuration requires option argument
52+
* ({@code .hasArg == true}).
53+
*
54+
* @param option An option name.
55+
* @param storeKey A store key.
56+
*/
57+
public record OptionNeedsArg(String option, String storeKey)
58+
implements InvalidOption {}
59+
60+
/**
61+
* Is the exception reason which indicates that an option is input with an
62+
* option argument though its option configuration does not accept option
63+
* arguments ({@code .hasArg == false}).
64+
*
65+
* @param option An option name.
66+
* @param storeKey A store key.
67+
* @param optArg An option argument.
68+
*/
69+
public record OptionTakesNoArg(String option, String storeKey, String optArg)
70+
implements InvalidOption {}
71+
72+
/**
73+
* Is the exception reason which indicates that an option is input with an
74+
* option argument multiple times though its option configuration specifies
75+
* the option is not an array ({@code .isArray == false}).
76+
*
77+
* @param option An option name.
78+
* @param storeKey A store key.
79+
* @param optArg An option argument.
80+
*/
81+
public record OptionIsNotArray(String option, String storeKey, String optArg)
82+
implements InvalidOption {}
83+
84+
/**
85+
* Is the exception reason which indicates that an option argument is failed
86+
* to convert the specified type value.
87+
*
88+
* @param optArg An option argument.
89+
* @param option An option name.
90+
* @param storeKey A store key.
91+
*/
92+
public record FailToConvertOptionArg(
93+
String optArg, String option, String storeKey
94+
) implements InvalidOption {}
95+
96+
/// Exception reasones for option configurations.
97+
98+
/**
99+
* Is the exception reason which indicates that a store key in an option
100+
* configuration is duplicated another among all option configurations.
101+
*
102+
* @param storeKey A store key.
103+
*/
104+
public record StoreKeyIsDuplicated(String storeKey) {}
105+
106+
/**
107+
* Is the exception reason which indicates that an option configuration
108+
* contradicts that the option must be an array ({@code .isArray == true})
109+
* though it has no option argument ({@code .hasArg == false}).
110+
*
111+
* @param storeKey A store key.
112+
*/
113+
public record ConfigIsArrayButHasNoArg(String storeKey) {}
114+
115+
/**
116+
* Is the exception reason which indicates that an opiton configuration
117+
* contradicts that the option has default value(s)
118+
* ({@code .defaults != null}) though it has no option argument
119+
* ({@code .hasArg == false}).
120+
*
121+
* @param storeKey A store key.
122+
*/
123+
public record ConfigHasDefaultsButHasNoArg(String storeKey) {}
124+
125+
/**
126+
* Is the exception reason which indicates that an option name in
127+
* {@code .names} field is duplicated another among all option
128+
* configurations.
129+
*
130+
* @param storeKey A store key.
131+
* @param option An option name.
132+
*/
133+
public record OptionNameIsDuplicated(String storeKey, String option) {}
134+
35135
///
36136

37137
/** The command name. */
@@ -81,4 +181,49 @@ public CliArgs(String cmd, String ...args) {
81181
public Result parse() {
82182
return Parse.parse(this.cmd, this.args);
83183
}
184+
185+
/**
186+
* Parses command line arguments with option configurations.
187+
* <p>
188+
* This method divides command line arguments to command arguments and
189+
* options.
190+
* And an option consists of a name and an option argument.
191+
* Options are divided to long format options and short format options.
192+
* About long/short format options, since they are same with {@link parse}
193+
* method, see the comment of that method.
194+
* <p>
195+
* This method basically allows only options declared in option
196+
* configurations.
197+
* An option configuration has fields: {@code storeKey}, {@code names},
198+
* {@code hasArg}, {@code isArray}, {@code type}, {@code defaults},
199+
* {@code desc}, {@code argInHelp}, and {@code converter}.
200+
* When an option in command line arguments matches one of the {@code names}
201+
* in an option configuration, the option is registered into {@code Cmd} with
202+
* {@code storeKey}.
203+
* If both {@code hasArg} and {@code isArray} are true, the option can have
204+
* one or multiple option arguments, and if {@code hasArg} is true and
205+
* {@code isArray} is false, the option can have only one option argument,
206+
* otherwise the option cannot have option arguments.
207+
* If {@code defaults} is specified and no option argument is give in command
208+
* line arguments, the option argument(s) is set to the value(s) of
209+
* {@code defaults}.
210+
* If {@code converter} is specified and option argument(s) is given in
211+
* command line arguments, the value or each of option argument(s) are
212+
* converted with the {@code converter}.
213+
* If {@code type} is specified but {@code converter} is not specified,
214+
* a converter which converts a string to the specified type value is set.
215+
* <p>
216+
* If options not declared in option configurations are given in command line
217+
* arguments, this method basically throws a {@link ReasonedException} with
218+
* the reason {@code UnconfiguredOption}.
219+
* However, if you want to allow other options, add an option configuration
220+
* of which {@code storeKey} or the first element of {@code names} is
221+
* {@code "*"}.
222+
*
223+
* @param optCfgs An array of {@code OptCfg} objects.
224+
* @return A {@link Result} object that contains the parsed result.
225+
*/
226+
public Result parseWith(OptCfg[] optCfgs) {
227+
return ParseWith.parse(optCfgs, this.cmd, this.args);
228+
}
84229
}

src/main/java/com/github/sttk/cliargs/Cmd.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public boolean hasOpt(String name) {
7171
* If there is no option argument for the specified option, this method
7272
* returns null.
7373
*
74+
* @param <T> The type of of option argument value.
75+
*
7476
* @param name The option name.
7577
* @return The first option argument of the specified option.
7678
*/
@@ -90,6 +92,8 @@ public <T> T getOptArg(String name) {
9092
* If there is no option argument for the specified option, this method
9193
* returns an empty list.
9294
*
95+
* @param <T> The type of of option argument list.
96+
*
9397
* @param name The option name.
9498
* @return The option arguments of the specified option.
9599
*/

0 commit comments

Comments
 (0)