Skip to content

Commit

Permalink
feat: Added check unknwon parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Mar 5, 2024
1 parent 32eb837 commit b6b04cc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.file.Files;
import java.util.stream.Collectors;

import org.jline.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -62,7 +61,7 @@ public static void main(String... args) throws Exception {
public static int execute(String... args) throws Exception {
var argument = new CliArgument();
var commander = JCommander.newBuilder() //
.programName(Main.class.getName()) //
.programName("tgsql") //$NON-NLS-1$
.addObject(argument) //
.build();
try (Closeable c0 = () -> ReplJLineTerminal.close()) {
Expand Down Expand Up @@ -105,15 +104,6 @@ public static int execute(String... args) throws Exception {
LOG.error(e.getMessage());
}

String command = commander.getParsedCommand();
if (command != null) {
var c = commander.getCommands().get(command);
if (c != null) {
c.usage();
return 1;
}
}

commander.usage();
return 1;
}
Expand Down Expand Up @@ -159,7 +149,7 @@ private static String getVersion(String title, StringSupplier supplier) {
try {
return supplier.get();
} catch (Exception e) {
Log.debug(title + " get error", e);
LOG.debug(title + " get error", e);
return "unknown";
}
}
Expand All @@ -169,12 +159,14 @@ private static String getServiceMessageVersion() {
var classList = ServiceClientCollector.collect(false);
return classList.stream().flatMap(c -> ServiceClientCollector.findServiceMessageVersion(c).stream()).collect(Collectors.joining(", "));
} catch (Exception e) {
Log.debug("getServiceMessageVersion error", e);
LOG.debug("getServiceMessageVersion error", e);
return "N/A";
}
}

private static void executeConsole(JCommander commander, CliArgument argument) throws Exception {
argument.checkUnknownParameter();

ReplCvKey.registerKey();

var builder = new ConsoleConfigBuilder(argument);
Expand All @@ -195,6 +187,7 @@ private static void executeExec(JCommander commander, CliArgument argument) thro
var builder = new ExecConfigBuilder(argument);
var config = builder.build();
var statement = builder.getStatement();
LOG.debug("exec.statement=[{}]", statement);

try (var reader = new StringReader(statement)) {
ScriptRunner.execute(() -> reader, config);
Expand All @@ -205,7 +198,9 @@ private static void executeScript(JCommander commander, CliArgument argument) th
var builder = new ScriptConfigBuilder(argument);
var config = builder.build();
var script = builder.getScript();
LOG.debug("script.script=[{}]", script);
var encoding = builder.getEncoding();
LOG.debug("script.encoding=[{}]", encoding);

try (var reader = Files.newBufferedReader(script, encoding)) {
ScriptRunner.execute(() -> reader, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -390,6 +391,18 @@ private List<String> normalizeList(List<String> list) {
return list.stream().map(String::trim).collect(Collectors.toList());
}

/**
* check unknown parameter.
*
* @throws ParameterException invalid parameter
*/
public void checkUnknownParameter() throws ParameterException {
if (this.otherList == null || otherList.isEmpty()) {
return; // OK
}
throw new ParameterException(MessageFormat.format("invalid parameter {0}", otherList));
}

/**
* --execute.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class ExplainConvertRunner {
* @throws Exception if exception was occurred
*/
public static void execute(CliArgument argument) throws Exception {
argument.checkUnknownParameter();

var builder = new ExplainConfigBuilder(argument);
var config = builder.build();
new ExplainConvertRunner(argument, config).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ void readAreaExclude() {
}
}

@Test
void checkUnknownParameter() {
{
var argument = new CliArgument();
argument.checkUnknownParameter();
}
{
var argument = new CliArgument();
setList(argument, "otherList", "--zzz", "hoge");
var e = assertThrows(ParameterException.class, () -> argument.checkUnknownParameter());
assertEquals("invalid parameter [--zzz, hoge]", e.getMessage());
}
}

@Test
void execute() {
{
Expand Down

0 comments on commit b6b04cc

Please sign in to comment.