Class CommandLine
- java.lang.Object
-
- org.matsim.core.config.CommandLine
-
public class CommandLine extends Object
This class provides functionality to configure (MATSim) applications via the command line. While it can be used as a tool for configuration of any Java application in itself, it provides advanced functionality to directly modify the MATSim
Config
object.General usage
The command line interpreter is set up using the
CommandLine.Builder
:CommandLine cmd = new CommandLine.Builder(args) // .allowOptions("optionA", "optionB") // .requireOptions("outputPath") // .allowPositionalArguments(false).build();
The command line option can be accessed via safe getters, which return
Optional
s, or strict getters, which raise exceptions:int numberOfThreads = cmd.getOption("threads").map(Integer::parseInt).orElse(4); int numberOfThreads = Integer.parseInt(cmd.getOptionStrict("threads"));
As can be seen, options are always returned as strings. It is the task of the user to convert the arguments to the expected data types. They are given in one of the following ways:
- Value following the option name:
--threads 20
- With equals sign between:
--threads=20
MATSim usage
In order to configure MATSim with the command line, one needs to tell the interpreter to apply the command line options to the MATSim
Config
:CommandLine cmd = new CommandLine.Builder(args) // .allowPositionalArguments(false)// .build(); Config config = ConfigUtils.createConfig(); cmd.applyConfiguration(config);
This will interpret all command line options of the form
config:*
as options that are supposed to be inserted into the MATSim config. The rules are as follows:--config:MODULE.PARAM VALUE
sets a certain parameter with namePARAM
in the moduleMODULE
to the valueVALUE
. The way this passed value is intepreted is up to the MATSimConfigGroup
, just as if the value would have come from the configuration file.--config:MODULE.SET_TYPE[ID_PARAM=ID_VALUE].PARAM VALUE
sets a value in a specific parameter set, which is identified using a specific parameter in that set with a specific selection value.
--config:global.numberOfThreads 48
--config:strategy.strategysettings[strategyName=ReRoute].weight 0.0
--config:planCalcScore.scoringParameters[subpopulation=null].modeParams[mode=car].constant -3.5
- Author:
- Sebastian Hörl
- Value following the option name:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CommandLine.Builder
Fluent builder to create a command line interpreter for MATSim applications.static class
CommandLine.ConfigurationException
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
applyConfiguration(Config config)
Applies the command line configuration to a MATSimConfig
instance.Collection<String>
getAvailableOptions()
Returns a list of available named command line options.int
getNumberOfPositionalArguments()
Returns the number of positional (unnamed) arguments that have been passed to the application.Optional<String>
getOption(String option)
Returns a named command line option if it is present.String
getOptionStrict(String option)
Returns a named command line option if it is present, raises aCommandLine.ConfigurationException
otherwise.Optional<String>
getPositionalArgument(int index)
Returns the positional (unnamed) command line argument with indexindex
if present.List<String>
getPositionalArguments()
Returns the positional (unnamed) arguments that have been passed to the application.String
getPositionalArgumentStrict(int index)
Returns the positional (unnamed) command line argument with indexindex
if present or raises aCommandLine.ConfigurationException
.boolean
hasOption(String option)
Returns whether a certain named command line option is present.
-
-
-
Method Detail
-
getNumberOfPositionalArguments
public int getNumberOfPositionalArguments()
Returns the number of positional (unnamed) arguments that have been passed to the application.
-
getPositionalArguments
public List<String> getPositionalArguments()
Returns the positional (unnamed) arguments that have been passed to the application.
-
getPositionalArgument
public Optional<String> getPositionalArgument(int index)
Returns the positional (unnamed) command line argument with indexindex
if present.
-
getPositionalArgumentStrict
public String getPositionalArgumentStrict(int index) throws CommandLine.ConfigurationException
Returns the positional (unnamed) command line argument with indexindex
if present or raises aCommandLine.ConfigurationException
.
-
getAvailableOptions
public Collection<String> getAvailableOptions()
Returns a list of available named command line options.
-
hasOption
public boolean hasOption(String option)
Returns whether a certain named command line option is present.
-
getOption
public Optional<String> getOption(String option)
Returns a named command line option if it is present.
-
getOptionStrict
public String getOptionStrict(String option) throws CommandLine.ConfigurationException
Returns a named command line option if it is present, raises aCommandLine.ConfigurationException
otherwise.
-
applyConfiguration
public void applyConfiguration(Config config) throws CommandLine.ConfigurationException
Applies the command line configuration to a MATSimConfig
instance. SeeCommandLine
for more information on the syntax.
-
-