Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1989] Reduce scope of try-catch block in ArgsParser::parse #2074

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 65 additions & 63 deletions src/main/java/reposense/parser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,76 +262,78 @@ private static ArgumentParser getArgumentParser() {
* @throws ParseException if the given string arguments fails to parse to a {@link CliArguments} object.
*/
public static CliArguments parse(String[] args) throws HelpScreenException, ParseException {
ArgumentParser parser = getArgumentParser();
Namespace results;

try {
ArgumentParser parser = getArgumentParser();
Namespace results = parser.parseArgs(args);

Path configFolderPath = results.get(CONFIG_FLAGS[0]);
Path reportFolderPath = results.get(VIEW_FLAGS[0]);
Path outputFolderPath = results.get(OUTPUT_FLAGS[0]);
ZoneId zoneId = results.get(TIMEZONE_FLAGS[0]);
Path assetsFolderPath = results.get(ASSETS_FLAGS[0]);
List<String> locations = results.get(REPO_FLAGS[0]);
List<FileType> formats = FileType.convertFormatStringsToFileTypes(results.get(FORMAT_FLAGS[0]));
boolean isStandaloneConfigIgnored = results.get(IGNORE_CONFIG_FLAGS[0]);
boolean isFileSizeLimitIgnored = results.get(IGNORE_SIZELIMIT_FLAGS[0]);
boolean shouldIncludeLastModifiedDate = results.get(LAST_MODIFIED_DATE_FLAGS[0]);
boolean shouldPerformShallowCloning = results.get(SHALLOW_CLONING_FLAGS[0]);
boolean shouldFindPreviousAuthors = results.get(FIND_PREVIOUS_AUTHORS_FLAGS[0]);
boolean isTestMode = results.get(TEST_MODE_FLAG[0]);
int numCloningThreads = results.get(CLONING_THREADS_FLAG[0]);
int numAnalysisThreads = results.get(ANALYSIS_THREADS_FLAG[0]);

CliArguments.Builder cliArgumentsBuilder = new CliArguments.Builder()
.configFolderPath(configFolderPath)
.reportDirectoryPath(reportFolderPath)
.outputFilePath(outputFolderPath)
.zoneId(zoneId)
.assetsFilePath(assetsFolderPath)
.locations(locations)
.formats(formats)
.isStandaloneConfigIgnored(isStandaloneConfigIgnored)
.isFileSizeLimitIgnored(isFileSizeLimitIgnored)
.isLastModifiedDateIncluded(shouldIncludeLastModifiedDate)
.isShallowCloningPerformed(shouldPerformShallowCloning)
.isFindingPreviousAuthorsPerformed(shouldFindPreviousAuthors)
.numCloningThreads(numCloningThreads)
.numAnalysisThreads(numAnalysisThreads)
.isTestMode(isTestMode);

LogsManager.setLogFolderLocation(outputFolderPath);

if (locations == null && configFolderPath.equals(DEFAULT_CONFIG_PATH)) {
logger.info(MESSAGE_USING_DEFAULT_CONFIG_PATH);
}
results = parser.parseArgs(args);
} catch (HelpScreenException hse) {
throw hse;
} catch (ArgumentParserException ape) {
throw new ParseException(getArgumentParser().formatUsage() + ape.getMessage() + "\n");
}

addReportConfigToBuilder(cliArgumentsBuilder, results);
addAnalysisDatesToBuilder(cliArgumentsBuilder, results);
Path configFolderPath = results.get(CONFIG_FLAGS[0]);
Path reportFolderPath = results.get(VIEW_FLAGS[0]);
Path outputFolderPath = results.get(OUTPUT_FLAGS[0]);
ZoneId zoneId = results.get(TIMEZONE_FLAGS[0]);
Path assetsFolderPath = results.get(ASSETS_FLAGS[0]);
List<String> locations = results.get(REPO_FLAGS[0]);
List<FileType> formats = FileType.convertFormatStringsToFileTypes(results.get(FORMAT_FLAGS[0]));
boolean isStandaloneConfigIgnored = results.get(IGNORE_CONFIG_FLAGS[0]);
boolean isFileSizeLimitIgnored = results.get(IGNORE_SIZELIMIT_FLAGS[0]);
boolean shouldIncludeLastModifiedDate = results.get(LAST_MODIFIED_DATE_FLAGS[0]);
boolean shouldPerformShallowCloning = results.get(SHALLOW_CLONING_FLAGS[0]);
boolean shouldFindPreviousAuthors = results.get(FIND_PREVIOUS_AUTHORS_FLAGS[0]);
boolean isTestMode = results.get(TEST_MODE_FLAG[0]);
int numCloningThreads = results.get(CLONING_THREADS_FLAG[0]);
int numAnalysisThreads = results.get(ANALYSIS_THREADS_FLAG[0]);

CliArguments.Builder cliArgumentsBuilder = new CliArguments.Builder()
.configFolderPath(configFolderPath)
.reportDirectoryPath(reportFolderPath)
.outputFilePath(outputFolderPath)
.zoneId(zoneId)
.assetsFilePath(assetsFolderPath)
.locations(locations)
.formats(formats)
.isStandaloneConfigIgnored(isStandaloneConfigIgnored)
.isFileSizeLimitIgnored(isFileSizeLimitIgnored)
.isLastModifiedDateIncluded(shouldIncludeLastModifiedDate)
.isShallowCloningPerformed(shouldPerformShallowCloning)
.isFindingPreviousAuthorsPerformed(shouldFindPreviousAuthors)
.numCloningThreads(numCloningThreads)
.numAnalysisThreads(numAnalysisThreads)
.isTestMode(isTestMode);

LogsManager.setLogFolderLocation(outputFolderPath);

if (locations == null && configFolderPath.equals(DEFAULT_CONFIG_PATH)) {
logger.info(MESSAGE_USING_DEFAULT_CONFIG_PATH);
}

boolean isViewModeOnly = reportFolderPath != null
&& !reportFolderPath.equals(EMPTY_PATH)
&& configFolderPath.equals(DEFAULT_CONFIG_PATH)
&& locations == null;
cliArgumentsBuilder.isViewModeOnly(isViewModeOnly);
addReportConfigToBuilder(cliArgumentsBuilder, results);
addAnalysisDatesToBuilder(cliArgumentsBuilder, results);

boolean isAutomaticallyLaunching = reportFolderPath != null;
if (isAutomaticallyLaunching && !reportFolderPath.equals(EMPTY_PATH) && !isViewModeOnly) {
logger.info(String.format("Ignoring argument '%s' for --view.", reportFolderPath.toString()));
}
cliArgumentsBuilder.isAutomaticallyLaunching(isAutomaticallyLaunching);
boolean isViewModeOnly = reportFolderPath != null
&& !reportFolderPath.equals(EMPTY_PATH)
&& configFolderPath.equals(DEFAULT_CONFIG_PATH)
&& locations == null;
cliArgumentsBuilder.isViewModeOnly(isViewModeOnly);

boolean isAutomaticallyLaunching = reportFolderPath != null;
if (isAutomaticallyLaunching && !reportFolderPath.equals(EMPTY_PATH) && !isViewModeOnly) {
logger.info(String.format("Ignoring argument '%s' for --view.", reportFolderPath.toString()));
}
cliArgumentsBuilder.isAutomaticallyLaunching(isAutomaticallyLaunching);

boolean shouldPerformFreshCloning = isTestMode
? results.get(FRESH_CLONING_FLAG[0])
: DEFAULT_SHOULD_FRESH_CLONE;
cliArgumentsBuilder.isFreshClonePerformed(shouldPerformFreshCloning);

return cliArgumentsBuilder.build();
} catch (HelpScreenException hse) {
throw hse;
} catch (ArgumentParserException ape) {
throw new ParseException(getArgumentParser().formatUsage() + ape.getMessage() + "\n");
}
boolean shouldPerformFreshCloning = isTestMode
? results.get(FRESH_CLONING_FLAG[0])
: DEFAULT_SHOULD_FRESH_CLONE;
cliArgumentsBuilder.isFreshClonePerformed(shouldPerformFreshCloning);

return cliArgumentsBuilder.build();
}

/**
Expand Down
Loading