本文整理匯總了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();
}
}