@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { for (CommandElement element : this.elements) { element.parse(source, args, context); } }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { for (int i = 0; i < this.times; ++i) { this.element.parse(source, args, context); } }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { while (args.hasNext()) { this.element.parse(source, args, context); } }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { checkPermission(source, args); this.element.parse(source, args, context); }
@Override public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) { for (int i = 0; i < this.times; ++i) { CommandArgs.Snapshot startState = args.getSnapshot(); try { this.element.parse(src, args, context); } catch (ArgumentParseException e) { args.applySnapshot(startState); return this.element.complete(src, args, context); } } return Collections.emptyList(); }
@Override public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) { while (args.hasNext()) { CommandArgs.Snapshot startState = args.getSnapshot(); try { this.element.parse(src, args, context); } catch (ArgumentParseException e) { args.applySnapshot(startState); return this.element.complete(src, args, context); } } return Collections.emptyList(); }
private boolean parseLongFlag(CommandSource source, String longFlag, CommandArgs args, CommandContext context) throws ArgumentParseException { String[] flagSplit = longFlag.split("=", 2); String flag = flagSplit[0].toLowerCase(); CommandElement element = this.longFlags.get(flag); if (element == null) { switch (this.unknownLongFlagBehavior) { case ERROR: throw args.createError(t("Unknown long flag %s specified", flagSplit[0])); case ACCEPT_NONVALUE: context.putArg(flag, flagSplit.length == 2 ? flagSplit[1] : true); return true; case ACCEPT_VALUE: context.putArg(flag, flagSplit.length == 2 ? flagSplit[1] : args.next()); return true; case IGNORE: return false; default: throw new Error("New UnknownFlagBehavior added without corresponding case clauses"); } } else if (flagSplit.length == 2) { args.insertArg(flagSplit[1]); } element.parse(source, args, context); return true; }
private boolean parseShortFlags(CommandSource source, String shortFlags, CommandArgs args, CommandContext context) throws ArgumentParseException { for (int i = 0; i < shortFlags.length(); i++) { String shortFlag = shortFlags.substring(i, i + 1); CommandElement element = this.shortFlags.get(shortFlag); if (element == null) { switch (this.unknownShortFlagBehavior) { case IGNORE: if (i == 0) { return false; } throw args.createError(t("Unknown short flag %s specified", shortFlag)); case ERROR: throw args.createError(t("Unknown short flag %s specified", shortFlag)); case ACCEPT_NONVALUE: context.putArg(shortFlag, true); break; case ACCEPT_VALUE: context.putArg(shortFlag, args.next()); break; default: throw new Error("New UnknownFlagBehavior added without corresponding case clauses"); } } else { element.parse(source, args, context); } } return true; }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { this.element.parse(source, args, context); if (context.getAll(this.element.getUntranslatedKey()).size() > 1) { Text key = this.element.getKey(); throw args.createError(t("Argument %s may have only one value!", key != null ? key : t("unknown"))); } }
/** * Process this command with existing arguments and context objects. * * * @param source The source to populate the context with * @param args The arguments to process with * @param context The context to put data in * @throws ArgumentParseException if an invalid argument is provided */ public void populateContext(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { this.args.parse(source, args, context); if (args.hasNext()) { args.next(); throw args.createError(t("Too many arguments!")); } }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { ArgumentParseException lastException = null; for (CommandElement element : this.elements) { CommandArgs.Snapshot startState = args.getSnapshot(); CommandContext.Snapshot contextSnapshot = context.createSnapshot(); try { element.parse(source, args, context); return; } catch (ArgumentParseException ex) { lastException = ex; args.applySnapshot(startState); context.applySnapshot(contextSnapshot); } } if (lastException != null) { throw lastException; } }
CommandArgs.Snapshot start = args.getSnapshot(); try { element.parse(src, args, context);
@Override public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) { Set<String> completions = Sets.newHashSet(); for (CommandElement element : elements) { CommandArgs.Snapshot state = args.getSnapshot(); try { element.parse(src, args, context); if (state.equals(args.getSnapshot())) { completions.addAll(element.complete(src, args, context)); args.applySnapshot(state); } else if (args.hasNext()) { completions.clear(); } else { args.applySnapshot(state); completions.addAll(element.complete(src, args, context)); if (!(element instanceof OptionalCommandElement)) { break; } args.applySnapshot(state); } } catch (ArgumentParseException ignored) { args.applySnapshot(state); completions.addAll(element.complete(src, args, context)); break; } } return Lists.newArrayList(completions); }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { CommandArgs.Snapshot state = args.getSnapshot(); while (args.hasNext()) { String arg = args.next(); if (arg.startsWith("-")) { CommandArgs.Snapshot start = args.getSnapshot(); boolean remove; if (arg.startsWith("--")) { // Long flag remove = parseLongFlag(source, arg.substring(2), args, context); } else { remove = parseShortFlags(source, arg.substring(1), args, context); } if (remove) { args.removeArgs(start, args.getSnapshot()); } } else if (this.anchorFlags) { break; } } // We removed the arguments so we don't parse them as they have already been parsed as flags, // so don't restore them here! args.applySnapshot(state, false); if (this.childElement != null) { this.childElement.parse(source, args, context); } }
@Test public void testGettingBothPlayers() throws Exception { CommandArgs args = new CommandArgs("test", Lists.newArrayList(new SingleArg("test", 0, 5))); CommandContext context = new CommandContext(); CommandSource source = Mockito.mock(CommandSource.class); getPlayerElement().parse(source, args, context); Collection<Player> cpl = context.getAll(Text.of("player")); assertEquals(2, cpl.size()); Collection<String> s = cpl.stream().map(User::getName).collect(Collectors.toList()); assertTrue(s.contains("test1")); assertTrue(s.contains("test2")); }
private CommandContext parseWithInput(CommandElement element, String input) throws ArgumentParseException { CommandContext context = new CommandContext(); element.parse(TEST_SOURCE, new CommandArgs(input, InputTokenizer.quotedStrings(false).tokenize(input, false)), context); return context; }
@Test public void testGettingSinglePlayer() throws Exception { CommandArgs args = new CommandArgs("test1", Lists.newArrayList(new SingleArg("test1", 0, 5))); CommandContext context = new CommandContext(); CommandSource source = Mockito.mock(CommandSource.class); getPlayerElement().parse(source, args, context); assertEquals("test1", context.<Player>getOne(Text.of("player")).get().getName()); }
@Override public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException { if (!args.hasNext()) { Text key = this.element.getKey(); if (key != null && this.value != null) { context.putArg(key.toPlain(), this.value); } return; } CommandArgs.Snapshot startState = args.getSnapshot(); try { this.element.parse(source, args, context); } catch (ArgumentParseException ex) { if (this.considerInvalidFormatEmpty || args.hasNext()) { // If there are more args, suppress. Otherwise, throw the error args.applySnapshot(startState); if (this.element.getKey() != null && this.value != null) { context.putArg(this.element.getUntranslatedKey(), this.value); } } else { throw ex; } } }
@Test public void testFirstParsingWhenFirstSequenceSucceeds() throws ArgumentParseException { CommandArgs args = new CommandArgs("test test", InputTokenizer.spaceSplitString().tokenize("test test", true)); CommandContext context = new CommandContext(); CommandElement sut = GenericArguments.firstParsing( GenericArguments.seq(GenericArguments.literal(Text.of("test"), "test"), GenericArguments.literal(Text.of("test1"), "test")), GenericArguments.seq(GenericArguments.literal(Text.of("test2"), "test"), GenericArguments.literal(Text.of("test3"), "test")) ); sut.parse(MOCK_SOURCE, args, context); assertFalse(context.hasAny("test2")); assertFalse(context.hasAny("test3")); assertTrue(context.hasAny("test")); assertTrue(context.hasAny("test")); }
@Test public void testFirstParsingWhenFirstSequenceFails() throws ArgumentParseException { CommandArgs args = new CommandArgs("test test", InputTokenizer.spaceSplitString().tokenize("test test", true)); CommandContext context = new CommandContext(); CommandElement sut = GenericArguments.firstParsing( GenericArguments.seq(GenericArguments.literal(Text.of("test"), "test"), GenericArguments.literal(Text.of("test1"), "notatest")), GenericArguments.seq(GenericArguments.literal(Text.of("test2"), "test"), GenericArguments.literal(Text.of("test3"), "test")) ); sut.parse(MOCK_SOURCE, args, context); assertTrue(context.hasAny("test2")); assertTrue(context.hasAny("test3")); assertFalse(context.hasAny("test")); assertFalse(context.hasAny("test1")); }