Skip to content

Commit

Permalink
[reposense#1989] Reduce scope of try-catch block in ArgsParser::parse (
Browse files Browse the repository at this point in the history
…reposense#2074)

The old scope of the try block in ArgsParser::parse 
is too permissive, making it less apparent which 
methods throw the associated exceptions.

Let's reduce the scope of the try block in 
ArgsParser::parse.
  • Loading branch information
nseah21 authored Dec 31, 2023
1 parent d8ce443 commit 3cd1b6e
Showing 1 changed file with 65 additions and 63 deletions.
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

0 comments on commit 3cd1b6e

Please sign in to comment.