Skip to content
This repository has been archived by the owner on Jul 11, 2021. It is now read-only.

Commit

Permalink
Removed dependency on Apache Commons CLI
Browse files Browse the repository at this point in the history
This increased the size of the JAR file by 200 kb just to use a single object from the library. The Bullwinkle library already provides a command-line parser, so the code was modified to use it instead.
  • Loading branch information
sylvainhalle committed Apr 26, 2018
1 parent fca1b00 commit a9202f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 103 deletions.
128 changes: 42 additions & 86 deletions Source/Server/src/ca/uqac/lif/cornipickle/server/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Cornipickle, validation of layout bugs in web applications
Copyright (C) 2015-2016 Sylvain Hallé
Copyright (C) 2015-2018 Sylvain Hallé
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -22,22 +22,16 @@
import java.util.List;
import java.util.logging.Level;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

import ca.uqac.lif.cornipickle.Interpreter;
import ca.uqac.lif.cornipickle.util.AnsiPrinter;


import ca.uqac.lif.util.CliParser;
import ca.uqac.lif.util.CliParser.Argument;
import ca.uqac.lif.util.CliParser.ArgumentMap;

public class Main
{
/**
* Return codes
* Exit codes
*/
public static final int ERR_OK = 0;
public static final int ERR_PARSE = 2;
Expand All @@ -50,8 +44,8 @@ public class Main
/**
* Build string to identify versions
*/
protected static final String VERSION_STRING = "0.0";
protected static final String BUILD_STRING = "20180424";
protected static final String VERSION_STRING = "0.1";
protected static final String BUILD_STRING = "20180426";

/**
* Default server name
Expand Down Expand Up @@ -99,8 +93,14 @@ public void run()
}));

// Parse command line arguments
Options options = setupOptions();
CommandLine c_line = setupCommandLine(args, options, stderr);
CliParser parser = setupCommandLine();
ArgumentMap c_line = parser.parse(args);
if (c_line == null)
{
// oops, something went wrong
stderr.println("ERROR parsing command line arguments\n");
System.exit(ERR_ARGUMENTS);
}
assert c_line != null;
if (c_line.hasOption("verbosity"))
{
Expand All @@ -109,15 +109,15 @@ public void run()

if (c_line.hasOption("version"))
{
stderr.println("(C) 2015-2016 Laboratoire d'informatique formelle");
stderr.println("(C) 2015-2018 Laboratoire d'informatique formelle");
stderr.println("This program comes with ABSOLUTELY NO WARRANTY.");
stderr.println("This is a free software, and you are welcome to redistribute it");
stderr.println("under certain conditions. See the file LICENSE for details.\n");
System.exit(ERR_OK);
}
if (c_line.hasOption("h"))
{
showUsage(options);
showUsage(parser, stderr);
System.exit(ERR_OK);
}
if (c_line.hasOption("p"))
Expand All @@ -144,7 +144,7 @@ public void run()

// The remaining arguments are the Cornipickle files to read
CornipickleServer server = new CornipickleServer(server_name, server_port);
List<String> remaining_args = c_line.getArgList();
List<String> remaining_args = c_line.getOthers();
for (String filename : remaining_args)
{
stdout.setForegroundColor(AnsiPrinter.Color.BROWN);
Expand Down Expand Up @@ -184,84 +184,40 @@ protected static void println(PrintStream out, String message, int verbosity_lev
}
}

/**
* Sets up the options for the command line parser
* @return The options
*/
private static Options setupOptions()
{
Options options = new Options();
Option opt;
opt = Option.builder("h")
.longOpt("help")
.desc("Display command line usage")
.build();
options.addOption(opt);
opt = Option.builder("s")
.longOpt("servername")
.argName("x")
.hasArg()
.desc("Set server name or IP address x (default: " + s_defaultServerName + ")")
.build();
options.addOption(opt);
opt = Option.builder("p")
.longOpt("port")
.argName("x")
.hasArg()
.desc("Listen on port x (default: " + s_defaultPort + ")")
.build();
options.addOption(opt);
opt = Option.builder()
.longOpt("serve-as")
.argName("path")
.hasArg()
.desc("Serve local folder as path")
.build();
options.addOption(opt);
opt = Option.builder("a")
.longOpt("android")
//.argName("x")
//.hasArg()
.desc("Change platform type")
.build();
options.addOption(opt);
return options;
}

/**
* Show the benchmark's usage
* @param options The options created for the command line parser
*/
private static void showUsage(Options options)
private static void showUsage(CliParser parser, PrintStream stderr)
{
HelpFormatter hf = new HelpFormatter();
hf.printHelp("java -jar Cornipickle.jar [options] [file1 [file2 ...]]", options);
parser.printHelp("java -jar cornipickle.jar [options] [file1 [file2 ...]]", stderr);
}
/**
* Sets up the command line parser
* @param args The command line arguments passed to the class' {@link main}
* method
* @param options The command line options to be used by the parser
* @return The object that parsed the command line parameters
* @return The object that parses the command line parameters
*/
private static CommandLine setupCommandLine(String[] args, Options options, PrintStream stderr)
private static CliParser setupCommandLine()
{
CommandLineParser parser = new DefaultParser();
CommandLine c_line = null;
try
{
// parse the command line arguments
c_line = parser.parse(options, args);
}
catch (org.apache.commons.cli.ParseException exp)
{
// oops, something went wrong
stderr.println("ERROR: " + exp.getMessage() + "\n");
//HelpFormatter hf = new HelpFormatter();
//hf.printHelp(t_gen.getAppName() + " [options]", options);
System.exit(ERR_ARGUMENTS);
}
return c_line;
CliParser parser = new CliParser();
parser.addArgument(new Argument().withShortName("h")
.withLongName("help")
.withDescription("Display command line usage"));
parser.addArgument(new Argument().withShortName("s")
.withLongName("servername")
.withArgument("x")
.withDescription("Set server name or IP address x (default: " + s_defaultServerName + ")"));
parser.addArgument(new Argument().withShortName("p")
.withLongName("port")
.withArgument("x")
.withDescription("Listen on port x (default: " + s_defaultPort + ")"));
parser.addArgument(new Argument()
.withLongName("serve-as")
.withArgument("path")
.withDescription("Serve local folder as path"));
parser.addArgument(new Argument().withShortName("a")
.withLongName("android")
.withDescription("Change platform type to Android"));
return parser;
}

private static void showHeader(PrintStream out)
Expand Down
18 changes: 13 additions & 5 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@
</fileset>
<pathelement path="${java.class.path}"/>
</path>
<path id="build.test.classpath">
<pathelement location="${build.bindir}"/>
<fileset dir="${build.depdir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${build.libdir}">
<include name="*.jar"/>
</fileset>
<pathelement location="${build.test.bindir}"/>
<pathelement path="${java.class.path}"/>
</path>
</target>

<!-- Check if a local rt.jar is present. If so, it will override the
Expand Down Expand Up @@ -231,11 +242,10 @@
<!-- Target: jar
Generates a JAR file with the compiled files and javadoc
-->
<target name="jar" depends="compile" description="Create the runnable JAR">
<target name="jar" depends="compile,javadoc" description="Create the runnable JAR">
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<echo message="WITHDOC ${build.jar.withdoc}"/>
<jar destfile="${build.jar.filename}" filesetmanifest="skip">
<manifest>
<attribute name="Main-Class" value="${build.mainclass}"/>
Expand Down Expand Up @@ -270,9 +280,7 @@
<jacoco:coverage>
<junit printsummary="yes" haltonfailure="false" fork="true"
failureproperty="test.failed">
<classpath>
<classpath refid="build.classpath"/>
</classpath>
<classpath refid="build.test.classpath"/>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${test.reportdir}">
Expand Down
13 changes: 1 addition & 12 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<name>Cornipickle</name>

<!-- The project's version number -->
<version>1.2.1</version>
<version>0.1</version>

<!-- The project's author. Currently this only
shows up in the footer of the Javadoc documentation. -->
Expand Down Expand Up @@ -56,17 +56,6 @@
Write JAR dependencies here.
-->
<dependencies>
<dependency>
<!--
Apache Commons CLI. Used to parse command-line options.
-->
<name>Apache Commons CLI</name>
<classname>org.apache.commons.cli.GnuParser</classname>
<files>
<zip>http://archive.apache.org/dist/commons/cli/binaries/commons-cli-1.3.1-bin.zip</zip>
</files>
<bundle>true</bundle>
</dependency>
<dependency>
<!--
Bullwinkle. Used to parse Cornipickle statements.
Expand Down

0 comments on commit a9202f6

Please sign in to comment.