1919
2020package org .elasticsearch .cli ;
2121
22+ import joptsimple .ArgumentAcceptingOptionSpec ;
2223import joptsimple .OptionSet ;
24+ import joptsimple .util .KeyValuePair ;
2325import org .junit .Before ;
2426
2527import java .io .IOException ;
28+ import java .util .List ;
2629import java .util .concurrent .atomic .AtomicBoolean ;
2730
31+ import static org .hamcrest .Matchers .containsString ;
32+
2833public class MultiCommandTests extends CommandTestCase {
2934
3035 static class DummyMultiCommand extends MultiCommand {
3136
3237 final AtomicBoolean closed = new AtomicBoolean ();
3338
3439 DummyMultiCommand () {
35- super ("A dummy multi command" , () -> {
36- });
40+ super ("A dummy multi command" , () -> {});
3741 }
3842
3943 @ Override
@@ -75,7 +79,23 @@ public void close() throws IOException {
7579 }
7680 }
7781
78- DummyMultiCommand multiCommand ;
82+ static class DummySettingsSubCommand extends DummySubCommand {
83+ private final ArgumentAcceptingOptionSpec <KeyValuePair > settingOption ;
84+
85+ DummySettingsSubCommand () {
86+ super ();
87+ this .settingOption = parser .accepts ("E" , "Configure a setting" ).withRequiredArg ().ofType (KeyValuePair .class );
88+ }
89+
90+ @ Override
91+ protected void execute (Terminal terminal , OptionSet options ) throws Exception {
92+ final List <KeyValuePair > values = this .settingOption .values (options );
93+ terminal .println ("Settings: " + values );
94+ super .execute (terminal , options );
95+ }
96+ }
97+
98+ private DummyMultiCommand multiCommand ;
7999
80100 @ Before
81101 public void setupCommand () {
@@ -87,27 +107,21 @@ protected Command newCommand() {
87107 return multiCommand ;
88108 }
89109
90- public void testNoCommandsConfigured () throws Exception {
91- IllegalStateException e = expectThrows (IllegalStateException .class , () -> {
92- execute ();
93- });
110+ public void testNoCommandsConfigured () {
111+ IllegalStateException e = expectThrows (IllegalStateException .class , this ::execute );
94112 assertEquals ("No subcommands configured" , e .getMessage ());
95113 }
96114
97- public void testUnknownCommand () throws Exception {
115+ public void testUnknownCommand () {
98116 multiCommand .subcommands .put ("something" , new DummySubCommand ());
99- UserException e = expectThrows (UserException .class , () -> {
100- execute ("somethingelse" );
101- });
117+ UserException e = expectThrows (UserException .class , () -> execute ("somethingelse" ));
102118 assertEquals (ExitCodes .USAGE , e .exitCode );
103119 assertEquals ("Unknown command [somethingelse]" , e .getMessage ());
104120 }
105121
106- public void testMissingCommand () throws Exception {
122+ public void testMissingCommand () {
107123 multiCommand .subcommands .put ("command1" , new DummySubCommand ());
108- UserException e = expectThrows (UserException .class , () -> {
109- execute ();
110- });
124+ UserException e = expectThrows (UserException .class , this ::execute );
111125 assertEquals (ExitCodes .USAGE , e .exitCode );
112126 assertEquals ("Missing command" , e .getMessage ());
113127 }
@@ -121,6 +135,19 @@ public void testHelp() throws Exception {
121135 assertTrue (output , output .contains ("command2" ));
122136 }
123137
138+ /**
139+ * Check that if -E arguments are passed to the main command, then they are accepted
140+ * and passed on to the subcommand.
141+ */
142+ public void testSettingsOnMainCommand () throws Exception {
143+ multiCommand .subcommands .put ("command1" , new DummySettingsSubCommand ());
144+ execute ("-Esetting1=value1" , "-Esetting2=value2" , "command1" , "otherArg" );
145+
146+ String output = terminal .getOutput ();
147+ assertThat (output , containsString ("Settings: [setting1=value1, setting2=value2]" ));
148+ assertThat (output , containsString ("Arguments: [otherArg]" ));
149+ }
150+
124151 public void testSubcommandHelp () throws Exception {
125152 multiCommand .subcommands .put ("command1" , new DummySubCommand ());
126153 multiCommand .subcommands .put ("command2" , new DummySubCommand ());
0 commit comments