All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rinde.rinsim.cli.Menu Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/*
 * Copyright (C) 2011-2015 Rinde van Lon, iMinds-DistriNet, KU Leuven
 *
 * 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 com.github.rinde.rinsim.cli;

import static com.github.rinde.rinsim.cli.CliException.checkAlreadySelected;
import static com.github.rinde.rinsim.cli.CliException.checkCommand;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Sets.newLinkedHashSet;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

import com.github.rinde.rinsim.cli.CliException.CauseType;
import com.github.rinde.rinsim.cli.Option.OptionArg;
import com.github.rinde.rinsim.cli.Option.OptionNoArg;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

/**
 * A menu is the main class for a command-line interface. It contains all
 * options and via the {@link #execute(String...)} method the command-line
 * arguments are parsed and handled. Instances can be constructed via the
 * {@link #builder()} method.
 * @author Rinde van Lon
 */
public final class Menu {
  final String header;
  final String footer;
  final String cmdLineSyntax;
  final ImmutableMap optionMap;
  final ImmutableList> groups;
  final ImmutableMultimap groupMap;
  final HelpFormatter helpFormatter;

  Menu(Builder b) {
    header = b.header;
    footer = b.footer;
    cmdLineSyntax = b.cmdLineSyntax;
    optionMap = ImmutableMap.copyOf(b.optionMap);
    helpFormatter = b.helpFormatter;

    final ImmutableList.Builder> groupsBuilder = ImmutableList
        .builder();
    final ImmutableMultimap.Builder groups2Builder = ImmutableMultimap
        .builder();
    for (final Set