Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/0.0.3'
Browse files Browse the repository at this point in the history
Release: com.io7m.claypot 0.0.3
Change: Introduce commands() method to return map of commands (Ticket: #4)
Change: (Backwards incompatible) Introduce ClaypotType interface
Change: Allow applications to publish documentation URIs. (Ticket: #2)
  • Loading branch information
io7m committed Jul 4, 2020
2 parents 6509b03 + b4cc222 commit caf3429
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 26 deletions.
14 changes: 12 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -4,7 +4,7 @@
<c:release date="2020-06-26T00:00:00+00:00" ticket-system="com.github.io7m.claypot" version="0.0.1">
<c:changes/>
</c:release>
<c:release date="2020-06-27T20:07:38+00:00" ticket-system="com.github.io7m.claypot" version="0.0.2">
<c:release date="2020-07-04T00:00:00+00:00" ticket-system="com.github.io7m.claypot" version="0.0.2">
<c:changes>
<c:change date="2020-06-27T00:00:00+00:00" summary="Add extended help methods.">
<c:tickets>
Expand All @@ -16,11 +16,21 @@
<c:ticket id="3"/>
</c:tickets>
</c:change>
<c:change date="2020-06-27T20:07:38+00:00" summary="Allow applications to publish documentation URIs.">
</c:changes>
</c:release>
<c:release date="2020-07-04T15:10:45+00:00" ticket-system="com.github.io7m.claypot" version="0.0.3">
<c:changes>
<c:change date="2020-06-27T00:00:00+00:00" summary="Allow applications to publish documentation URIs.">
<c:tickets>
<c:ticket id="2"/>
</c:tickets>
</c:change>
<c:change compatible="false" date="2020-07-04T00:00:00+00:00" summary="Introduce ClaypotType interface"/>
<c:change date="2020-07-04T00:00:00+00:00" summary="Introduce commands() method to return map of commands">
<c:tickets>
<c:ticket id="4"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.claypot.core/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.claypot</artifactId>
<groupId>com.io7m.claypot</groupId>
<version>0.0.2</version>
<version>0.0.3</version>
</parent>

<artifactId>com.io7m.claypot.core</artifactId>
Expand Down
Expand Up @@ -23,25 +23,27 @@
import com.io7m.claypot.core.internal.CLPCommandRoot;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Collections;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

/**
* The main wrapper over {@link JCommander}.
*/

public final class Claypot
public final class Claypot implements ClaypotType
{
private final CLPApplicationConfiguration configuration;
private final JCommander commander;
private final HashMap<String, CLPCommandType> commandMap;
private final TreeMap<String, CLPCommandType> commandMap;
private final CLPStringsType strings;
private int exitCode;

private Claypot(
final CLPApplicationConfiguration inConfiguration,
final JCommander inCommander,
final HashMap<String, CLPCommandType> inCommandMap,
final TreeMap<String, CLPCommandType> inCommandMap,
final CLPStringsType inStrings)
{
this.configuration =
Expand All @@ -62,7 +64,7 @@ private Claypot(
* @return A new wrapper
*/

public static Claypot create(
public static ClaypotType create(
final CLPApplicationConfiguration configuration)
{
final var strings = CLPStrings.create();
Expand All @@ -75,7 +77,7 @@ public static Claypot create(
final var constructors =
configuration.commands();
final var commandMap =
new HashMap<String, CLPCommandType>(constructors.size() + 1);
new TreeMap<String, CLPCommandType>();

final var help = new CLPCommandHelp(context);
commandMap.put(help.name(), help);
Expand All @@ -98,21 +100,13 @@ public static Claypot create(
return new Claypot(configuration, commander, commandMap, strings);
}

/**
* @return The exit code resulting from the most recent {@link #execute(String[])}
*/

@Override
public int exitCode()
{
return this.exitCode;
}

/**
* Execute the wrapper for the given command-line arguments.
*
* @param args The command-line arguments
*/

@Override
public void execute(
final String[] args)
{
Expand Down Expand Up @@ -147,6 +141,12 @@ public void execute(
}
}

@Override
public SortedMap<String, CLPCommandType> commands()
{
return Collections.unmodifiableSortedMap(this.commandMap);
}

private void logExceptionFriendly(
final Logger logger,
final boolean isCause,
Expand Down
@@ -0,0 +1,48 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.claypot.core;

import com.beust.jcommander.JCommander;

import java.util.SortedMap;

/**
* The main wrapper over {@link JCommander}.
*/

public interface ClaypotType
{
/**
* @return The exit code resulting from the most recent {@link #execute(String[])}
*/

int exitCode();

/**
* Execute the wrapper for the given command-line arguments.
*
* @param args The command-line arguments
*/

void execute(String[] args);

/**
* @return The available commands, by name
*/

SortedMap<String, CLPCommandType> commands();
}
Expand Up @@ -16,7 +16,6 @@

package com.io7m.claypot.core.internal;

import com.beust.jcommander.DefaultUsageFormatter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.io7m.claypot.core.CLPAbstractCommand;
Expand Down Expand Up @@ -66,7 +65,7 @@ protected Status executeActual()
if (this.commandNames.isEmpty()) {
final var console = new CLPStringBuilderConsole();
final var commander = this.commander();
commander.setUsageFormatter(new DefaultUsageFormatter(commander));
commander.setUsageFormatter(new CLPLongUsageFormatter(commander));
commander.setConsole(console);
commander.usage();
logger.info("{}", console.builder().toString());
Expand All @@ -84,7 +83,7 @@ protected Status executeActual()
}

final var console = new CLPStringBuilderConsole();
subCommander.setUsageFormatter(new DefaultUsageFormatter(subCommander));
subCommander.setUsageFormatter(new CLPLongUsageFormatter(subCommander));
subCommander.setConsole(console);
subCommander.usage();

Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.claypot.core.internal;

import com.beust.jcommander.DefaultUsageFormatter;
import com.beust.jcommander.JCommander;

public final class CLPLongUsageFormatter extends DefaultUsageFormatter
{
public CLPLongUsageFormatter(
final JCommander commander)
{
super(commander);
}

@Override
public void appendMainLine(
final StringBuilder out,
final boolean hasOptions,
final boolean hasCommands,
final int indentCount,
final String indent)
{
super.appendMainLine(out, hasOptions, hasCommands, indentCount, indent);
out.append('\n');
}

@Override
public void appendCommands(
final StringBuilder out,
final int indentCount,
final int descriptionIndent,
final String indent)
{
out.append('\n');
super.appendCommands(out, indentCount, descriptionIndent, indent);
}
}
Expand Up @@ -33,5 +33,6 @@ right now.
Example: {0} help help
]]></entry>

<entry key="com.io7m.claypot.documentation">Documentation: {0}</entry>
<entry key="com.io7m.claypot.documentation"><![CDATA[Documentation:
{0}]]></entry>
</properties>
2 changes: 1 addition & 1 deletion com.io7m.claypot.example/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.claypot</artifactId>
<groupId>com.io7m.claypot</groupId>
<version>0.0.2</version>
<version>0.0.3</version>
</parent>

<artifactId>com.io7m.claypot.example</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.claypot.tests/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.claypot</artifactId>
<groupId>com.io7m.claypot</groupId>
<version>0.0.2</version>
<version>0.0.3</version>
</parent>

<artifactId>com.io7m.claypot.tests</artifactId>
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.io7m.claypot.tests;

import com.io7m.claypot.core.CLPApplicationConfiguration;
import com.io7m.claypot.core.CLPCommandType;
import com.io7m.claypot.core.Claypot;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -26,6 +27,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.SortedMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.AdditionalAnswers.delegatesTo;
Expand Down Expand Up @@ -231,4 +235,23 @@ public void commandDuplicate()
Claypot.create(applicationConfiguration);
});
}

@Test
public void commandNames()
{
final var applicationConfiguration =
CLPApplicationConfiguration.builder()
.setProgramName("cex")
.setLogger(this.spyLog)
.addCommands(CrashCommand::new)
.addCommands(EmptyCommand::new)
.build();

final var claypot = Claypot.create(applicationConfiguration);

final Map<String, CLPCommandType> commands = claypot.commands();
assertEquals(3, commands.size());
assertEquals(CrashCommand.class, commands.get("crash").getClass());
assertEquals(EmptyCommand.class, commands.get("empty").getClass());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -12,7 +12,7 @@

<groupId>com.io7m.claypot</groupId>
<artifactId>com.io7m.claypot</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<packaging>pom</packaging>

<description>JCommander conventions for io7m projects</description>
Expand Down

0 comments on commit caf3429

Please sign in to comment.