Skip to main content

Function: parseKnownParameters()

parseKnownParameters(options?: { args: string[]; defaults: Parameters; exitOnError: boolean; usage: string; }): [Parameters, string[]]

Parses OptalCP solver parameters from the command line, passing through unrecognized arguments.

Parameters

ParameterTypeDescription
options?{ args: string[]; defaults: Parameters; exitOnError: boolean; usage: string; }-
options.args?string[]Command-line arguments to parse. Defaults to process.argv.slice(2)
options.defaults?ParametersDefault parameter values. CLI arguments override these
options.exitOnError?booleanIf true (default), exits on error or --help. If false, throws an exception instead
options.usage?stringCustom usage text shown before the parameter list when --help is used

Returns

[Parameters, string[]]

A tuple of the parsed parameters and an array of unrecognized arguments

Remarks

This function parses OptalCP solver parameters from the command line and returns both a Parameters object and an array of unrecognized arguments.

Instead of hardcoding solver settings like time limits or worker counts in your code, you can let users configure them when running your application. This variant is particularly useful when your application accepts its own arguments (like input file names) alongside solver parameters. For example, running node solve.js --timeLimit 120 input.txt would parse --timeLimit 120 as a solver parameter and return input.txt as an unrecognized argument for your code to handle.

The defaults option lets you specify sensible default values for your application. When users don't provide a parameter on the command line, the default value is used.

By default (exitOnError: true), parse errors and --help/--optalcpVersion flags cause the process to exit. Set exitOnError: false to throw exceptions instead.

If --help or -h is given, the function prints help starting with the usage option (if provided), followed by the list of recognized parameters:

WorkerParameters can be specified for individual workers using --workerN. prefix. For example, --worker0.searchType FDS sets the search type for the first worker only.

import * as cp from "@scheduleopt/optalcp";

// Parse solver parameters, collect input files as unrecognized args
const [params, inputFiles] = cp.parseKnownParameters({
defaults: { timeLimit: 60 },
usage: "Usage: node solve.js [OPTIONS] <input-file>..."
});

for (const file of inputFiles) {
const model = loadModel(file);
await model.solve(params);
}

See