本文整理汇总了Java中com.beust.jcommander.JCommander.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java JCommander.Builder方法的具体用法?Java JCommander.Builder怎么用?Java JCommander.Builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.beust.jcommander.JCommander
的用法示例。
在下文中一共展示了JCommander.Builder方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.beust.jcommander.JCommander; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
initializeCommands();
Runner runner = new Runner();
Server server = new Server();
JCommander.Builder builder = JCommander.newBuilder().addObject(runner);
commands.forEach(command -> builder
.addCommand(command.getClass().getSimpleName().toLowerCase(), command));
JCommander jc = builder.build();
try {
jc.parse(args);
Optional<SubCommandBase> selectedCommand = commands.stream().filter(
command -> command.getClass().getSimpleName().toLowerCase()
.equals(jc.getParsedCommand())).findFirst();
if (selectedCommand.isPresent()) {
selectedCommand.get().run();
} else {
jc.usage();
}
} catch (ParameterException exception) {
System.err.println("Wrong parameters: " + exception.getMessage());
jc.usage();
}
}
示例2: createCommander
import com.beust.jcommander.JCommander; //导入方法依赖的package包/类
private static JCommander createCommander(CommandContainer commandContainer, String[] args) {
JCommander.Builder builder = JCommander.newBuilder();
for (String operation : commandContainer.getAllCommands()) {
CommandContainer.Command command = commandContainer.getCommand(operation);
builder.addCommand(operation, command);
}
JCommander commander = builder.build();
commander.setProgramName("stf");
commander.setCaseSensitiveOptions(false);
commander.parseWithoutValidation(args);
return commander;
}
示例3: main
import com.beust.jcommander.JCommander; //导入方法依赖的package包/类
public static void main(String... argv) throws Exception {
//Attempt to determine verbose level first
final Now now = new Now();
JCommander.newBuilder().addObject(now)
.acceptUnknownOptions(true)
.build().parse(argv);
final CommandMain commandMain = new CommandMain();
final JCommander.Builder commanderBuilder = JCommander.newBuilder()
.programName("java -jar now4j-cli-<version>.jar")
.verbose(now.verboseLevel)
.addObject(commandMain);
for (final Map.Entry<String, AbstractCommand> commandEntry : commandMap.entrySet()) {
commanderBuilder.addCommand(commandEntry.getKey(), commandEntry.getValue());
}
final JCommander commander = commanderBuilder.build();
commander.parse(argv);
if (commandMain.help) {
commander.usage();
return;
}
final NowClient nowClient;
final String token = commandMain.token;
if (token == null || token.isEmpty()) {
//Default Now Client, with no option => read from /.now.json file
nowClient = NowClient.create();
} else {
//There is a token option
final String team = commandMain.team;
if (team == null || team.isEmpty()) {
//Read team from /.now.json, if any
nowClient = NowClient.create(token);
} else {
nowClient = NowClient.create(token, team);
}
}
final String parsedCommand = commander.getParsedCommand();
if (!commandMap.containsKey(parsedCommand)) {
throw new IllegalArgumentException("Unsupported command: " +
parsedCommand + ". Possible commands: " + commandMap.keySet());
}
final AbstractCommand abstractCommand = commandMap.get(parsedCommand);
abstractCommand.setNowClient(nowClient);
abstractCommand.work();
}
示例4: main
import com.beust.jcommander.JCommander; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
SenseHatTester senseHatTester = new SenseHatTester();
Map<String, Command> commands = new HashMap<>();
commands.put("fill", new FillCommand());
commands.put("set-pixel", new SetPixelCommand());
commands.put("save-image", new SaveImageCommand());
commands.put("load-image", new LoadImageCommand());
commands.put("env", new EnvCommand());
commands.put("pulse", new PulseCommand());
commands.put("text", new TextCommand());
commands.put("clock", new BinaryClockCommand());
JCommander.Builder builder = JCommander.newBuilder()
.addObject(senseHatTester);
commands.forEach((name, cmd) -> builder.addCommand(name, cmd));
JCommander jCommander = builder.build();
jCommander.parse(args);
if(senseHatTester.isPrintUsage()) {
jCommander.usage();
return;
}
String parsedCommand = jCommander.getParsedCommand();
if (parsedCommand != null && commands.containsKey(parsedCommand)) {
SenseHat senseHat;
try {
if(senseHatTester.getFrameBuffer() != null) {
senseHat = new SenseHat(senseHatTester.getFrameBuffer());
} else {
senseHat = new SenseHat();
}
} catch (IllegalStateException e) {
log.error(e.getLocalizedMessage(), e);
return;
}
commands.get(parsedCommand).run(senseHat);
} else {
jCommander.usage();
}
}