br.com.objectos.args.Command Maven / Gradle / Ivy
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.args;
import java.util.Objects;
import java.util.stream.Stream;
/**
* @author [email protected] (Marcio Endo)
*/
public class Command extends Part {
private static final Command EMPTY = new Empty();
private final String name;
Command(Part next, String name) {
super(next);
this.name = name;
}
public static Command empty() {
return EMPTY;
}
@Override
public String toString() {
return name;
}
@Override
void acceptOptionMap(OptionMap.Builder optionMap) {
// noop
}
boolean hasName(String name) {
Objects.requireNonNull(name);
return this.name.equals(name);
}
CommandBuilder matchedSubcommand(Args args, ArgsParser parser) {
Objects.requireNonNull(parser);
next.visited();
return CommandBuilder.matched(parser.parse(args));
}
boolean nextSubCommand(String subCommand) {
Objects.requireNonNull(subCommand);
return next.subCommand(subCommand);
}
CommandBuilder onCommand(Args args, String name, ArgsParser parser) {
Objects.requireNonNull(parser);
return hasName(name)
? CommandBuilder.matched(parser.parse(args))
: CommandBuilder.chain(args, this);
}
CommandBuilder onCommand(Args args, String name, String subCommand, ArgsParser parser) {
return hasName(name)
? nextSubCommand(subCommand)
? matchedSubcommand(args, parser)
: CommandBuilder.chain(args, this)
: CommandBuilder.chain(args, this);
}
@Override
Command toCommand() {
return this;
}
@Override
Stream upgradeIfPossible(Box box) {
throw new UnsupportedOperationException();
}
private static class Empty extends Command {
public Empty() {
super(null, null);
}
@Override
CommandBuilder onCommand(Args args, String name, ArgsParser parser) {
return CommandBuilder.empty(args);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy