forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgsParser.java
More file actions
143 lines (122 loc) · 5.93 KB
/
ArgsParser.java
File metadata and controls
143 lines (122 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package reposense.parser;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.action.HelpArgumentAction;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
import net.sourceforge.argparse4j.inf.Namespace;
import reposense.model.CliArguments;
import reposense.model.ConfigCliArguments;
import reposense.model.LocationsCliArguments;
import reposense.model.ViewCliArguments;
/**
* Verifies and parses a string-formatted date to a {@code CliArguments} object.
*/
public class ArgsParser {
public static final String DEFAULT_REPORT_NAME = "reposense-report";
public static final List<String> DEFAULT_FORMATS = Arrays.asList(
"adoc", "cs", "css", "fxml", "gradle", "html", "java", "js", "json", "jsp", "md", "py", "tag", "xml");
private static final String PROGRAM_USAGE = "java -jar RepoSense.jar";
private static final String PROGRAM_DESCRIPTION =
"RepoSense is a contribution analysis tool for Git repositories.";
private static final String MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE =
"\"Since Date\" cannot be later than \"Until Date\"";
private static ArgumentParser getArgumentParser() {
ArgumentParser parser = ArgumentParsers
.newFor(PROGRAM_USAGE)
.addHelp(false)
.build()
.description(PROGRAM_DESCRIPTION);
MutuallyExclusiveGroup mutexParser = parser
.addMutuallyExclusiveGroup(PROGRAM_USAGE)
.required(false);
parser.addArgument("-h", "--help")
.help("Show help message.")
.action(new HelpArgumentAction());
mutexParser.addArgument("-config")
.type(new ConfigFolderArgumentType())
.metavar("PATH")
.setDefault(Paths.get("").toAbsolutePath())
.help("The directory containing the config files."
+ "If not provided, the config files will be obtained from the current working directory.");
mutexParser.addArgument("-repo", "-repos")
.nargs("+")
.dest("repos")
.metavar("LOCATION")
.help("The GitHub URL or disk locations to clone repository.");
mutexParser.addArgument("-view")
.metavar("PATH")
.type(new ReportFolderArgumentType())
.help("Starts a server to display the dashboard in the provided directory.");
parser.addArgument("-output")
.metavar("PATH")
.type(new OutputFolderArgumentType())
.setDefault(Paths.get(ArgsParser.DEFAULT_REPORT_NAME))
.help("The directory to output the report folder, reposense-report. "
+ "If not provided, the report folder will be created in the current working directory.");
parser.addArgument("-since")
.metavar("dd/MM/yyyy")
.type(new DateArgumentType())
.setDefault(Optional.empty())
.help("The date to start filtering.");
parser.addArgument("-until")
.metavar("dd/MM/yyyy")
.type(new DateArgumentType())
.setDefault(Optional.empty())
.help("The date to stop filtering.");
parser.addArgument("-formats")
.nargs("*")
.metavar("FORMAT")
.type(new AlphanumericArgumentType())
.setDefault(DEFAULT_FORMATS)
.help("The alphanumeric file formats to process.\n"
+ "If not provided, default file formats will be used.\n"
+ "Please refer to userguide for more information.");
return parser;
}
/**
* Parses the given string arguments to a {@code CliArguments} object.
*
* @throws ParseException if the given string arguments fails to parse to a {@code CliArguments} object.
*/
public static CliArguments parse(String[] args) throws ParseException {
try {
ArgumentParser parser = getArgumentParser();
Namespace results = parser.parseArgs(args);
Path configFolderPath = results.get("config");
Path reportFolderPath = results.get("view");
Path outputFolderPath = results.get("output");
Optional<Date> sinceDate = results.get("since");
Optional<Date> untilDate = results.get("until");
List<String> formats = results.get("formats");
List<String> locations = results.get("repos");
verifyDatesRangeIsCorrect(sinceDate, untilDate);
if (locations != null) {
return new LocationsCliArguments(locations, outputFolderPath, sinceDate, untilDate, formats);
}
if (reportFolderPath != null) {
return new ViewCliArguments(reportFolderPath);
}
return new ConfigCliArguments(configFolderPath, outputFolderPath, sinceDate, untilDate, formats);
} catch (ArgumentParserException ape) {
throw new ParseException(getArgumentParser().formatUsage() + ape.getMessage() + "\n");
}
}
/**
* Verifies that {@code sinceDate} is earlier than {@code untilDate}.
*
* @throws ParseException if {@code sinceDate} supplied is later than {@code untilDate}.
*/
private static void verifyDatesRangeIsCorrect(Optional<Date> sinceDate, Optional<Date> untilDate)
throws ParseException {
if (sinceDate.isPresent() && untilDate.isPresent() && sinceDate.get().getTime() > untilDate.get().getTime()) {
throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE);
}
}
}