Single entry point for NBCLI parser

This commit is contained in:
Jonathan Shook
2024-01-10 14:41:15 -06:00
parent ceadc24399
commit 033e5d2284
7 changed files with 63 additions and 61 deletions

View File

@@ -216,7 +216,7 @@ public class NBCLI implements Function<String[], Integer>, NBLabeledElement {
annotatorsConfig = gson.toJson(annotatorsConfigs);
}
final NBCLIOptions options = new NBCLIOptions(args);
final NBCLIOptions options = new NBCLIOptions(args,Mode.ParseAllOptions);
NBCLI.logger = LogManager.getLogger("NBCLI");
NBIO.addGlobalIncludes(options.wantsIncludes());

View File

@@ -279,10 +279,6 @@ public class NBCLIOptions {
ParseAllOptions
}
public NBCLIOptions(final String[] args) {
this(args, Mode.ParseAllOptions);
}
public NBCLIOptions(final String[] args, final Mode mode) {
switch (mode) {
case ParseGlobalsOnly:
@@ -306,8 +302,8 @@ public class NBCLIOptions {
// Process --include and --statedir, separately first
// regardless of position
LinkedList<String> nonincludes = new LinkedList<>();
while (null != arglist.peekFirst()) {
LinkedList<String> everythingButIncludes = new LinkedList<>();
while (!arglist.isEmpty()) {
final String word = arglist.peekFirst();
if (word.startsWith("--") && word.contains("=")) {
final String wordToSplit = arglist.removeFirst();
@@ -328,13 +324,13 @@ public class NBCLIOptions {
this.wantsToIncludePaths.add(include);
break;
default:
nonincludes.addLast(arglist.removeFirst());
everythingButIncludes.addLast(arglist.removeFirst());
}
}
this.statepath = NBStatePath.initialize(statedirs);
arglist = nonincludes;
nonincludes = new LinkedList<>();
arglist = everythingButIncludes;
everythingButIncludes = new LinkedList<>();
// Now that statdirs is settled, auto load argsfile if it is present
final NBCLIArgsFile argsfile = new NBCLIArgsFile();
@@ -507,11 +503,11 @@ public class NBCLIOptions {
this.metricsLabelSpec = this.readWordOrThrow(arglist, "labels validator specification for metric labels");
break;
default:
nonincludes.addLast(arglist.removeFirst());
everythingButIncludes.addLast(arglist.removeFirst());
}
}
return nonincludes;
return everythingButIncludes;
}
private void setLabels(String labeldata) {
@@ -674,10 +670,16 @@ public class NBCLIOptions {
You can discover available ways to invoke PROG by using the various --list-* commands:
[ --list-commands, --list-scripts, --list-workloads (and --list-scenarios), --list-apps ]
After parsing all global options out of the command line, the remaining commands were found,
and mapped to valid options as describe above. This command stream was:
COMMANDSTREAM
"""
.replaceAll("ARG", cmdParam)
.replaceAll("PROG", "nb5")
.replaceAll("INCLUDES", String.join(",", wantsIncludes()));
.replaceAll("INCLUDES", String.join(",", wantsIncludes()))
.replaceAll("COMMANDSTREAM",
String.join(" ",arglist));
throw new BasicError(helpmsg);
}

View File

@@ -28,7 +28,7 @@ public class BasicScriptBufferTest {
@Test
public void testScriptInterpolation() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=script_to_interpolate", "parameter1=replaced"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=script_to_interpolate", "parameter1=replaced"}, NBCLIOptions.Mode.ParseAllOptions);
BasicScriptBuffer b = new BasicScriptBuffer();
b.add(opts.getCommands().toArray(new Cmd[0]));
@@ -40,7 +40,7 @@ public class BasicScriptBufferTest {
@Test
public void testAutoScriptCommand() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "script","path=acommand" });
NBCLIOptions opts = new NBCLIOptions(new String[]{ "script","path=acommand" }, NBCLIOptions.Mode.ParseAllOptions);
BasicScriptBuffer b = new BasicScriptBuffer();
b.add(opts.getCommands().toArray(new Cmd[0]));
String s = b.getParsedScript();
@@ -53,8 +53,8 @@ public class BasicScriptBufferTest {
NBCLIOptions opts = new NBCLIOptions(new String[] {
"script",
"path=testscripts/printscript.js",
"param1=value1"
});
"param1=value1",
}, NBCLIOptions.Mode.ParseAllOptions);
BasicScriptBuffer b = new BasicScriptBuffer();
b.add(opts.getCommands().toArray(new Cmd[0]));
String script = b.getParsedScript();
@@ -72,7 +72,7 @@ public class BasicScriptBufferTest {
"path=testscripts/printparam.js",
"paramname=another",
"param2=andanother"
});
}, NBCLIOptions.Mode.ParseAllOptions);
BasicScriptBuffer b = new BasicScriptBuffer();
b.add(opts.getCommands().toArray(new Cmd[0]));
String script = b.getParsedScript();
@@ -84,6 +84,6 @@ public class BasicScriptBufferTest {
@Test
public void shouldThrowErrorForInvalidWaitMillisOperand() {
assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{ "waitmillis", "ms=noway" }));
.isThrownBy(() -> new NBCLIOptions(new String[]{ "waitmillis", "ms=noway" }, NBCLIOptions.Mode.ParseAllOptions));
}
}

View File

@@ -30,7 +30,7 @@ public class NBCLIScenarioPreprocessorTemplateVarTest {
@Test
public void testMultipleOccurencesOfSameTemplateVar() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "local/example_scenarios_templatevars" });
NBCLIOptions opts = new NBCLIOptions(new String[]{ "local/example_scenarios_templatevars" }, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
cmds.forEach(System.out::println);
@@ -47,7 +47,7 @@ public class NBCLIScenarioPreprocessorTemplateVarTest {
@Test
public void testThatCLIOverridesWorkForTemplateVars() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "local/example_scenarios_templatevars", "tvar1=overridden" });
NBCLIOptions opts = new NBCLIOptions(new String[]{ "local/example_scenarios_templatevars", "tvar1=overridden" }, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
cmds.forEach(System.out::println);
@@ -59,7 +59,7 @@ public class NBCLIScenarioPreprocessorTemplateVarTest {
@Test
public void testThatAdditionalCLIParamIsAdded() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"local/example_scenarios_templatevars", "tvar3=tval3"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"local/example_scenarios_templatevars", "tvar3=tval3"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
cmds.forEach(System.out::println);
assertThat(cmds).hasSize(2);

View File

@@ -33,39 +33,39 @@ public class NBCLIScenarioPreprocessorTest {
@Test
public void providePathForScenario() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"local/example_scenarios"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"local/example_scenarios"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
}
@Test
public void defaultScenario() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
}
@Test
public void defaultScenarioWithParams() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "cycles=100"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "cycles=100"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.get(0).getArgValue("cycles")).isEqualTo("100");
}
@Test
public void namedScenario() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
}
@Test
public void namedScenarioWithParams() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles=100"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles=100"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.get(0).getArgValue("cycles")).containsOnlyOnce("100");
}
@Test
public void testThatSilentFinalParametersPersist() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "type=foo"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "type=foo"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.get(0).getArgValue("driver")).isEqualTo("stdout");
}
@@ -73,25 +73,25 @@ public class NBCLIScenarioPreprocessorTest {
@Test
public void testThatVerboseFinalParameterThrowsError() {
assertThatExceptionOfType(BasicError.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{"scenario_test", "workload=canttouchthis"}));
.isThrownBy(() -> new NBCLIOptions(new String[]{"scenario_test", "workload=canttouchthis"}, NBCLIOptions.Mode.ParseAllOptions));
}
@Test
public void testThatMissingScenarioNameThrowsError() {
assertThatExceptionOfType(BasicError.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{"scenario_test", "missing_scenario"}));
.isThrownBy(() -> new NBCLIOptions(new String[]{"scenario_test", "missing_scenario"}, NBCLIOptions.Mode.ParseAllOptions));
}
@Test
public void testThatMultipleScenariosConcatenate() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "default", "default"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "default", "default"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isEqualTo(6);
}
@Test
public void testThatTemplatesAreExpandedDefault() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "template_test"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "template_test"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isEqualTo(1);
assertThat(cmds.get(0).getArgValue("driver")).isEqualTo("stdout");
@@ -101,7 +101,7 @@ public class NBCLIScenarioPreprocessorTest {
@Test
public void testThatTemplateParamsAreExpandedAndNotRemovedOverride() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "template_test", "cycles-test=20"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "template_test", "cycles-test=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isEqualTo(1);
assertThat(cmds.get(0).getArgMap()).isEqualTo(Map.of(
@@ -119,7 +119,7 @@ public class NBCLIScenarioPreprocessorTest {
@Test
public void testThatUndefValuesAreUndefined() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles-test=20"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles-test=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isEqualTo(1);
assertThat(cmds.get(0).getArgMap()).isEqualTo(Map.of(
@@ -133,7 +133,7 @@ public class NBCLIScenarioPreprocessorTest {
"tags", "block:\"schema.*\"",
"workload", "scenario_test"
));
NBCLIOptions opts1 = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "doundef=20"});
NBCLIOptions opts1 = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "doundef=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds1 = opts1.getCommands();
assertThat(cmds1.size()).isEqualTo(1);
assertThat(cmds1.get(0).getArgValueOrNull("cycles-test")).isNull();
@@ -150,7 +150,7 @@ public class NBCLIScenarioPreprocessorTest {
Path absolute = rel.toAbsolutePath();
assertThat(absolute).exists();
NBCLIOptions opts = new NBCLIOptions(new String[]{absolute.toString(), "schema_only", "cycles-test=20"});
NBCLIOptions opts = new NBCLIOptions(new String[]{absolute.toString(), "schema_only", "cycles-test=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isGreaterThan(0);
}
@@ -161,7 +161,7 @@ public class NBCLIScenarioPreprocessorTest {
//TODO: This might change?
String urlScenario = "https://raw.githubusercontent.com/nosqlbench/nosqlbench/main/engine-cli/src/test/resources/activities/scenario_test.yaml";
NBCLIOptions opts = new NBCLIOptions(new String[]{urlScenario, "schema_only", "cycles-test=20"});
NBCLIOptions opts = new NBCLIOptions(new String[]{urlScenario, "schema_only", "cycles-test=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isGreaterThan(0);
}
@@ -174,7 +174,7 @@ public class NBCLIScenarioPreprocessorTest {
@Test
public void testSubStepSelection() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles-test=20"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"scenario_test", "schema_only", "cycles-test=20"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds = opts.getCommands();
assertThat(cmds.size()).isEqualTo(1);
assertThat(cmds.get(0).getArgMap()).isEqualTo(Map.of(
@@ -188,7 +188,7 @@ public class NBCLIScenarioPreprocessorTest {
"tags", "block:\"schema.*\"",
"workload", "scenario_test"
));
NBCLIOptions opts1 = new NBCLIOptions(new String[]{"local/example_scenarios", "namedsteps.one", "testparam1=testvalue2"});
NBCLIOptions opts1 = new NBCLIOptions(new String[]{"local/example_scenarios", "namedsteps.one", "testparam1=testvalue2"}, NBCLIOptions.Mode.ParseAllOptions);
List<Cmd> cmds1 = opts1.getCommands();
assertThat(cmds1.size()).isEqualTo(1);
assertThat(cmds1.get(0).getArgValueOrNull("cycles_test")).isNull();

View File

@@ -36,7 +36,7 @@ public class TestNBCLIOptions {
@Test
public void shouldRecognizeActivities() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"start", "foo=wan", "start", "bar=lan"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"start", "foo=wan", "start", "bar=lan"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.getCommands()).isNotNull();
assertThat(opts.getCommands().size()).isEqualTo(2);
// assertThat(opts.getCommands().get(0).getArgs()).containsEntry("foo","wan");
@@ -46,7 +46,7 @@ public class TestNBCLIOptions {
@Test
public void shouldParseLongActivityForm() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"start", "param1=param2", "param3=param4",
"--report-graphite-to", "woot", "--report-interval", "23"});
"--report-graphite-to", "woot", "--report-interval", "23"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.getCommands().size()).isEqualTo(1);
// assertThat(opts.getCommands().get(0).getArgs()).containsEntry("param1","param2");
// assertThat(opts.getCommands().get(0).getArgs()).containsEntry("param3","param4");
@@ -56,21 +56,21 @@ public class TestNBCLIOptions {
@Test
public void shouldRecognizeShortVersion() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"--version"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"--version"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.isWantsVersionShort()).isTrue();
assertThat(opts.wantsVersionCoords()).isFalse();
}
@Test
public void shouldRecognizeVersion() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"--version-coords"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"--version-coords"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.isWantsVersionShort()).isFalse();
assertThat(opts.wantsVersionCoords()).isTrue();
}
@Test
public void shouldRecognizeScripts() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=ascriptaone", "script", "path=ascriptatwo"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=ascriptaone", "script", "path=ascriptatwo"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.getCommands()).isNotNull();
assertThat(opts.getCommands().size()).isEqualTo(2);
assertThat(opts.getCommands().get(0).getCmdType()).isEqualTo(CmdType.script);
@@ -81,41 +81,41 @@ public class TestNBCLIOptions {
@Test
public void shouldRecognizeWantsActivityTypes() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"--list-activity-types"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"--list-activity-types"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsActivityTypes()).isTrue();
opts = new NBCLIOptions(new String[]{"--version"});
opts = new NBCLIOptions(new String[]{"--version"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsActivityTypes()).isFalse();
opts = new NBCLIOptions(new String[]{"--list-drivers"});
opts = new NBCLIOptions(new String[]{"--list-drivers"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsActivityTypes()).isTrue();
}
@Test
public void shouldRecognizeWantsBasicHelp() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"--help"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"--help"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsBasicHelp()).isTrue();
opts = new NBCLIOptions(new String[]{"--version"});
opts = new NBCLIOptions(new String[]{"--version"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsTopicalHelp()).isFalse();
}
@Test
public void shouldRecognizeWantsActivityHelp() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"--help", "foo"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"--help", "foo"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsTopicalHelp()).isTrue();
assertThat(opts.wantsTopicalHelpFor()).isEqualTo("foo");
opts = new NBCLIOptions(new String[]{"--version"});
opts = new NBCLIOptions(new String[]{"--version"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsTopicalHelp()).isFalse();
}
@Test
public void shouldErrorSanelyWhenNoMatch() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{"unrecognizable command"}));
.isThrownBy(() -> new NBCLIOptions(new String[]{"unrecognizable command"}, NBCLIOptions.Mode.ParseAllOptions));
}
@Test
public void testShouldRecognizeScriptParams() {
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=ascript", "param1=value1"});
NBCLIOptions opts = new NBCLIOptions(new String[]{"script", "path=ascript", "param1=value1"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.getCommands().size()).isEqualTo(1);
Cmd cmd = opts.getCommands().get(0);
assertThat(cmd.getArgs().size()).isEqualTo(2);
@@ -127,14 +127,14 @@ public class TestNBCLIOptions {
@Test
public void testShouldErrorSanelyWhenScriptNameSkipped() {
assertThatExceptionOfType(InvalidParameterException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{"script", "param1=value1"}));
.isThrownBy(() -> new NBCLIOptions(new String[]{"script", "param1=value1"}, NBCLIOptions.Mode.ParseAllOptions));
}
@Disabled("semantic parsing is deferred until later")
@Test
public void testShouldErrorForMissingScriptName() {
assertThatExceptionOfType(InvalidParameterException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{"script"}));
.isThrownBy(() -> new NBCLIOptions(new String[]{"script"}, NBCLIOptions.Mode.ParseAllOptions));
}
// @Test
@@ -169,7 +169,7 @@ public class TestNBCLIOptions {
@Test
public void shouldThrowErrorForInvalidStopActivity() {
assertThatExceptionOfType(InvalidParameterException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{ "stop", "woah=woah" }));
.isThrownBy(() -> new NBCLIOptions(new String[]{ "stop", "woah=woah" }, NBCLIOptions.Mode.ParseAllOptions));
}
// @Test
@@ -185,7 +185,7 @@ public class TestNBCLIOptions {
@Test
public void shouldThrowErrorForInvalidAwaitActivity() {
assertThatExceptionOfType(InvalidParameterException.class)
.isThrownBy(() -> new NBCLIOptions(new String[]{ "await", "awaitme=notvalid" }));
.isThrownBy(() -> new NBCLIOptions(new String[]{ "await", "awaitme=notvalid" }, NBCLIOptions.Mode.ParseAllOptions));
}
// @Test
@@ -199,19 +199,19 @@ public class TestNBCLIOptions {
@Test
public void listWorkloads() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-workloads"});
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-workloads"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsWorkloadsList()).isTrue();
}
@Test
public void listScenarios() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-scenarios"});
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-scenarios"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsScenariosList()).isTrue();
}
@Test
public void listScripts() {
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-scripts"});
NBCLIOptions opts = new NBCLIOptions(new String[]{ "--list-scripts"}, NBCLIOptions.Mode.ParseAllOptions);
assertThat(opts.wantsListScripts()).isTrue();
}