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

Commit

Permalink
Replace airline with jcommander
Browse files Browse the repository at this point in the history
This replaces the use of airline with jcommander.

Fix #9
  • Loading branch information
io7m committed Nov 16, 2017
1 parent 4e75bc5 commit 35a3603
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 68 deletions.
9 changes: 7 additions & 2 deletions README-CHANGES.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,20 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2017-11-16T20:02:28+00:00" ticket-system="com.github.io7m.jpra" version="0.7.0">
<c:release date="2017-11-16T20:29:40+00:00" ticket-system="com.github.io7m.jpra" version="0.7.0">
<c:changes>
<c:change compatible="false" date="2017-11-16T00:00:00+00:00" summary="All modules are now Java 9 modules. JDK 9 is now required."/>
<c:change compatible="false" date="2017-11-16T00:00:00+00:00" summary="Removed dependency on com.io7m.jnull."/>
<c:change compatible="false" date="2017-11-16T20:02:28+00:00" summary="Replace gs-collections with Vavr">
<c:change compatible="false" date="2017-11-16T00:00:00+00:00" summary="Replace gs-collections with Vavr">
<c:tickets>
<c:ticket id="25"/>
</c:tickets>
</c:change>
<c:change compatible="false" date="2017-11-16T20:29:40+00:00" summary="Replace airline with jcommander">
<c:tickets>
<c:ticket id="9"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
6 changes: 3 additions & 3 deletions com.io7m.jpra.compiler.frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>airline</artifactId>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -78,7 +78,7 @@
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<minimizeJar>false</minimizeJar>
<shadedClassifierName>jpra-c</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.io7m.jpra.compiler.core.JPRAProblemFormatter;
import com.io7m.jpra.compiler.core.JPRAProblemFormatterType;
import com.io7m.jpra.compiler.core.checker.JPRACheckerStandardCapabilities;
Expand All @@ -35,22 +39,16 @@
import com.io7m.jpra.model.names.TypeName;
import com.io7m.jpra.model.types.TypeUserDefinedType;
import com.io7m.junreachable.UnreachableCodeException;
import io.airlift.airline.Arguments;
import io.airlift.airline.Cli;
import io.airlift.airline.Command;
import io.airlift.airline.Help;
import io.airlift.airline.Option;
import io.airlift.airline.ParseArgumentsMissingException;
import io.airlift.airline.ParseArgumentsUnexpectedException;
import io.airlift.airline.ParseOptionMissingException;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.stream.Collectors;

Expand All @@ -66,9 +64,28 @@ public final class Main
LOG = (Logger) LoggerFactory.getLogger(Main.class);
}

private Main()
private final String[] args;
private final HashMap<String, CommandType> commands;
private final JCommander commander;
private int exit_code;

private Main(
final String[] in_args)
{
throw new UnreachableCodeException();
this.args =
Objects.requireNonNull(in_args, "Command line arguments");

final CommandCheck check = new CommandCheck();
final CommandGenerateJava generate = new CommandGenerateJava();

this.commands = new HashMap<>(8);
this.commands.put("check", check);
this.commands.put("generate", generate);

this.commander = new JCommander();
this.commander.setProgramName("jpra-c");
this.commander.addCommand("check", check);
this.commander.addCommand("generate", generate);
}

/**
Expand All @@ -77,36 +94,61 @@ private Main()
* @param args Command line arguments
*/

public static void main(final String[] args)
public static void main(
final String[] args)
{
final Cli.CliBuilder<Runnable> builder = Cli.builder("jpra");
builder.withDescription("Packed record access compiler");
builder.withDefaultCommand(Help.class);
builder.withCommand(Help.class);
builder.withCommand(CommandCheck.class);
builder.withCommand(CommandGenerateJava.class);
final Cli<Runnable> parser = builder.build();
final Main cm = new Main(args);
cm.run();
System.exit(cm.exitCode());
}

private int exitCode()
{
return this.exit_code;
}

private void run()
{
try {
parser.parse(args).run();
} catch (final ParseArgumentsMissingException
| ParseOptionMissingException
| ParseArgumentsUnexpectedException e) {
LOG.error("parse error: {}", e.getMessage());
Help.help(parser.getMetadata(), Collections.emptyList());
this.commander.parse(this.args);

final String cmd = this.commander.getParsedCommand();
if (cmd == null) {
final StringBuilder sb = new StringBuilder(128);
this.commander.usage(sb);
LOG.info("Arguments required.\n{}", sb.toString());
this.exit_code = 1;
return;
}

final CommandType command = this.commands.get(cmd);
final CommandType.Status status = command.run();
this.exit_code = status.exitCode();
} catch (final ParameterException e) {
final StringBuilder sb = new StringBuilder(128);
this.commander.usage(sb);
LOG.error("{}\n{}", e.getMessage(), sb.toString());
this.exit_code = 1;
} catch (final Exception e) {
LOG.error("{}", e.getMessage(), e);
this.exit_code = 1;
}
}

abstract static class CommandType implements Runnable
abstract static class CommandType
{
@Option(name = "--debug", description = "Enable debug logging")
@Parameter(
names = "--debug",
description = "Enable debug logging")
private boolean debug;

CommandType()
{

}

protected abstract Status run();

protected final void setup()
{
final Logger root = (Logger) LoggerFactory.getLogger(
Expand All @@ -115,23 +157,42 @@ protected final void setup()
root.setLevel(Level.TRACE);
}
}

enum Status
{
SUCCESS,
FAILURE;

int exitCode()
{
switch (this) {
case SUCCESS:
return 0;
case FAILURE:
return 1;
}
throw new UnreachableCodeException();
}
}
}

/**
* A check command.
*/

@Command(name = "check", description = "Check a list of packages")
@Parameters(commandDescription = "Check a list of packages")
public static final class CommandCheck extends CommandType
{
@Option(
arity = 1,
@Parameter(
description = "Source directory",
name = "--source-directory",
required = true) private String source_directory;
@Arguments(
description = "Packages to be checked",
required = true) private List<String> packages;
names = "--source-directory",
required = true)
private String source_directory;

@Parameter(
description = "Packages to be checked (may be specified multiple times)",
names = "--package")
private List<String> packages = new ArrayList<>();

/**
* Construct a command.
Expand All @@ -143,7 +204,7 @@ public CommandCheck()
}

@Override
public void run()
public Status run()
{
this.setup();

Expand Down Expand Up @@ -179,32 +240,37 @@ public void run()
}

if (error) {
System.exit(1);
return Status.FAILURE;
}
System.exit(0);
return Status.SUCCESS;
}
}

/**
* A {@code generate-java} command.
*/

@Command(name = "generate-java", description = "Generate Java code")
@Parameters(commandDescription = "Generate Java code")
public static final class CommandGenerateJava extends CommandType
{
@Option(
@Parameter(
arity = 1,
description = "Source directory",
name = "--source-directory",
required = true) private String source_directory;
@Option(
names = "--source-directory",
required = true)
private String source_directory;

@Parameter(
arity = 1,
description = "Target directory",
name = "--target-directory",
required = true) private String target_directory;
@Arguments(
description = "Packages to be exported",
required = true) private List<String> packages;
names = "--target-directory",
required = true)
private String target_directory;

@Parameter(
description = "Packages to be exported (may be specified multiple times)",
names = "--package")
private List<String> packages = new ArrayList<>();

/**
* Construct a command.
Expand All @@ -216,7 +282,7 @@ public CommandGenerateJava()
}

@Override
public void run()
public Status run()
{
this.setup();

Expand Down Expand Up @@ -276,9 +342,9 @@ public void run()

System.err.flush();
if (error) {
System.exit(1);
return Status.FAILURE;
}
System.exit(0);
return Status.SUCCESS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@
<s:formal-item s:kind="specification">
<s:formal-item-title>Options</s:formal-item-title>
<s:verbatim><![CDATA[
usage: jpra <command> [<args>]
Usage: jpra [command] [command options]
Commands:
check Check a list of packages
Usage: check [options]
Options:
--debug
Enable debug logging
Default: false
--package
Packages to be checked (may be specified multiple times)
Default: []
* --source-directory
Source directory
The most commonly used jpra commands are:
check Check a list of packages
generate-java Generate Java code
help Display help information
generate Generate Java code
Usage: generate [options]
Options:
--debug
Enable debug logging
Default: false
--package
Packages to be exported (may be specified multiple times)
Default: []
* --source-directory
Source directory
* --target-directory
Target directory
See 'jpra help <command>' for more information on a specific command.
]]></s:verbatim>
</s:formal-item>
<s:paragraph>
Expand All @@ -35,7 +55,7 @@ See 'jpra help <command>' for more information on a specific command.
<s:formal-item s:kind="example">
<s:formal-item-title>Checking sources</s:formal-item-title>
<s:verbatim><![CDATA[
$ java -jar jpra.jar check --source-directory p com.io7m.example
$ java -jar jpra.jar check --source-directory p --package com.io7m.example
]]></s:verbatim>
</s:formal-item>
<s:paragraph>
Expand All @@ -59,7 +79,7 @@ $ java -jar jpra.jar check --source-directory p com.io7m.example
<s:formal-item s:kind="example">
<s:formal-item-title>Checking sources</s:formal-item-title>
<s:verbatim><![CDATA[
$ java -jar jpra.jar generate-java --source-directory p --target-directory t com.io7m.example
$ java -jar jpra.jar generate-java --source-directory p --target-directory t --package com.io7m.example
]]></s:verbatim>
</s:formal-item>
<s:paragraph>
Expand Down
Loading

0 comments on commit 35a3603

Please sign in to comment.