us.fatehi.commandlineparser.CommandLineArgumentsParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schemacrawler Show documentation
Show all versions of schemacrawler Show documentation
SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.
/*
* SchemaCrawler
* http://www.schemacrawler.com
* Copyright (c) 2000-2016, Sualeh Fatehi.
* This library is free software; you can redistribute it and/or modify it under
* the terms
* of the GNU Lesser General Public License as published by the Free Software
* Foundation;
* either version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
package us.fatehi.commandlineparser;
import static java.util.Objects.requireNonNull;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Command-line options parser. Not POSIX compliant. Follows these POSIX
* rules:
*
* - Arguments are options if they begin with a hyphen delimiter
* ('-').
* - Certain options require an argument. For example, the '-o'
* command of the ld command requires an argument—an output file name.
*
* - Options typically precede other non-option arguments.
* - Options may be supplied in any order, or appear multiple times.
* The interpretation is left up to the particular application program.
*
*
* Does not honor these POSIX rules:
*
* - Multiple options may follow a hyphen delimiter in a single token
* if the options do not take arguments. Thus, '-abc' is equivalent to
* '-a -b -c'.
* - Option names are single alphanumeric characters (as for isalnum;
* see Classification of Characters).
* - An option and its argument may or may not appear as separate
* tokens. (In other words, the whitespace separating them is optional.)
* Thus, '-o foo' and '-ofoo' are equivalent.
* - The argument '--' terminates all options; any following arguments
* are treated as non-option arguments, even if they begin with a
* hyphen.
* - A token consisting of a single hyphen character is interpreted as
* an ordinary non-option argument. By convention, it is used to specify
* input from or output to the standard input and output streams.
*
*/
public class CommandLineArgumentsParser
{
private static final String DASH = "-";
private final String[] args;
private final Map optionsMap;
private final List nonOptionArguments;
public CommandLineArgumentsParser(final String[] args)
{
this.args = requireNonNull(args);
optionsMap = new HashMap<>();
nonOptionArguments = new ArrayList<>();
}
public List getNonOptionArguments()
{
return nonOptionArguments;
}
public Map getOptionsMap()
{
return optionsMap;
}
/**
* Extract the options and non-option arguments from the given list of
* command-line arguments.
*/
public void parse()
{
final Deque argsList = new ArrayDeque<>(Arrays.asList(args));
while (true)
{
final String currentArg = argsList.pollFirst();
if (currentArg == null)
{
if (argsList.isEmpty())
{
break;
}
else
{
continue;
}
}
if (currentArg.startsWith(DASH))
{
// Handle -arg=value
if (currentArg.contains("="))
{
final String[] split = currentArg.split("=", 2);
final String option = split[0].replaceAll("^-+", "");
final String value;
if (split.length == 2)
{
value = split[1];
}
else
{
value = null;
}
optionsMap.put(option, value);
}
else
{
// Look at the next argument, and if is an option, that means
// there is no value for the current option
final String option = currentArg.replaceAll("^-+", "");
final String value = argsList.peekFirst();
if (value != null && value.startsWith(DASH))
{
optionsMap.put(option, null);
}
else
{
optionsMap.put(option, argsList.pollFirst());
}
}
}
else
{
nonOptionArguments.add(currentArg);
}
}
}
@Override
public String toString()
{
return Arrays.toString(args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy