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

org.apache.commons.cli.CommandLine Maven / Gradle / Ivy

/*
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You 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 org.apache.commons.cli;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/**
 * Represents list of arguments parsed against a {@link Options} descriptor.
 * 

* It allows querying of a boolean {@link #hasOption(String opt)}, in addition to retrieving the * {@link #getOptionValue(String opt)} for options requiring arguments. *

* Additionally, any left-over or unrecognized arguments, are available for further processing. */ public class CommandLine implements Serializable { /** * A nested builder class to create {@code CommandLine} instance using descriptive methods. * * @since 1.4 */ public static final class Builder { /** * CommandLine that is being build by this Builder. */ private final CommandLine commandLine = new CommandLine(); /** * Add left-over unrecognized option/argument. * * @param arg the unrecognized option/argument. * * @return this Builder instance for method chaining. */ public Builder addArg(final String arg) { commandLine.addArg(arg); return this; } /** * Add an option to the command line. The values of the option are stored. * * @param opt the processed option. * * @return this Builder instance for method chaining. */ public Builder addOption(final Option opt) { commandLine.addOption(opt); return this; } public CommandLine build() { return commandLine; } } /** The serial version UID. */ private static final long serialVersionUID = 1L; /** The unrecognized options/arguments */ private final List args = new LinkedList<>(); /** The processed options */ private final List

* Dump state, suitable for debugging. *

* * @return Stringified form of this object. */ /* * public String toString() { StringBuilder buf = new StringBuilder(); * * buf.append("[ CommandLine: [ options: "); buf.append(options.toString()); buf.append(" ] [ args: "); * buf.append(args.toString()); buf.append(" ] ]"); * * return buf.toString(); } */ /** * Tests to see if an option has been set. * * @param opt character name of the option. * @return true if set, false if not. */ public boolean hasOption(final char opt) { return hasOption(String.valueOf(opt)); } /** * Tests to see if an option has been set. * * @param opt the option to check. * @return true if set, false if not. * @since 1.5.0 */ public boolean hasOption(final Option opt) { return options.contains(opt); } /** * Tests to see if an option has been set. * * @param opt Short name of the option. * @return true if set, false if not. */ public boolean hasOption(final String opt) { return hasOption(resolveOption(opt)); } /** * Returns an iterator over the Option members of CommandLine. * * @return an {@code Iterator} over the processed {@link Option} members of this {@link CommandLine}. */ public Iterator




© 2015 - 2025 Weber Informatics LLC | Privacy Policy