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

Commit

Permalink
Expose read-only access to commands
Browse files Browse the repository at this point in the history
This introduces a ClaypotType interface for easier mocking, and
adds to it a method that returns a read-only map of the available
registered commands.

Fix: #4
  • Loading branch information
io7m committed Jul 4, 2020
1 parent aaf0232 commit 415b5d8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
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 @@ -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());
}
}

0 comments on commit 415b5d8

Please sign in to comment.