Skip to content

Commit

Permalink
improve nb、mqtt and coap help and version information
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed May 16, 2023
1 parent ca96060 commit 1894a99
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@
*/
package iot.technology.client.toolkit.app;

import iot.technology.client.toolkit.app.config.CommandLineConfig;
import iot.technology.client.toolkit.app.config.LogLevelConfig;
import iot.technology.client.toolkit.app.settings.ConfigCommand;
import iot.technology.client.toolkit.app.settings.info.MainInfo;
import iot.technology.client.toolkit.app.settings.lang.LangService;
import iot.technology.client.toolkit.coap.command.CoapCommand;
import iot.technology.client.toolkit.common.commandline.CommandErrorMessageHandler;
import iot.technology.client.toolkit.common.commandline.CommandLineConfig;
import iot.technology.client.toolkit.common.commandline.ExceptionMessageHandler;
import iot.technology.client.toolkit.common.config.LogLevelConfig;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import iot.technology.client.toolkit.common.constants.HelpVersionGroup;
import iot.technology.client.toolkit.common.constants.StorageConstants;
import iot.technology.client.toolkit.common.constants.VersionInfo;
import iot.technology.client.toolkit.common.exception.ExceptionMessageHandler;
import iot.technology.client.toolkit.mqtt.command.MqttCommand;
import iot.technology.client.toolkit.nb.command.NbCommand;
import org.fusesource.jansi.AnsiConsole;
import picocli.AutoComplete;
import picocli.CommandLine;

import java.security.Security;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -69,6 +71,8 @@ public class ToolKitCommand implements Callable<Integer> {


public static void main(String[] args) {
Security.setProperty("crypto.policy", "unlimited");

LogLevelConfig.setLogLevel();
AnsiConsole.systemInstall();
try {
Expand All @@ -79,6 +83,7 @@ public static void main(String[] args) {
.setResourceBundle(bundle)
.setColorScheme(CommandLineConfig.getColorScheme())
.setUsageHelpWidth(CommandLineConfig.getCliWidth())
.setParameterExceptionHandler(new CommandErrorMessageHandler())
.setExecutionExceptionHandler(new ExceptionMessageHandler())
.setAdjustLineBreaksForWideCJKCharacters(true)
.setCaseInsensitiveEnumValuesAllowed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
@CommandLine.Command(
name = "config",
requiredOptionMarker = '*',
header = "@|bold ${bundle:config.header}|@",
description = "@|fg(red),bold ${bundle:config.description}|@",
optionListHeading = "%n${bundle:general.option}:%n",
header = "@|fg(Cyan),bold ${bundle:config.header}|@",
description = "@|fg(Cyan),italic ${bundle:config.description}|@",
synopsisHeading = "%n@|bold ${bundle:general.usage}|@%n",
commandListHeading = "%n@|bold ${bundle:general.commands}|@%n",
descriptionHeading = "%n",
footerHeading = "%nCopyright (c) 2019-2023, ${bundle:general.copyright}",
footer = "%nDeveloped by mushuwei",
versionProvider = iot.technology.client.toolkit.common.constants.VersionInfo.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
@CommandLine.Command(
name = "coap",
requiredOptionMarker = '*',
header = "@|fg(Cyan),bold ${bundle:coap.header}|@",
description = "@|fg(Cyan),italic ${bundle:coap.description}|@",
optionListHeading = "%n${bundle:general.option}:%n",
header = "@|bold ${bundle:coap.header}|@",
description = "@|fg(red),bold ${bundle:coap.description}|@",
synopsisHeading = "%n@|bold ${bundle:general.usage}|@%n",
commandListHeading = "%n@|bold ${bundle:general.commands}|@%n",
optionListHeading = "%n@|bold ${bundle:general.option}|@%n",
descriptionHeading = "%n",
subcommands = {
CoapDescribeCommand.class,
CoapMediaTypesCommand.class,
Expand All @@ -53,24 +56,12 @@ public class CoapCommand implements Callable<Integer> {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "${bundle:general.help.description}")
boolean usageHelpRequested;

@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@Override
public Integer call() {
StringBuilder sb = new StringBuilder();
sb.append("describe, desc: ").append(colorItalic(bundle.getString("coap.desc.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("media-type, mt: ").append(colorItalic(bundle.getString("coap.media.types.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("discover, disc: ").append(colorItalic(bundle.getString("coap.disc.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("get: ").append(colorItalic(bundle.getString("coap.get.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("post: ").append(colorItalic(bundle.getString("coap.post.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("put: ").append(colorItalic(bundle.getString("coap.put.description"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("delete, del: ").append(colorItalic(bundle.getString("coap.del.description"), "cyan"))
.append(StringUtils.lineSeparator());
System.out.println(sb);
System.out.println(spec.commandLine().getUsageMessage());
return ExitCodeEnum.SUCCESS.getValue();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package iot.technology.client.toolkit.common.commandline;

import picocli.CommandLine;

import java.io.PrintWriter;

/**
* @author mushuwei
*/
public class CommandErrorMessageHandler extends CommonErrorMessageHandler
implements CommandLine.IParameterExceptionHandler {

@Override
public int handleParseException(CommandLine.ParameterException ex, String[] args) throws Exception {
final int exitCode = super.handleParseException(ex, args);
final CommandLine cmd = ex.getCommandLine();
final PrintWriter writer = cmd.getErr();
final CommandLine.Model.CommandSpec spec = cmd.getCommandSpec();
writer.printf("Try '%s --help' for more information.%n", spec.qualifiedName());
return exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iot.technology.client.toolkit.app.config;
package iot.technology.client.toolkit.common.commandline;

import picocli.CommandLine;

Expand All @@ -13,6 +13,7 @@ public class CommandLineConfig {
.options(CommandLine.Help.Ansi.Style.italic, CommandLine.Help.Ansi.Style.fg_yellow)
.parameters(CommandLine.Help.Ansi.Style.fg_yellow)
.optionParams(CommandLine.Help.Ansi.Style.italic)
.errors(CommandLine.Help.Ansi.Style.bold, CommandLine.Help.Ansi.Style.fg_red)
.build();
private static final int CLI_WIDTH = 160;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package iot.technology.client.toolkit.common.commandline;

import picocli.CommandLine;

import java.io.PrintWriter;

/**
* @author mushuwei
*/
public abstract class CommonErrorMessageHandler implements CommandLine.IParameterExceptionHandler {

@Override
public int handleParseException(CommandLine.ParameterException ex, String[] args) throws Exception {
printErrorMessage(ex);

return generateExitCode(ex);
}

private void printErrorMessage(final CommandLine.ParameterException parameterException) {
final PrintWriter writer = parameterException.getCommandLine().getErr();

if (parameterException.getCause() != null) {
writer.println(parameterException.getMessage()
.replace(parameterException.getCause().toString(), parameterException.getCause().getMessage()));
} else {
writer.println(parameterException.getMessage());
}
}

private int generateExitCode(final CommandLine.ParameterException parameterException) {
final CommandLine cmd = parameterException.getCommandLine();
final CommandLine.Model.CommandSpec spec = cmd.getCommandSpec();
return cmd.getExitCodeExceptionMapper() != null ?
cmd.getExitCodeExceptionMapper().getExitCode(parameterException) :
spec.exitCodeOnInvalidInput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package iot.technology.client.toolkit.common.exception;
package iot.technology.client.toolkit.common.commandline;

import picocli.CommandLine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package iot.technology.client.toolkit.app.config;
package iot.technology.client.toolkit.common.config;

/**
* @author mushuwei
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import iot.technology.client.toolkit.mqtt.command.sub.MqttPublishCommand;
import iot.technology.client.toolkit.mqtt.command.sub.MqttSettingsCommand;
import iot.technology.client.toolkit.mqtt.command.sub.MqttSubscribeCommand;
import org.jetbrains.annotations.NotNull;
import picocli.CommandLine;

import java.util.ResourceBundle;
Expand All @@ -34,9 +35,12 @@
@CommandLine.Command(
name = "mqtt",
requiredOptionMarker = '*',
header = "@|fg(Cyan),bold ${bundle:mqtt.header}|@",
description = "@|fg(Cyan),italic ${bundle:mqtt.description}|@",
optionListHeading = "%n${bundle:general.option}:%n",
header = "@|bold ${bundle:mqtt.header}|@",
description = "@|fg(red),bold ${bundle:mqtt.description}|@",
synopsisHeading = "%n@|bold ${bundle:general.usage}|@%n",
commandListHeading = "%n@|bold ${bundle:general.commands}|@%n",
optionListHeading = "%n@|bold ${bundle:general.option}|@%n",
descriptionHeading = "%n",
subcommands = {
MqttDescribeCommand.class,
MqttSettingsCommand.class,
Expand All @@ -52,12 +56,12 @@ public class MqttCommand implements Callable<Integer> {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "${bundle:general.help.description}")
boolean usageHelpRequested;

@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@Override
public Integer call() {
System.out.format("describe, desc: " + colorItalic(bundle.getString("mqtt.desc.description"), "cyan") + "%n");
System.out.format("settings, set: " + colorItalic(bundle.getString("mqtt.settings.desc"), "cyan") + "%n");
System.out.format("publish, pub: " + colorItalic(bundle.getString("mqtt.pub.description"), "cyan") + "%n");
System.out.format("subscribe, sub: " + colorItalic(bundle.getString("mqtt.sub.description"), "cyan") + "%n");
System.out.println(spec.commandLine().getUsageMessage());
return ExitCodeEnum.SUCCESS.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
@CommandLine.Command(
name = "nb",
requiredOptionMarker = '*',
header = "@|fg(Cyan),bold ${bundle:nb.header}|@",
description = "@|fg(Cyan),italic ${bundle:nb.description}|@",
optionListHeading = "%n${bundle:general.option}:%n",
header = "@|bold ${bundle:nb.header}|@",
description = "@|fg(red),bold ${bundle:nb.description}|@",
synopsisHeading = "%n@|bold ${bundle:general.usage}|@%n",
commandListHeading = "%n@|bold ${bundle:general.commands}|@%n",
optionListHeading = "%n@|bold ${bundle:general.option}|@%n",
descriptionHeading = "%n",
subcommands = {
NbCallCommand.class,
NbSettingsCommand.class,
Expand All @@ -50,16 +53,12 @@ public class NbCommand implements Callable<Integer> {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "${bundle:general.help.description}")
boolean usageHelpRequested;

@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@Override
public Integer call() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("call: ").append(colorItalic(bundle.getString("nb.call.desc"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("settings, set: ").append(colorItalic(bundle.getString("nb.call.desc"), "cyan"))
.append(StringUtils.lineSeparator());
sb.append("describe, desc: ").append(colorItalic(bundle.getString("nb.desc.desc"), "cyan"))
.append(StringUtils.lineSeparator());
System.out.println(sb);
System.out.println(spec.commandLine().getUsageMessage());
return ExitCodeEnum.SUCCESS.getValue();
}

Expand Down

0 comments on commit 1894a99

Please sign in to comment.