Skip to content

Commit cd35351

Browse files
committed
Zapisywanie w formacie JSON za pomocą CL
1 parent 75b98f8 commit cd35351

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,30 @@ A simple and useful generator of important christian dates.
99
<artifactId>cmf</artifactId>
1010
<version>0.1.1</version>
1111
</dependency>
12-
```
12+
```
13+
### Command line execution
14+
Prints a command line help:
15+
```shell
16+
java -jar cmf.jar -h
17+
```
18+
#### Run without arguments (default run)
19+
```shell
20+
java -jar cmf.jar
21+
```
22+
It causes generating a movable feasts row for the current year and saves as JSON file in the current location.
23+
It formats a file name according to the pattern:<a name="default-filename"/>
24+
```regexp
25+
(\d{4})-(\d{2})-(\d{2})_(\d{2})(\d{2})(\d{2})\.cmf\.json
26+
```
27+
e.g. `2024-10-04_150923.cmf.json`.
28+
#### Run with options
29+
Available options:
30+
31+
| Option | Short | Default<br/>value | Description |
32+
|--------------|-------|----------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
33+
| `--dir` | `-d` | the current directory | It determines a directory of an output file. |
34+
| `--filename` | `-n` | [default file name](#default-filename) | It determines a file name of an output file. |
35+
| `--format` | `-f` | `json` | It determines format for the output file. It is one of:<ul><li>`json`/`prettyjson`</li></ul> |
36+
| `--help` | `-h` | not applicable | It prints help. |
37+
| `--years` | `-y` | the current year | It determines a list of year for a MFR generator. Numbers can be separated by dot (e.g. `2024.2028.2050`) or determine a range of values (e.g. `2024-2050`). |
38+
Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,99 @@
11
package pl.koder95.cmf;
22

33
import org.apache.commons.cli.*;
4+
import pl.koder95.cmf.format.JSON;
45

6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
13+
import java.time.format.DateTimeFormatter;
514
import java.util.Arrays;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.IntStream;
619

720
public class Main {
821

22+
private Main() {}
23+
924
public static void main(String[] args) {
1025
System.out.println("Catholic Moveable Feasts by Kamil Jan Mularski [Koder95]. See more: https://koder95.pl/");
1126
System.out.println("- It is starting with arguments: " + Arrays.deepToString(args));
1227
System.out.println();
1328

1429
Options options = new Options();
1530
options.addOption("h", "help", false, "wyświetla pomoc programu");
31+
options.addOption("d", "dir", true, "określa katalog dla zapisania pliku");
32+
options.addOption("n", "filename", true, "określa nazwę pliku");
33+
options.addOption(Option.builder("f")
34+
.longOpt("format")
35+
.hasArg()
36+
.desc("określa format danych wyjściowych")
37+
.build());
38+
options.addOption(Option.builder("y")
39+
.longOpt("years")
40+
.hasArg()
41+
.type(int[].class)
42+
.converter(string -> {
43+
System.out.println("Converting: " + string);
44+
String digits = "(\\d)+";
45+
if (string.matches(digits)) {
46+
return new int[] { Integer.parseInt(string) };
47+
}
48+
int dash = string.indexOf('-');
49+
if (dash > 0) {
50+
int begin = Integer.parseInt(string.substring(0, dash));
51+
int end = Integer.parseInt(string.substring(dash + 1));
52+
return IntStream.rangeClosed(begin, end).toArray();
53+
} else {
54+
String[] years = string.split("\\.");
55+
System.out.println("Parts: " + Arrays.deepToString(years));
56+
return Arrays.stream(years).filter(s -> s.matches(digits)).mapToInt(Integer::parseInt).toArray();
57+
}
58+
})
59+
.build());
1660
CommandLineParser parser = new DefaultParser(false);
1761
try {
1862
CommandLine cl = parser.parse(options, args);
1963
if (cl.hasOption("h")) {
2064
HelpFormatter hf = new HelpFormatter();
2165
hf.printHelp("cmf", options);
66+
} else {
67+
String dir = cl.hasOption("dir")? cl.getOptionValue("dir") : "./";
68+
String filename = cl.hasOption("filename")? cl.getOptionValue("filename") : defaultFilename();
69+
String format = cl.hasOption("format")? cl.getOptionValue("format") : "json";
70+
int[] years = cl.hasOption("years")? cl.getParsedOptionValue("years") : new int[] { LocalDate.now().getYear() };
71+
List<FinalMFR> result = generate(years);
72+
save(result, dir, filename, format);
2273
}
23-
} catch (ParseException e) {
74+
} catch (ParseException | IOException e) {
2475
throw new RuntimeException(e);
2576
}
2677
}
78+
79+
private static String defaultFilename() {
80+
LocalDateTime now = LocalDateTime.now();
81+
String formatted = now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
82+
String adapted = formatted.replace('T', '_').replace(":", "");
83+
String clipped = adapted.substring(0, adapted.indexOf('.'));
84+
return clipped + ".cmf";
85+
}
86+
87+
private static List<FinalMFR> generate(int... years) {
88+
System.out.println("Generating for years: " + Arrays.toString(years));
89+
return IntStream.of(years).sorted().mapToObj(MFR_Basic::new).map(MFR_Basic::toStable).map(MFR_Stable::toFinal).collect(Collectors.toList());
90+
}
91+
92+
private static void save(List<FinalMFR> list, String dir, String filename, String format) throws IOException {
93+
if (format.endsWith("json")) {
94+
String json = JSON.toJson(list, format.replace("json", "").equals("pretty"));
95+
Path destin = Paths.get(dir, filename + ".json");
96+
Files.write(destin, Collections.singleton(json), StandardCharsets.UTF_8);
97+
}
98+
}
2799
}

0 commit comments

Comments
 (0)