Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.beust.jcommander.JCommander;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -2424,6 +2426,43 @@ public void customCommands() throws Exception {

}

@Test
public void testHelpFlag() {
PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);

{
CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
cmdSchemas.run(split("-h"));
assertTrue(cmdSchemas.isHelp());
}

{
CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
cmdSchemas.run(split("--help"));
assertTrue(cmdSchemas.isHelp());
}

{
CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
cmdSchemas.run(split("delete --help"));
assertFalse(cmdSchemas.isHelp());
JCommander commander = cmdSchemas.getJcommander();
JCommander subCommander = commander.getCommands().get("delete");
CliCommand subcommand = (CliCommand) subCommander.getObjects().get(0);
assertTrue(subcommand.isHelp());
}

{
CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
cmdSchemas.run(split("delete -h"));
assertFalse(cmdSchemas.isHelp());
JCommander commander = cmdSchemas.getJcommander();
JCommander subCommander = commander.getCommands().get("delete");
CliCommand subcommand = (CliCommand) subCommander.getObjects().get(0);
assertTrue(subcommand.isHelp());
}
}

private static String runCustomCommand(String[] args) throws Exception {
File narFile = new File(PulsarAdminTool.class.getClassLoader()
.getResource("cliextensions/customCommands-nar.nar").getFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.admin.cli;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
Expand All @@ -38,6 +39,13 @@

public abstract class CliCommand {

@Parameter(names = { "--help", "-h" }, help = true, hidden = true)
private boolean help = false;

public boolean isHelp() {
return help;
}

static String[] validatePropertyCluster(List<String> params) {
return splitParameter(params, 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ public abstract class CmdBase {
private PulsarAdmin admin;
private IUsageFormatter usageFormatter;

@Parameter(names = { "-h", "--help" }, help = true, hidden = true)
private boolean help;
@Parameter(names = { "--help", "-h" }, help = true, hidden = true)
private boolean help = false;
Comment thread
tisonkun marked this conversation as resolved.

public boolean isHelp() {
return help;
}

public CmdBase(String cmdName, Supplier<PulsarAdmin> adminSupplier) {
this.adminSupplier = adminSupplier;
jcommander = new JCommander();
jcommander = new JCommander(this);
usageFormatter = new CmdUsageFormatter(jcommander);
jcommander.setProgramName("pulsar-admin " + cmdName);
jcommander.setUsageFormatter(usageFormatter);
Expand Down Expand Up @@ -78,32 +82,38 @@ public boolean run(String[] args) {
String cmd = jcommander.getParsedCommand();
if (cmd == null) {
jcommander.usage();
return help;
}

JCommander obj = jcommander.getCommands().get(cmd);
CliCommand cmdObj = (CliCommand) obj.getObjects().get(0);

if (cmdObj.isHelp()) {
obj.setProgramName(jcommander.getProgramName() + " " + cmd);
obj.usage();
return true;
}

try {
cmdObj.run();
return true;
} catch (ParameterException e) {
System.err.println(e.getMessage());
System.err.println();
return false;
} catch (ConnectException e) {
System.err.println(e.getMessage());
System.err.println();
System.err.println("Error connecting to: " + getAdmin().getServiceUrl());
return false;
} catch (PulsarAdminException e) {
System.err.println(e.getHttpError());
System.err.println();
System.err.println("Reason: " + e.getMessage());
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
} else {
JCommander obj = jcommander.getCommands().get(cmd);
CliCommand cmdObj = (CliCommand) obj.getObjects().get(0);

try {
cmdObj.run();
return true;
} catch (ParameterException e) {
System.err.println(e.getMessage());
System.err.println();
return false;
} catch (ConnectException e) {
System.err.println(e.getMessage());
System.err.println();
System.err.println("Error connecting to: " + getAdmin().getServiceUrl());
return false;
} catch (PulsarAdminException e) {
System.err.println(e.getHttpError());
System.err.println();
System.err.println("Reason: " + e.getMessage());
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

Expand Down