Skip to content
This repository has been archived by the owner on Oct 16, 2018. It is now read-only.

Commit

Permalink
CSAFE-2249 - Replace CLI flag parsing library JSAP with JCommander
Browse files Browse the repository at this point in the history
Changed junit test code to reflect this
added unit test cases for ogcli and objectfilecli
  • Loading branch information
nlahmedIBM committed Aug 24, 2016
1 parent 3bc4993 commit 5308537
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 76 deletions.
10 changes: 6 additions & 4 deletions og-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.martiansoftware</groupId>
<artifactId>jsap</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
Expand Down Expand Up @@ -93,6 +89,12 @@
<groupId>com.cleversafe</groupId>
<artifactId>apache-juice</artifactId>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2015 Cleversafe, Inc. All rights reserved.
* Copyright (C) 2005-2016 Cleversafe, Inc. All rights reserved.
*
* Contact Information: Cleversafe, Inc. 222 South Riverside Plaza Suite 1700 Chicago, IL 60606, USA
*
Expand All @@ -19,16 +19,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cleversafe.og.util.Version;
import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;

/**
* A utility class for creating an application cli
Expand All @@ -45,13 +42,13 @@ private Application() {}
* Creates a Cli instance
*
* @param name a textual name that describes this cli
* @param jsapResourceName the classpath location for this cli's jsap configuration
* @param args command line arguments
* @return a Cli instance
* @throws NullPointerException if name, jsapResourceName or args are null
* @throws NullPointerException if name, or args are null
*/
public static Cli cli(final String name, final String jsapResourceName, final String[] args) {
return new Cli(name, jsapResourceName, args);
public static Cli cli(final String name, final String[] args) {
GetOpt getopt = ArgumentProcessorFactory.makeArgumentProcessor(name);
return new Cli(name, getopt, args);
}

/**
Expand All @@ -62,25 +59,23 @@ public static Cli cli(final String name, final String jsapResourceName, final St
public static class Cli {
private static final Logger _consoleLogger = LoggerFactory.getLogger("ConsoleLogger");
private final String name;
private final JSAP jsap;
private final JSAPResult jsapResult;
private GetOpt getopt;

/**
* Creates an instance
*
* @param name a textual name that describes this cli
* @param jsapResourceName the classpath location for this cli's jsap configuration
* @param args command line arguments
* @return a Cli instance
* @throws NullPointerException if name, jsapResourceName or args are null
* @throws NullPointerException if name, or args are null
*/
private Cli(final String name, final String jsapResourceName, final String[] args) {
private Cli(final String name, final GetOpt getopt, final String[] args) {
this.name = checkNotNull(name);
checkNotNull(jsapResourceName);
checkNotNull(args);
checkNotNull(getopt);
try {
this.jsap = new JSAP(Application.getResource(jsapResourceName).toURL());
this.jsapResult = this.jsap.parse(args);
this.getopt = getopt;
getopt.processArguments(name, args);
} catch (final Exception e) {
throw new IllegalArgumentException(e);
}
Expand All @@ -92,7 +87,7 @@ private Cli(final String name, final String jsapResourceName, final String[] arg
* @return true if the caller should stop; false otherwise
*/
public boolean shouldStop() {
return error() || help() || version();
return getopt.isError() || getopt.getHelp() || getopt.getVersion();
}

/**
Expand All @@ -101,7 +96,7 @@ public boolean shouldStop() {
* @return true if cli parsing errors occurred; false otherwise
*/
public boolean error() {
return !this.jsapResult.success();
return getopt.isError();
}

/**
Expand All @@ -110,7 +105,7 @@ public boolean error() {
* @return true if a help flag is present; false otherwise
*/
public boolean help() {
return this.jsapResult.getBoolean("help");
return getopt.getHelp();
}

/**
Expand All @@ -119,35 +114,30 @@ public boolean help() {
* @return true if a version plag is present; false otherwise
*/
public boolean version() {
return this.jsapResult.getBoolean("version");
return getopt.getVersion();
}

/**
* Makes available the underlying {@code JSAPResult } result object
*
* @return the underlying cli results object
*/
public JSAPResult flags() {
return this.jsapResult;

public GetOpt getOpt() {
return getopt;
}


/**
* Generates and logs a suitable cli usage block to the console
*/
public void printUsage() {
_consoleLogger.info("Usage: {} {}", this.name, this.jsap.getUsage());
_consoleLogger.info(this.jsap.getHelp());
//_consoleLogger.info("Usage: {} {}", this.name, this.jsap.getUsage());
StringBuilder sb = new StringBuilder();
getopt.usage(sb);
_consoleLogger.info(sb.toString());
}

/**
* Generates and logs a suitable errors block to console
*/
public void printErrors() {
@SuppressWarnings("rawtypes")
final Iterator errs = this.jsapResult.getErrorMessageIterator();
while (errs.hasNext()) {
_consoleLogger.error("{}", errs.next());
}
_consoleLogger.error("{}", getopt.getErrorMsg());
}

/**
Expand All @@ -158,6 +148,38 @@ public void printVersion() {
}
}

/**
* Helper Factory class to create the appropriate Argument Processor based on the application name
*/
private static class ArgumentProcessorFactory {

private ArgumentProcessorFactory() {}

/***
*
* @param appName a textual name that describes the cli
* @return a Cli instance
*/
public static GetOpt makeArgumentProcessor(String appName) {

if (appName.contentEquals("application")) {
// just basic Argument process that supports options help and version - for testing only
return new GetOpt();
} else if (appName.contentEquals("og")) {
// Arg processor for Object Generator
return new OGGetOpt();
} else if (appName.contentEquals("object-file")) {
// Arg processor for Object File
return new ObjectFileGetOpt();
} else {
// throw IllegalArgument exception
throw new IllegalArgumentException("Illegal application Name " + appName);
}

}

}

/**
* Creates a URI that points to a classpath resource
*
Expand Down
82 changes: 82 additions & 0 deletions og-configuration/src/main/java/com/cleversafe/og/cli/GetOpt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2005-2016 Cleversafe, Inc. All rights reserved.
*
* Contact Information: Cleversafe, Inc. 222 South Riverside Plaza Suite 1700 Chicago, IL 60606, USA
*
* licensing@cleversafe.com
*/

package com.cleversafe.og.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;


public class GetOpt {
@Parameter(names = {"--version", "-v"}, description = "Prints the og version and exits")
private boolean version = false;

@Parameter(names = {"--help", "-h"}, description = "Prints this help and exits")
private boolean help;

private boolean error;

private String errorMsg = "";

private JCommander jc;

public Boolean getVersion() {
return version;
}

public void setVersion(Boolean version) {
this.version = version;
}

public Boolean getHelp() {
return help;
}

public void setHelp(Boolean help) {
this.help = help;
}

public boolean isError() {
return error;
}

public void setError(boolean error) {
this.error = error;
}

public String getErrorMsg() {
return errorMsg;
}

public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}



public void processArguments (String progName, String args[]) {

try {
jc = new JCommander(this);
jc.setProgramName(progName);
jc.parse(args);
} catch(RuntimeException re) {
// record error in the state to match with the existing semantics
error = true;
errorMsg = re.getLocalizedMessage();

}

}

public void usage(StringBuilder sb) {
jc.usage(sb);
}


}
51 changes: 51 additions & 0 deletions og-configuration/src/main/java/com/cleversafe/og/cli/OGGetOpt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2005-2016 Cleversafe, Inc. All rights reserved.
*
* Contact Information: Cleversafe, Inc. 222 South Riverside Plaza Suite 1700 Chicago, IL 60606, USA
*
* licensing@cleversafe.com
*/

package com.cleversafe.og.cli;
import java.util.ArrayList;
import java.util.List;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import static com.google.common.base.Preconditions.checkNotNull;


class OGGetOpt extends GetOpt {

@Parameter(description = "<og_config>", required = true)
private List<String> arguments = new ArrayList<String>();

public OGGetOpt() {

}

public List<String> getArguments() {
return arguments;
}

public void setArguments(List<String> arguments) {
this.arguments = arguments;
}

public String getOGConfigFileName() {
checkNotNull(arguments);
return arguments.get(0);

}

public GetOpt getObject() {
return new OGGetOpt();
}






}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2015 Cleversafe, Inc. All rights reserved.
* Copyright (C) 2005-2016 Cleversafe, Inc. All rights reserved.
*
* Contact Information: Cleversafe, Inc. 222 South Riverside Plaza Suite 1700 Chicago, IL 60606, USA
*
Expand Down Expand Up @@ -51,7 +51,7 @@ public class ObjectFile {
private ObjectFile() {}

public static void main(final String[] args) {
final Cli cli = Application.cli("object-file", "objectfile.jsap", args);
final Cli cli = Application.cli("object-file", args);
if (cli.shouldStop()) {
if (cli.help()) {
cli.printUsage();
Expand All @@ -65,19 +65,21 @@ public static void main(final String[] args) {
Application.exit(0);
}

final File input = cli.flags().getFile("input");
final boolean write = cli.flags().getBoolean("write");
final boolean read = cli.flags().getBoolean("read");
final boolean filter = cli.flags().getBoolean("filter");
final boolean upgrade = cli.flags().getBoolean("upgrade");
final boolean split = cli.flags().getBoolean("split");
final int splitSize = cli.flags().getInt("split-size");
final String output = cli.flags().getString("output");
final long minFilesize = cli.flags().getLong("min-filesize");
final long maxFilesize = cli.flags().getLong("max-filesize");
final int minContainerSuffix = cli.flags().getInt("min-suffix");
final int maxContainerSuffix = cli.flags().getInt("max-suffix");
final int[] tmpContainerSuffixes = cli.flags().getIntArray("container-suffixes");
ObjectFileGetOpt getOpt = (ObjectFileGetOpt)cli.getOpt();
final File input = getOpt.getInput();

final boolean write = getOpt.getWrite();
final boolean read = getOpt.getRead();
final boolean filter = getOpt.getFilter();
final boolean upgrade = getOpt.getUpgrade();
final boolean split = getOpt.getSplit();
final int splitSize = getOpt.getSplitSize();
final String output = getOpt.getOutput();
final long minFilesize = getOpt.getMinSize();
final long maxFilesize = getOpt.getMaxSize();
final int minContainerSuffix = getOpt.getMinSuffix();
final int maxContainerSuffix = getOpt.getMaxSuffix();
final int[] tmpContainerSuffixes = getOpt.getContainerSuffixes();

final Set<Integer> containerSuffixes = ImmutableSet.copyOf(Ints.asList(tmpContainerSuffixes));

Expand Down
Loading

0 comments on commit 5308537

Please sign in to comment.