Skip to content

Commit 57796bb

Browse files
committed
update!: made overall changes to align the API with cliargs-rust
1 parent 09d5c6a commit 57796bb

File tree

91 files changed

+9550
-9730
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+9550
-9730
lines changed

.ci/macos-latest/resource-config.json

-7
This file was deleted.

.ci/ubuntu-latest/resource-config.json

-7
This file was deleted.

.ci/windows-latest/resource-config.json

-7
This file was deleted.

.github/workflows/java-ci.yml

-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,5 @@ jobs:
2424
native-image-job-reports: 'true'
2525
- name: Build and test
2626
run: mvn package
27-
- name: Prepare native test
28-
run: |
29-
cp .ci/${{ matrix.os }}/resource-config.json src/test/resources/META-INF/native-image/
3027
- name: Native test
3128
run: mvn -Pnative test

README.md

+95-111
Original file line numberDiff line numberDiff line change
@@ -46,97 +46,76 @@ dependencies {
4646

4747
### Parse without configurations
4848

49-
This library provides the method `CliArgs#parse` which parses command line arguments without configurations.
50-
This method automatically divides command line arguments to options and command arguments.
49+
This library provides the method `Cmd#parse` which parses command line arguments without configurations.
50+
This method automatically divides command line arguments to command options and command arguments.
5151

52-
Command line arguments starting with `-` or `--` are options, and others are command arguments.
52+
Command line arguments starting with `-` or `--` are command options, and others are command arguments.
5353
If you want to specify a value to an option, follows `"="` and the value after the option, like `foo=123`.
5454

5555
All command line arguments after `--` are command arguments, even they starts
5656
with `-` or `--`.
5757

5858
```java
59-
var args = new String[]{"--foo-bar", "hoge", "--baz=1", "-z=2", "-xyz=3", "fuga"};
60-
var cliargs = new CliArgs("path/to/app", args);
61-
var result = cliargs.parse();
62-
63-
if (result.exception() == null) {
64-
var cmd = result.cmd();
65-
cmd.getName(); // "app"
66-
cmd.getArgs(); // ["hoge", "fuga"]
67-
cmd.hasOpt("foo-bar"); // true
68-
cmd.hasOpt("baz"); // true
69-
cmd.hasOpt("x"); // true
70-
cmd.hasOpt("y"); // true
71-
cmd.hasOpt("z"); // true
72-
cmd.getOptArg("foo-bar"); // null
73-
cmd.getOptArg("baz"); // "1"
74-
cmd.getOptArg("x"); // null
75-
cmd.getOptArg("y"); // null
76-
cmd.getOptArg("z"); // "2"
77-
cmd.getOptArgs("foo-bar"); // []
78-
cmd.getOptArgs("baz"); // [1]
79-
cmd.getOptArgs("x"); // []
80-
cmd.getOptArgs("y"); // []
81-
cmd.getOptArgs("z"); // ["2", "3"]
82-
}
83-
```
84-
85-
Or
86-
87-
```java
88-
var args = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-xyz=3", "fuga"};
89-
var cliargs = new CliArgs("path/to/app", args);
59+
var osArgs = new String[]{"--foo-bar", "hoge", "--baz=1", "-z=2", "-xyz=3", "fuga"};
60+
var cmd = new Cmd("path/to/app", osArgs);
9061
try {
91-
var cmd = cliargs.parse().cmdOrThrow();
92-
cmd.getName(); // "app"
93-
cmd.getArgs(); // ["hoge", "fuga"]
94-
cmd.hasOpt("foo-bar"); // true
95-
// ...
96-
97-
} catch (ReasonedException e) {
62+
cmd.parse();
63+
} catch (InvalidOption e) {
9864
...
9965
}
100-
```
10166

67+
cmd.name(); // "app"
68+
cmd.args(); // ["hoge", "fuga"]
69+
cmd.hasOpt("foo-bar"); // true
70+
cmd.hasOpt("baz"); // true
71+
cmd.hasOpt("x"); // true
72+
cmd.hasOpt("y"); // true
73+
cmd.hasOpt("z"); // true
74+
cmd.optArg("foo-bar"); // null
75+
cmd.optArg("baz"); // "1"
76+
cmd.optArg("x"); // null
77+
cmd.optArg("y"); // null
78+
cmd.optArg("z"); // "2"
79+
cmd.optArgs("foo-bar"); // []
80+
cmd.optArgs("baz"); // [1]
81+
cmd.optArgs("x"); // []
82+
cmd.optArgs("y"); // []
83+
cmd.optArgs("z"); // ["2", "3"]
84+
```
10285

10386
### Parse with configurations
10487

105-
This library provides the method `CliArgs#parseWith` which parses command line arguments with configurations.
106-
This method takes an array of option configurations: `OptCfg[]` as the argument, and divides command line arguments to options and command arguments with this configurations.
88+
This library provides the method `Cmd#parseWith` which parses command line arguments with option configurations.
89+
This method takes an `OptCfg` array as the argument, and divides command line arguments to command options and command arguments with this configurations.
10790

108-
And option cnfiguration has fields: `storeKey`, `names`, `hasArg`, `isArray`, `type`, `defaults`, `decc`, `argInHelp`, `converter`, and `postparser`.
91+
And option configuration has fields: `storeKey`, `names`, `hasArg`, `isArray`, `type`, `defaults`, `desc`, `argInHelp`, and `validator`.
10992

110-
`storeKey` field is specified the key name to store the option value in the option map.
93+
`storeKey` field is to specify the key name to store the option value in the option map.
11194
If this field is not specified the first element of `names` field is set instead.
11295

113-
`names` field is a string array and specified the option names, that are both long options and short options.
96+
`names` field is a string array and is to specify the option names, that are long options or short options.
11497
The order of elements in this field is used in a help text.
11598
If you want to prioritize the output of short option name first in the help text, like `-f, --foo-bar`, but use the long option name as the key in the option map, write `storekey` and `names` fields as follows:
11699
`OptCfg(field("foo-bar", names("f", "foo-bar"))`.
117100

118-
`hasArg` field indicates the option requires one or more values.
119-
120-
`isArray` field indicates the option can have multiple values.
101+
`hasArg` field is to specify the option requires one or more values.
121102

122-
`types` field is set the data type of the option value(s).
103+
`isArray` field is to specify the option can have multiple values.
123104

124-
`defaults` field is an array which is used as default one or more values if the
105+
`defaults` field is to specify an array which is used as default one or more values if the
125106
option is not specified in command line arguments.
126107

127-
`desc` field is a description of the option for help text.
128-
129-
`argInHelp` field is a text which is output after option name and aliases as an option value in help text.
108+
`desc` field is to specify a description of the option for help text.
130109

131-
`converter` field is a functional interface which converts an option argument string to the instance of the class specified by `type` field.
110+
`argInHelp` field is to specify a text which is output after option name and aliases as an option value in help text.
132111

133-
`postparser` field is a functional interface which processes option argument(s) after parsing if this field is specified.
112+
`validator` field is to specify an instance of a functional interface which validates an option argument string as a value of the desired type.
134113

135114
```java
136115
import com.github.sttk.cliargs.OptCfg.NamedParam.*;
137116
...
138-
var args = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "fuga"};
139-
var cliargs = new CliArgs("path/to/app", args);
117+
var osArgs = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "fuga"};
118+
var cmd = new Cmd("path/to/app", osArgs);
140119

141120
var optCfgs = new OptCfg[] {
142121
new OptCfg(
@@ -159,30 +138,32 @@ import com.github.sttk.cliargs.OptCfg.NamedParam.*;
159138
)
160139
};
161140

162-
var result = cliargs.parseWith(optCfgs);
163-
if (result.exception() == null) {
164-
var cmd = result.cmd();
165-
cmd.getName(); // "app"
166-
cmd.getArgs(); // ["hoge", "fuga"]
167-
cmd.hasOpt("FooBar"); // true
168-
cmd.hasOpt("baz"); // true
169-
cmd.hasOpt("x"); // true, due to "*" config
170-
cmd.getOptArg("FooBar"); // null
171-
cmd.getOptArg("baz"); // 1
172-
cmd.getOptArg("x"); // null
173-
cmd.getOptArgs("FooBar"); // []
174-
cmd.getOptArgs("baz"); // [1, 2]
175-
cmd.getOptArgs("x"); // []
141+
try {
142+
cmd.parseWith(optCfgs);
143+
} catch (InvalidOption e) {
144+
...
176145
}
146+
147+
cmd.getName(); // "app"
148+
cmd.getArgs(); // ["hoge", "fuga"]
149+
cmd.hasOpt("FooBar"); // true
150+
cmd.hasOpt("baz"); // true
151+
cmd.hasOpt("x"); // true, due to "*" config
152+
cmd.getOptArg("FooBar"); // null
153+
cmd.getOptArg("baz"); // 1
154+
cmd.getOptArg("x"); // null
155+
cmd.getOptArgs("FooBar"); // []
156+
cmd.getOptArgs("baz"); // [1, 2]
157+
cmd.getOptArgs("x"); // []
177158
```
178159

179-
This library provides `Help` class which generates help text from an `OptCfg` array.
160+
This library provides `Help` class which generates a help text from an `OptCfg` array.
180161
The following help text is generated from the above `optCfgs`.
181162

182163
```java
183164
var help = new Help();
184165
help.addText("This is the usage description.");
185-
help.addOpts(optCfgs, 0, 2);
166+
help.addOptsWithMargins(optCfgs, 2, 0);
186167
help.print();
187168

188169
// (stdout)
@@ -193,8 +174,9 @@ The following help text is generated from the above `optCfgs`.
193174

194175
### Parse for an option store with `@Opt` annoatation
195176

196-
This library provides the method `CliArgs#parseFor` which takes an option store object as the argument, and puts option values by parsing command line arguments to it.
197-
The `@Opt` annotations are needed for the fields of the option store.
177+
This library provides the method `Cmd#parseFor` which takes an option store object as the argument, and puts option values by parsing command line arguments to it.
178+
The `@Opt` annotations can be attached to the fields of the option store for their option
179+
configurations.
198180

199181
This `@Opt` annotations can have the attributes: `cfg`, `desc`, and `arg`.
200182
`cfg` can be specified the option name, aliases and default value(s).
@@ -209,11 +191,11 @@ The format of the `cfg` attribute is as follows:
209191
```
210192

211193
`desc` is what to specify a option description.
212-
And `arg` is what to specify a text for an option argument value in help text.
194+
And `arg` is what to specify a text for an option argument value in a help text.
213195

214196
```java
215-
var args = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "fuga"};
216-
var cliargs = new CliArgs("path/to/app", args);
197+
var osArgs = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "fuga"};
198+
var cmd = new Cmd("path/to/app", osArgs);
217199

218200
class Options {
219201
@Opt(cfg="foo-bar", desc="This is description of foo-bar.")
@@ -228,9 +210,13 @@ And `arg` is what to specify a text for an option argument value in help text.
228210

229211
var options = new Options();
230212

231-
var result = cliargs.parseFor(options);
232-
var optCfgs = result.optCfgs();
233-
var cmd = result.cmdOrThrow();
213+
try {
214+
cmd.parseFor(options);
215+
} catch (InvalidOption | FailToSetOptionStoreField e) {
216+
...
217+
}
218+
219+
var optCfgs = cmd.optCfgs();
234220

235221
cmd.getName(); // "app"
236222
cmd.getArgs(); // ["hoge", "fuga"]
@@ -282,12 +268,12 @@ And `arg` is what to specify a text for an option argument value in help text.
282268
// }
283269
```
284270

285-
The following help text is generated from the above optCfgs.
271+
The following help text is generated from the above `optCfgs`.
286272

287273
```java
288274
var help = new Help();
289275
help.addText("This is the usage description.")
290-
help.addOpts(optCfgs, 12, 1);
276+
help.addOptsWithIndentAndMargins(optCfgs, 12, 1, 0);
291277
help.print();
292278

293279
// (stdout)
@@ -300,34 +286,31 @@ The following help text is generated from the above optCfgs.
300286

301287
### Parse command line arguments including sub commands
302288

303-
This library provides the static method `CliArgs.findFirstArg` which returns the index of the first command argument.
304-
If this index is negative, there is no command argument.
305-
This static method enables to parse command line arguments with supporting sub
306-
command, as follows:
289+
This library provides the methods `Cmd#parseUntilSubCmd`, `Cmd#parseUntilSubCmdWith`, `Cmd#parseUntilSubCmdFor` that parses command line arguments until a sub command is found.
290+
The return of those methods is an `Optional<Cmd>` object.
291+
If no sub command is found, the returned object is empty.
307292

308293
```java
309-
var args = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "abcd"};
310-
int index = CliArgs.findFirstArg(args); // index => 1
311-
if (index < 0) throws ...; // no sub command
294+
var osArgs = new String[]{"--foo-bar", "hoge", "--baz", "1", "-z=2", "-x", "abcd"};
312295

313-
var topArgs = Arrays.copyOf(args, index);
314-
var subArgs = Arrays.copyOfRange(args, index + 1, args.length);
296+
var cmd = new Cmd("path/to/app", osArgs);
315297

316-
var cliargs = new CliArgs("app", topArgs);
317-
var result = cliargs.parseFor(topOptions);
318-
var topOptCfgs = result.optCfgs();
319-
var topCmd = result.cmdOrThrow();
298+
var optional = cmd.parseUntilSubCmdWith(topOptCfgs);
299+
300+
if (optional.isEmpty()) {
301+
... // no sub command
302+
}
303+
var subCmd = optional.get();
320304

321-
cliargs = new CliArgs(args.get(index), subArgs);
322-
switch (args.get(index)) {
305+
switch subCmd.name() {
323306
case "hoge":
324-
result = cliargs.parseFor(hogeOptions);
325-
var hogeOptCfgs = result.optCfgs();
326-
var hogeCmd = result.cmdOrThrow();
307+
subCmd.parseWith(hogeOptCfgs);
327308
...
328309
break;
329310
case "fuga":
311+
subCmd.parseWith(fugaOptCfgs);
330312
...
313+
break;
331314
}
332315
```
333316

@@ -337,12 +320,12 @@ And the help text can be generated as follows:
337320
var help = new Help();
338321
help.addText("This is the usage of this command.");
339322
help.addText("\nOPTIONS.");
340-
help.addOpts(topOptCfgs, 12, 2);
323+
help.addOptsWithIndentAndMargins(topOptCfgs, 12, 2, 0);
341324
help.addText("\nSUB COMMANDS:");
342-
help.addText(String.format("%12s%s", "hoge", "The description of hoge sub-command.");
343-
help.addOpts(hogeOpts, 12, 2);
344-
help.addText(String.format("%12s%s", "fuga", "The description of fuga sub-command.");
345-
help.addOpts(fugaOpts, 12, 2);
325+
help.addText("hoge");
326+
help.addOptsWithIndentAndMargins(hogeOptCfgs, 12, 2, 0);
327+
help.addText("\nfuga");
328+
help.addOptsWithIndentAndMargins(fugaOptCfgs, 12, 2, 0);
346329
help.print();
347330

348331
// (stdout)
@@ -365,7 +348,7 @@ And the help text can be generated as follows:
365348
## Native build
366349

367350
This library supports native build with GraalVM.
368-
However, since it utilizes reflection for the option store object passed to `CliArgs#parseFor`, the reflection configurations for the class of this object need to be specified in `reflect-config.json`. The configuration are as follows:
351+
However, since it utilizes reflection for the option store object passed to `Cmd#parseFor`, the reflection configurations for the class of this object need to be specified in `reflect-config.json`. The configuration are as follows:
369352

370353
```json
371354
[
@@ -391,8 +374,9 @@ This framework supports JDK 21 or later.
391374

392375
### Actually checked JDK versions:
393376

394-
- GraalVM CE 21.0.2+13.1 (openjdk version 21.0.2)
395-
- GraalVM CE 22.0.1+8.1 (openjdk version 22.0.1)
377+
- GraalVM 21.0.4+8.1 (build 21.0.4+8-LTS-jvmci-23.1-b41)
378+
- GraalVM 22.0.2+9.1 (build 22.0.2+9-jvmci-b01)
379+
- GraalVM 23+37.1 (build 23+37-jvmci-b01)
396380

397381
## License
398382

0 commit comments

Comments
 (0)