Skip to content

Commit

Permalink
Improve error handling for missing flags
Browse files Browse the repository at this point in the history
* When missing --pds3-lablel flag, we need to throw an error
* Add logging
  • Loading branch information
jordanpadams committed Feb 23, 2024
1 parent 42c8a80 commit 4a930df
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/main/java/gov/nasa/pds/imaging/generate/GenerateLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Class used as Command-line interface endpoint. Parses command-line arguments
Expand All @@ -62,7 +63,7 @@
public class GenerateLauncher {

/** Logger. **/
private static Logger log = Logger.getLogger(GenerateLauncher.class.getName());
private static Logger LOGGER = LoggerFactory.getLogger(GenerateLauncher.class.getName());

private String basePath;
private List<String> lblList;
Expand Down Expand Up @@ -102,10 +103,10 @@ public final void displayHelp() {
*
*/
public final void displayVersion() {
System.err.println("\n" + ToolInfo.getName());
System.err.println(ToolInfo.getVersion());
System.err.println("Release Date: " + ToolInfo.getReleaseDate());
System.err.println(ToolInfo.getCopyright() + "\n");
LOGGER.info("\n" + ToolInfo.getName());
LOGGER.info(ToolInfo.getVersion());
LOGGER.info("Release Date: " + ToolInfo.getReleaseDate());
LOGGER.info(ToolInfo.getCopyright() + "\n");
}

public final void generate(){
Expand Down Expand Up @@ -199,9 +200,9 @@ public final void query(final CommandLine line) throws Exception {

// Let's default to the one label if -p flag was specified,
// otherwise loop through the lbl list
if (this.lblList == null) {
throw new InvalidOptionException("Missing -p or -l flags. " +
"One or many PDS3 label must be specified.");
if (this.lblList.isEmpty()) {
throw new InvalidOptionException("Missing --pds3-label flag. " +
"One or more PDS3 labels required.");
} else {
String filepath;
PDS3Label pdsLabel;
Expand Down Expand Up @@ -260,7 +261,7 @@ public final void query(final CommandLine line) throws Exception {
this.generatorList.add(new Generator(pdsObj, this.templateFile,
outputFile, this.isXML));
} else {
log.warning(lbl + " does not exist.");
LOGGER.warn(lbl + " does not exist.");
}
}
}
Expand Down Expand Up @@ -293,9 +294,11 @@ public static void main(final String[] args) {
launcher.query(commandline);
launcher.generate();
// launcher.closeHandlers();
} catch (InvalidOptionException e) {
LOGGER.error(e.getMessage());
System.exit(1);
} catch (final ParseException pEx) {
System.err.println("Command-line parse failure: "
+ pEx.getMessage());
LOGGER.error("Command-line parse failure: " + pEx.getMessage());
System.exit(1);
} catch (final Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 4a930df

Please sign in to comment.