Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* 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.qpid.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* CommandLineParser provides a utility for specifying the format of a command line and parsing command lines to ensure
* that they fit their specified format. A command line is made up of flags and options, both may be referred to as
* options. A flag is an option that does not take an argument (specifying it means it has the value 'true' and not
* specifying it means it has the value 'false'). Options must take arguments but they can be set up with defaults so
* that they take a default value when not set. Options may be mandatory in which case it is an error not to specify
* them on the command line. Flags are never mandatory because they are implicitly set to false when not specified.
*
*
This has one no-arg flag and two 'free' arguments:
*
* zip -r project.zip project/*
*
*
This one concatenates multiple flags into a single block with only one '-':
*
* jar -tvf mytar.tar
*
*
*
*
The parsing rules are:
*
*
*
Flags may be combined after a single '-' because they never take arguments. Normally such flags are single letter
* flags but this is only a convention and not enforced. Flags of more than one letter are usually specified on their own.
*
Options expecting arguments must always be on their own.
*
The argument to an option may be separated from it by whitespace or appended directly onto the option.
*
The argument to an option may never begin with a '-' character.
*
All other arguments not beginning with a '-' character are free arguments that do not belong to any option.
*
The second or later of a set of duplicate or repeated flags are ignored.
*
Options are matched up to the shortest matching option. This is because of the possibility of having no space
* between an option and its argument. This rules out the possibility of using two options where one is an opening
* substring of the other. For example, the options "foo" and "foobar" cannot be used on the same command line because
* it is not possible to distinguish the argument "-foobar" from being the "foobar" option or the "foo" option with
* the "bar" argument.
*
*
*
By default, unknown options are simply ignored if specified on the command line. This behaviour may be changed
* so that the parser reports all unknowns as errors by using the {@link #setErrorsOnUnknowns} method.
*/
public class CommandLineParser
{
/** Holds a mapping from command line option names to detailed information about those options. */
private Map optionMap = new HashMap();
/** Holds a list of parsing errors. */
private List parsingErrors = new ArrayList();
/** Holds the regular expression matcher to match command line options with. */
private Matcher optionMatcher = null;
/** Holds the parsed command line properties after parsing. */
private Properties parsedProperties = null;
/** Flag used to indicate that errors should be created for unknown options. False by default. */
private boolean errorsOnUnknowns = false;
/**
* Creates a command line options parser from a command line specification. This is passed to this constructor
* as an array of arrays of strings. Each array of strings specifies the command line for a single option. A static
* array may therefore easily be used to configure the command line parser in a single method call with an easily
* readable format.
*
*
Each array of strings must be 2, 3, 4 or 5 elements long. If any of the last three elements are missing they
* are assumed to be null. The elements specify the following parameters:
*
*
The name of the option without the leading '-'. For example, "file". To specify the format of the 'free'
* arguments use the option names "1", "2", ... and so on.
*
The option comment. A line of text describing the usage of the option. For example, "The file to be processed."
*
The options argument. This is a very short description of the argument to the option, often a single word
* or a reminder as to the arguments format. When this element is null the option is a flag and does not
* accept any arguments. For example, "filename" or "(unix | windows)" or null. The actual text specified
* is only used to print in the usage message to remind the user of the usage of the option.
*
The mandatory flag. When set to "true" an option must always be specified. Any other value, including null,
* means that the option is mandatory. Flags are always mandatory (see class javadoc for explanation of why) so
* this is ignored for flags.
*
A regular expression describing the format that the argument must take. Ignored if null.
*
*
An example call to this constructor is:
*
*
* CommandLineParser commandLine = new CommandLineParser(
* new String[][] {{"file", "The file to be processed. ", "filename", "true"},
* {"dir", "Directory to store results in. Current dir used if not set.", "out dir"},
* {"os", "Operating system EOL format to use.", "(windows | unix)", null, "windows\|unix"},
* {"v", "Verbose mode. Prints information about the processing as it goes."},
* {"1", "The processing command to run.", "command", "true", "add\|remove\|list"}});
*
*
* @param config The configuration as an array of arrays of strings.
*/
public CommandLineParser(String[][] config)
{
// Loop through all the command line option specifications creating details for each in the options map.
for (int i = 0; i < config.length; i++)
{
String[] nextOptionSpec = config[i];
addOption(nextOptionSpec[0], nextOptionSpec[1], (nextOptionSpec.length > 2) ? nextOptionSpec[2] : null,
(nextOptionSpec.length > 3) ? ("true".equals(nextOptionSpec[3]) ? true : false) : false,
(nextOptionSpec.length > 4) ? nextOptionSpec[4] : null);
}
}
/**
* Lists all the parsing errors from the most recent parsing in a string.
*
* @return All the parsing errors from the most recent parsing.
*/
public String getErrors()
{
// Return the empty string if there are no errors.
if (parsingErrors.isEmpty())
{
return "";
}
// Concatenate all the parsing errors together.
StringBuilder result = new StringBuilder();
for (String s : parsingErrors)
{
result.append(s);
}
return result.toString();
}
/**
* Lists the properties set from the most recent parsing or an empty string if no parsing has been done yet.
*
* @return The properties set from the most recent parsing or an empty string if no parsing has been done yet.
*/
public String getOptionsInForce()
{
// Check if there are no properties to report and return and empty string if so.
if (parsedProperties == null)
{
return "";
}
// List all the properties.
StringBuilder result = new StringBuilder("Options in force:\n");
for (Map.Entry