Class 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 Optionals, 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 name PARAM in the module MODULE to the value VALUE. The way this passed value is intepreted is up to the MATSim ConfigGroup, 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.
    Some examples:
    • --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