Package org.obolibrary.robot
Class CommandLineHelper
- java.lang.Object
-
- org.obolibrary.robot.CommandLineHelper
-
public class CommandLineHelper extends Object
Convenience methods for working with command line options.
-
-
Constructor Summary
Constructors Constructor Description CommandLineHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static List<String>
cleanAxiomStrings(org.apache.commons.cli.CommandLine line)
Given a command line, get the 'axioms' option(s) and make sure all are properly split and return one axiom selector per list entry.static Map<String,String>
getAddPrefixes(org.apache.commons.cli.CommandLine line)
Given a command line, get a map of all prefixes to use and add to the output.static Set<Class<? extends org.semanticweb.owlapi.model.OWLAxiom>>
getAxiomValues(org.apache.commons.cli.CommandLine line)
Deprecated.split into methodscleanAxiomStrings(CommandLine)
and others inRelatedObjectsHelper
static List<String>
getBaseNamespaces(org.apache.commons.cli.CommandLine line, IOHelper ioHelper)
Given a command line and an IOHelper, return a list of base namespaces from the '--base-iri' option.static boolean
getBooleanValue(org.apache.commons.cli.CommandLine line, String name, boolean defaultValue)
Get the boolean value of a command-line option with the given name.static boolean
getBooleanValue(org.apache.commons.cli.CommandLine line, String name, boolean defaultValue, boolean optionalArg)
Given a command line, an argument name, the boolean default value, and boolean if the arg is optional, return the value of the command-line option 'name'.static String
getCommand(org.apache.commons.cli.CommandLine line)
Given a command line, return its first argument, which should be the name of the command to execute.static org.apache.commons.cli.CommandLine
getCommandLine(String usage, org.apache.commons.cli.Options options, String[] args)
Parse the command line, handle help and other common options, (May exit!) and return a CommandLine.static org.apache.commons.cli.Options
getCommonOptions()
Create a new Options object with shared options for 'help' and 'version'.static String
getDefaultValue(org.apache.commons.cli.CommandLine line, String name, String defaultValue)
Get the value of the command-line option with the given name, or return a default value if the option is not found.static String
getIndexValue(org.apache.commons.cli.CommandLine line, int index)
Given a command line and an index, return the argument at that index.static List<org.semanticweb.owlapi.model.OWLOntology>
getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line)
Given an IOHelper and a command line, check input options and return a list of loaded input ontologies.static List<org.semanticweb.owlapi.model.OWLOntology>
getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, boolean allowEmpty)
Given an IOHelper and a command line, check for required options and return a list of loaded input ontologies.static List<org.semanticweb.owlapi.model.OWLOntology>
getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String catalogPath)
Given an IOHelper, a command line, and the path to a catalog file, check input options and return a list of loaded input ontologies with the catalog file.static org.semanticweb.owlapi.model.OWLOntology
getInputOntology(IOHelper ioHelper, org.apache.commons.cli.CommandLine line)
Given an IOHelper and a command line, check for required options and return a loaded input ontology.static org.semanticweb.owlapi.model.OWLOntology
getInputOntology(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String catalogPath)
Given an IOHelper, a command line, and a path to a catalog, return an OWLOntology loaded from input or input-iri using the specified catalog (or null).static IOHelper
getIOHelper(org.apache.commons.cli.CommandLine line)
Given a command line, return an initialized IOHelper.static String
getOptionalValue(org.apache.commons.cli.CommandLine line, String name)
Get the value of the command-line option with the given name.static List<String>
getOptionalValues(org.apache.commons.cli.CommandLine line, String name)
Get the value of the command-line options with the given name.static List<String>
getOptionValues(org.apache.commons.cli.CommandLine line, String name)
Given a command line, return the values for an option as a list.static File
getOutputFile(org.apache.commons.cli.CommandLine line)
Given a command line, check for the required options and return a File for saving data.static org.semanticweb.owlapi.model.IRI
getOutputIRI(org.apache.commons.cli.CommandLine line)
Given a command line, check for the required options and return an IRI to be used as the OntologyIRI for the output ontology.static org.semanticweb.owlapi.reasoner.OWLReasonerFactory
getReasonerFactory(org.apache.commons.cli.CommandLine line)
Given a string of a reasoner name from user input, return the reasoner factory.static org.semanticweb.owlapi.reasoner.OWLReasonerFactory
getReasonerFactory(org.apache.commons.cli.CommandLine line, boolean allowEMR)
Given a string of a reasoner name from user input, return the reasoner factory.static String
getRequiredValue(org.apache.commons.cli.CommandLine line, String name, String message)
Get the value of the command-line option with the given name, or throw an IllegalArgumentException with a given message if the option is not found.static Set<org.semanticweb.owlapi.model.IRI>
getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line)
Given an IOHelper and a command line, check for the required options and return a set of IRIs for terms.static Set<org.semanticweb.owlapi.model.IRI>
getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, boolean allowEmpty)
As getTerms, but allow the list to be empty.static Set<org.semanticweb.owlapi.model.IRI>
getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String singles, String paths)
Given an IOHelper and a command line, and the names of two options, check for the required options and return a set of IRIs for terms.static void
handleException(Exception exception)
Shared method for dealing with exceptions, printing help, and exiting.static void
handleException(String usage, org.apache.commons.cli.Options options, Exception exception)
Shared method for dealing with exceptions, printing help, and exiting.static boolean
hasFlagOrCommand(org.apache.commons.cli.CommandLine line, String name)
Return true if a command line include the option with the given name, or the command with the given name.static org.semanticweb.owlapi.model.IRI
maybeCreateIRI(IOHelper ioHelper, String term, String field)
Try to create an IRI from a string input.static org.apache.commons.cli.CommandLine
maybeGetCommandLine(String usage, org.apache.commons.cli.Options options, String[] args, boolean stopAtNonOption)
Parse the command line, handle help and other common options, and return null or a CommandLine.static void
maybeSaveOutput(org.apache.commons.cli.CommandLine line, org.semanticweb.owlapi.model.OWLOntology ontology)
Given a command line and an ontology, for each `--output` option (if any), save a copy of the ontology to the specified path.static List<String>
parseArgList(String toProcess)
Given a single string, return a list of strings split at whitespace but allowing for quoted values, as a command-line parser does.static String[]
parseArgs(String toProcess)
Given a single string, return an array of strings split at whitespace but allowing for quoted values, as a command-line parser does.static void
printHelp(String usage, org.apache.commons.cli.Options options)
Print a help message for a command.static void
printVersion()
Print the ROBOT versionprotected static List<String>
splitSelects(String selects)
Given an input string, return a list of the string split on whitespace, while ignoring any whitespace in single string quotes.static CommandState
updateInputOntology(IOHelper ioHelper, CommandState state, org.apache.commons.cli.CommandLine line)
Given an IOHelper, a state object, and a command line, update the state with the ontology.static CommandState
updateInputOntology(IOHelper ioHelper, CommandState state, org.apache.commons.cli.CommandLine line, boolean required)
Given an IOHelper, a state object, a command line, and a "required" flag, update the state with the ontology.
-
-
-
Method Detail
-
parseArgList
public static List<String> parseArgList(String toProcess) throws Exception
Given a single string, return a list of strings split at whitespace but allowing for quoted values, as a command-line parser does.- Parameters:
toProcess
- the string to parse- Returns:
- a string list, split at whitespace, with quotation marks removed
- Throws:
Exception
- on parsing problems
-
parseArgs
public static String[] parseArgs(String toProcess) throws Exception
Given a single string, return an array of strings split at whitespace but allowing for quoted values, as a command-line parser does. Adapted from org.apache.tools.ant.types.Commandline- Parameters:
toProcess
- the string to parse- Returns:
- a string array, split at whitespace, with quotation marks removed
- Throws:
Exception
- on parsing problems
-
getOptionValues
public static List<String> getOptionValues(org.apache.commons.cli.CommandLine line, String name)
Given a command line, return the values for an option as a list. This is just a convenience function for turning an array into a list.- Parameters:
line
- the command line to usename
- the name of the option to find- Returns:
- the option values as a list of strings, maybe empty
-
getIndexValue
public static String getIndexValue(org.apache.commons.cli.CommandLine line, int index)
Given a command line and an index, return the argument at that index.- Parameters:
line
- the command line to useindex
- the index of the argument- Returns:
- the argument at the given index
-
getCommand
public static String getCommand(org.apache.commons.cli.CommandLine line)
Given a command line, return its first argument, which should be the name of the command to execute.- Parameters:
line
- the command line to use- Returns:
- the name of the command to execute
-
hasFlagOrCommand
public static boolean hasFlagOrCommand(org.apache.commons.cli.CommandLine line, String name)
Return true if a command line include the option with the given name, or the command with the given name.- Parameters:
line
- the command line to usename
- the name of the flag or argument to check for- Returns:
- true if a flag or first argument match the given name, false otherwise
-
cleanAxiomStrings
public static List<String> cleanAxiomStrings(org.apache.commons.cli.CommandLine line)
Given a command line, get the 'axioms' option(s) and make sure all are properly split and return one axiom selector per list entry.- Parameters:
line
- the command line to use- Returns:
- cleaned list of input axiom type strings
-
getBaseNamespaces
public static List<String> getBaseNamespaces(org.apache.commons.cli.CommandLine line, IOHelper ioHelper)
Given a command line and an IOHelper, return a list of base namespaces from the '--base-iri' option.- Parameters:
line
- the command line to useioHelper
- the IOHelper to resolve prefixes- Returns:
- list of full base namespaces
-
getBooleanValue
public static boolean getBooleanValue(org.apache.commons.cli.CommandLine line, String name, boolean defaultValue, boolean optionalArg)
Given a command line, an argument name, the boolean default value, and boolean if the arg is optional, return the value of the command-line option 'name'.- Parameters:
line
- the command line to usename
- the name of the option to finddefaultValue
- the default value to use if the option is not providedoptionalArg
- if true, the option without an arg will return true- Returns:
- the option value as boolean, or the default if not found
-
getAxiomValues
@Deprecated public static Set<Class<? extends org.semanticweb.owlapi.model.OWLAxiom>> getAxiomValues(org.apache.commons.cli.CommandLine line)
Deprecated.split into methodscleanAxiomStrings(CommandLine)
and others inRelatedObjectsHelper
Given a command line, return the value of --axioms as a set of classes that extend OWLAxiom.- Parameters:
line
- the command line to use- Returns:
- set of OWLAxiom types
-
getBooleanValue
public static boolean getBooleanValue(org.apache.commons.cli.CommandLine line, String name, boolean defaultValue)
Get the boolean value of a command-line option with the given name.- Parameters:
line
- the command line to usename
- the name of the option to finddefaultValue
- the default value to use- Returns:
- the option value as boolean, or the default if not found
-
getOptionalValue
public static String getOptionalValue(org.apache.commons.cli.CommandLine line, String name)
Get the value of the command-line option with the given name.- Parameters:
line
- the command line to usename
- the name of the option to find- Returns:
- the option value as a string, or null if not found
-
getOptionalValues
public static List<String> getOptionalValues(org.apache.commons.cli.CommandLine line, String name)
Get the value of the command-line options with the given name.- Parameters:
line
- the command line to usename
- the name of the option to find- Returns:
- the option value as a list of strings, maybe empty
-
getDefaultValue
public static String getDefaultValue(org.apache.commons.cli.CommandLine line, String name, String defaultValue)
Get the value of the command-line option with the given name, or return a default value if the option is not found.- Parameters:
line
- the command line to usename
- the name of the option to finddefaultValue
- the default value to use- Returns:
- the option value as a string, or the default if the option not found
-
getRequiredValue
public static String getRequiredValue(org.apache.commons.cli.CommandLine line, String name, String message) throws IllegalArgumentException
Get the value of the command-line option with the given name, or throw an IllegalArgumentException with a given message if the option is not found.- Parameters:
line
- the command line to usename
- the name of the option to findmessage
- the message for the exception- Returns:
- the option value as a string
- Throws:
IllegalArgumentException
- if the option is not found
-
getIOHelper
public static IOHelper getIOHelper(org.apache.commons.cli.CommandLine line) throws IOException
Given a command line, return an initialized IOHelper. The --prefix, --add-prefix, --prefixes, --add-prefixes, --noprefixes, --xml-entities, and --base options are handled.- Parameters:
line
- the command line to use- Returns:
- an initialized IOHelper
- Throws:
IOException
- on issue creating IOHelper with given context
-
getInputOntology
public static org.semanticweb.owlapi.model.OWLOntology getInputOntology(IOHelper ioHelper, org.apache.commons.cli.CommandLine line) throws IllegalArgumentException, IOException
Given an IOHelper and a command line, check for required options and return a loaded input ontology. Currently handles --input and --input-iri options.- Parameters:
ioHelper
- the IOHelper to load the ontology withline
- the command line to use- Returns:
- the input ontology
- Throws:
IllegalArgumentException
- if requires options are missingIOException
- if the ontology cannot be loaded
-
getInputOntology
public static org.semanticweb.owlapi.model.OWLOntology getInputOntology(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String catalogPath) throws IllegalArgumentException, IOException
Given an IOHelper, a command line, and a path to a catalog, return an OWLOntology loaded from input or input-iri using the specified catalog (or null).- Parameters:
ioHelper
- the IOHelper to load the ontology withline
- the command line to usecatalogPath
- the catalog to use to load imports- Returns:
- the input ontology
- Throws:
IllegalArgumentException
- if requires options are missingIOException
- if the ontology cannot be loaded
-
getInputOntologies
public static List<org.semanticweb.owlapi.model.OWLOntology> getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, boolean allowEmpty) throws IllegalArgumentException, IOException
Given an IOHelper and a command line, check for required options and return a list of loaded input ontologies. Currently handles --input, --input-iri, and --inputs options.- Parameters:
ioHelper
- the IOHelper to load the ontology withline
- the command line to useallowEmpty
- if an empty list may be returned (when chaining commands, an input was already provided)- Returns:
- the list of input ontologies
- Throws:
IllegalArgumentException
- if requires options are missingIOException
- if the ontology cannot be loaded
-
updateInputOntology
public static CommandState updateInputOntology(IOHelper ioHelper, CommandState state, org.apache.commons.cli.CommandLine line) throws IllegalArgumentException
Given an IOHelper, a state object, and a command line, update the state with the ontology.- Parameters:
ioHelper
- the IOHelper to load the ontology withstate
- the input state, maybe nullline
- the command line to use- Returns:
- the updated state
- Throws:
IllegalArgumentException
- if requires options are missing
-
updateInputOntology
public static CommandState updateInputOntology(IOHelper ioHelper, CommandState state, org.apache.commons.cli.CommandLine line, boolean required) throws IllegalArgumentException
Given an IOHelper, a state object, a command line, and a "required" flag, update the state with the ontology. If the state contains an ontology, use it. If the state is null or does not contain an ontology, use the `--input` option. If the state has an ontology and there's an `--input` throw an exception warning the use to use two commands instead of a chain of commands.- Parameters:
ioHelper
- the IOHelper to load the ontology withstate
- the input state, maybe nullline
- the command line to userequired
- when true, throw an exception if ontology is not found- Returns:
- the updated state
- Throws:
IllegalArgumentException
- if requires options are missing
-
getOutputFile
public static File getOutputFile(org.apache.commons.cli.CommandLine line) throws IllegalArgumentException
Given a command line, check for the required options and return a File for saving data.- Parameters:
line
- the command line to use- Returns:
- the File for output; may be null; may not exist!
- Throws:
IllegalArgumentException
- if required options are not found
-
getOutputIRI
public static org.semanticweb.owlapi.model.IRI getOutputIRI(org.apache.commons.cli.CommandLine line)
Given a command line, check for the required options and return an IRI to be used as the OntologyIRI for the output ontology.- Parameters:
line
- the command line to use- Returns:
- the IRI for the output ontology, or null
-
maybeSaveOutput
public static void maybeSaveOutput(org.apache.commons.cli.CommandLine line, org.semanticweb.owlapi.model.OWLOntology ontology) throws IOException
Given a command line and an ontology, for each `--output` option (if any), save a copy of the ontology to the specified path.- Parameters:
line
- the command lien to useontology
- the ontology to save- Throws:
IOException
- on any problem
-
maybeCreateIRI
public static org.semanticweb.owlapi.model.IRI maybeCreateIRI(IOHelper ioHelper, String term, String field)
Try to create an IRI from a string input. If the term is not in a valid format (null), an IllegalArgumentException is thrown to prevent null from being passed into other methods.- Parameters:
ioHelper
- IOHelper to useterm
- the term to convert to an IRIfield
- the field in which the term was entered, for reporting- Returns:
- the new IRI if successful
-
getAddPrefixes
public static Map<String,String> getAddPrefixes(org.apache.commons.cli.CommandLine line) throws IOException
Given a command line, get a map of all prefixes to use and add to the output.- Parameters:
line
- the command line to use- Returns:
- a map of prefixes to add to output
- Throws:
IOException
- if the prefixes are not formatted correctly or a JSON file cannot be read
-
getTerms
public static Set<org.semanticweb.owlapi.model.IRI> getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line) throws IllegalArgumentException, IOException
Given an IOHelper and a command line, check for the required options and return a set of IRIs for terms. Handles --terms and --term-file options.- Parameters:
ioHelper
- the IOHelper to use for loading the termsline
- the command line to use- Returns:
- a set of term IRIs
- Throws:
IllegalArgumentException
- if the required options are not foundIOException
- if the term file cannot be loaded
-
getTerms
public static Set<org.semanticweb.owlapi.model.IRI> getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, boolean allowEmpty) throws IllegalArgumentException, IOException
As getTerms, but allow the list to be empty.- Parameters:
ioHelper
- the IOHelper to use for loading the termsline
- the command line to useallowEmpty
- true if empty lists of properties are allowed- Returns:
- a set of term IRIs
- Throws:
IllegalArgumentException
- if the required options are not foundIOException
- if the term file cannot be loaded
-
getTerms
public static Set<org.semanticweb.owlapi.model.IRI> getTerms(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String singles, String paths) throws IllegalArgumentException, IOException
Given an IOHelper and a command line, and the names of two options, check for the required options and return a set of IRIs for terms. Handles single term options and term-file options. Allows empty returns.- Parameters:
ioHelper
- the IOHelper to use for loading the termsline
- the command line to usesingles
- the option name for single terms, or nullpaths
- the option name for term file paths, or null- Returns:
- a set of term IRIs
- Throws:
IllegalArgumentException
- if the required options are not foundIOException
- if the term file cannot be loaded
-
getReasonerFactory
public static org.semanticweb.owlapi.reasoner.OWLReasonerFactory getReasonerFactory(org.apache.commons.cli.CommandLine line)
Given a string of a reasoner name from user input, return the reasoner factory. If the user input is not valid, throw IllegalArgumentExcepiton. By default, EMR is not allowed.- Parameters:
line
- the command line to use- Returns:
- OWLReasonerFactory if successful
-
getReasonerFactory
public static org.semanticweb.owlapi.reasoner.OWLReasonerFactory getReasonerFactory(org.apache.commons.cli.CommandLine line, boolean allowEMR)
Given a string of a reasoner name from user input, return the reasoner factory. If the user input is not valid, throw IllegalArgumentExcepiton.- Parameters:
line
- the command line to useallowEMR
- boolean specifying if EMR can be returned- Returns:
- OWLReasonerFactory if successful
-
printHelp
public static void printHelp(String usage, org.apache.commons.cli.Options options)
Print a help message for a command.- Parameters:
usage
- the usage information for the commandoptions
- the command line options for the command
-
printVersion
public static void printVersion() throws IOException
Print the ROBOT version- Throws:
IOException
- on issue getting info from JAR
-
getCommonOptions
public static org.apache.commons.cli.Options getCommonOptions()
Create a new Options object with shared options for 'help' and 'version'.- Returns:
- a new Options object with some options added
-
maybeGetCommandLine
public static org.apache.commons.cli.CommandLine maybeGetCommandLine(String usage, org.apache.commons.cli.Options options, String[] args, boolean stopAtNonOption) throws org.apache.commons.cli.ParseException, IOException
Parse the command line, handle help and other common options, and return null or a CommandLine.- Parameters:
usage
- the usage string for this commandoptions
- the command-line options for this commandargs
- the command-line arguments providedstopAtNonOption
- same as CommandLineParser- Returns:
- a new CommandLine object or null
- Throws:
org.apache.commons.cli.ParseException
- if the arguments cannot be parsedIOException
- on issue printing version
-
getCommandLine
public static org.apache.commons.cli.CommandLine getCommandLine(String usage, org.apache.commons.cli.Options options, String[] args) throws org.apache.commons.cli.ParseException, IOException
Parse the command line, handle help and other common options, (May exit!) and return a CommandLine.- Parameters:
usage
- the usage string for this commandoptions
- the command-line options for this commandargs
- the command-line arguments provided- Returns:
- a new CommandLine object or exit(0)
- Throws:
org.apache.commons.cli.ParseException
- if the arguments cannot be parsedIOException
- on issue printing version
-
handleException
public static void handleException(Exception exception)
Shared method for dealing with exceptions, printing help, and exiting. Currently prints the error message, stack trace (DEBUG), usage, and then exits.- Parameters:
exception
- the exception to handle
-
handleException
public static void handleException(String usage, org.apache.commons.cli.Options options, Exception exception)
Shared method for dealing with exceptions, printing help, and exiting. Currently prints the error message, stack trace (DEBUG), usage, and then exits.- Parameters:
usage
- the usage string for this command; WARN: not usedoptions
- the command-line options for this command; WARN: not usedexception
- the exception to handle
-
splitSelects
protected static List<String> splitSelects(String selects)
Given an input string, return a list of the string split on whitespace, while ignoring any whitespace in single string quotes.- Parameters:
selects
- String of select options to split- Returns:
- List of split strings
-
getInputOntologies
public static List<org.semanticweb.owlapi.model.OWLOntology> getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line) throws IllegalArgumentException, IOException
Given an IOHelper and a command line, check input options and return a list of loaded input ontologies.- Parameters:
ioHelper
- the IOHelper to load the ontology withline
- the command line to use- Returns:
- the list of input ontologies
- Throws:
IllegalArgumentException
- on bad patternIOException
- if the ontology cannot be loaded
-
getInputOntologies
public static List<org.semanticweb.owlapi.model.OWLOntology> getInputOntologies(IOHelper ioHelper, org.apache.commons.cli.CommandLine line, String catalogPath) throws IOException
Given an IOHelper, a command line, and the path to a catalog file, check input options and return a list of loaded input ontologies with the catalog file.- Parameters:
ioHelper
- the IOHelper to load the ontology withline
- the command line to usecatalogPath
- the catalog file to use- Returns:
- the list of input ontologies
- Throws:
IOException
- if the ontology cannot be loaded
-
-