SettingsParser

Companion
object
class Object
trait Matchable
class Any

Value members

Inherited constructors

def this(description: String, enableHelpFlag: Boolean, enableBashCompletionFlag: Boolean, stdout: PrintStream, stderr: PrintStream)
Inherited from
ArgumentParser

Concrete methods

inline def mutableSettings[A](a: A): A

EXPERIMENTAL

EXPERIMENTAL

Add parameter definitions for all vars in a class and nested objects.

Variables will be set when a corresponding argument is encountered on the commandline. The rules are as follows:

  • Every variable will be assigned a named parameter (starting with '--')

  • The named parameter is optional. If it is not encountered on the command line, the variable's default value will be used (since this method requires a class instance, variables cannot be abstract).

  • For every variable of type A, an implicit Reader[A] must be available.

  • Boolean variables are parsed as flags.

Example:

class Settings {
 var opt1: String = "default1"
 var opt2: Int = 42
 var enableSystemA: Boolean = false

 object http {
   var host: String = "localhost"
   var port: Int = 2020
 }

 object monitoring {
   var host: String = "localhost"
   var port: Int = 3030
 }
}

def main(args: Array[String]): Unit = {
 val parser = argparse.ArgumentParser()

 val settings = parser.mutableSettings(Settings())

 println("before")
 println(settings) // all default values

 parser.parse(Array(
   "--opt1", "value1", "--enable-system-a", "--monitoring.port", "3031"
 ))

 println("after")
 println(settings) // opt1, enableSystemA and monitoring.port have been changed
}
inline def settings[A](name: String): () => A

EXPERIMENTAL

EXPERIMENTAL

Add arguments for case-class fields (recursively).

Inherited methods

def addParamDef(pdef: ParamDef): Unit

Low-level escape hatch for manually adding parameter definitions.

Low-level escape hatch for manually adding parameter definitions.

See also param, requiredParam and repeatedParam for the high-level API.

Inherited from
ArgumentParser
def addParamInfo(pinfo: ParamInfo): Unit

Low-level escape hatch for manually adding parameter information.

Low-level escape hatch for manually adding parameter information.

See also param, requiredParam and repeatedParam for the high-level API.

Inherited from
ArgumentParser
def command(name: String, action: Seq[String] => Unit, description: String): Unit

Utility to define a sub command.

Utility to define a sub command.

Many modern command line apps actually consist of multiple nested commands, each corresponding to the verb of an action, such as 'run' or 'clone'. Typically, each sub command also has its own dedicated parameters list.

In argparse, subcommands can easily be modelled by a positional parameter that represents the command, followed by a repeated, all-absorbing parameter which represents the command's arguments. However, since this pattern is fairly common, this method is provided as a shortcut.

Value Params
action

a function called with the remaining arguments after this command. Note that you may reference an Arg's value in the action.

name

the name of the command

Inherited from
ArgumentParser
def help(): String

A default help message, generated from parameter help strings.

A default help message, generated from parameter help strings.

Inherited from
ArgumentParser
def param[A](name: String, default: => A, env: String, aliases: Seq[String], help: String, flag: Boolean, absorbRemaining: Boolean, completer: String => Seq[String], bashCompleter: BashCompleter)(implicit reader: Reader[A]): () => A

Define an optional parameter, using the given default value if it is not supplied on the command line or by an environment variable.

Define an optional parameter, using the given default value if it is not supplied on the command line or by an environment variable.

ErgoTip: always give named parameters a default value.

''Internal design note: param and requiredParam differ only in the presence of the 'default' parameter. Ideally, they would be merged into one single method, giving the 'default' parameter a default null value (as is done for the other optional parameters, such as 'env' and 'help'). Unfortunately, since 'default' is of type A where A may be a primitive type, it cannot be assigned null. The usual solution would be to wrap it in an Option type, but that leads to an ugly API. Hence the method is split into two. See addParam() for the common denominator.''

Type Params
A

The type to which an argument shall be converted.

Value Params
absorbRemaining

Indicates that any arguments encountered after this parameter must be treated as positionals, even if they start with -. In other words, a parameter marked with this has the same effect as the -- separator. It can be useful for implementing sub-commands. (Note however that this ArgumentParser has a dedicated command method for such use cases)

aliases

Other names that may be used for this parameter. This is a good place to define single-character aliases for frequently used named parameters. Note that this has no effect for positional parameters.

completer

A bash snippet that is inserted in bash-completions, responsible for setting completion options for this param. If omitted, the parameter type's (A) default completer will be used. If present, this must be valid bash and should set COMPREPLY. The bash variable "$cur" may be used in the snippet, and will contain the current word being completed for this parameter.

default

The default value to use in case no matching argument is provided.

env

The name of an environment variable from which to read the argument in case it is not supplied on the command line. Set to 'null' to ignore.

flag

Set to true if the parameter should be treated as a flag. Flags are named parameters that are treated specially by the parser:

  • they never take arguments, unless the argument is embedded in the flag itself
  • they are always assigned the string value "true" if found on the command line Note that flags are intended to make it easy to pass boolean parameters; it is quite rare that they are useful for non-boolean params. The flag field has no effect on positional parameters.
help

A help message to display when the user types --help

name

The name of the parameter. A name starting with - indicates a named parameter, whereas any other name indicates a positional parameter. Prefer double-dash named params. I.e. prefer "--foo" over "-foo".

Returns

A handle to the parameter's future value, available once parse(args) has been called.

Inherited from
ArgumentParser
def parseOrExit(args: Iterable[String]): Unit

Parse the given arguments with respect to the parameters defined by param, requiredParam, repeatedParam and command.

Parse the given arguments with respect to the parameters defined by param, requiredParam, repeatedParam and command.

In case no errors are encountered, the arguments will be populated in the functions returned by the parameter definitions.

In case errors are encountered, the default behaviour is to exit the program.

The classes of errors are:

  1. An unknown argument is encountered. This can either be an unspecified named argument or an extranous positional argument.

  2. A required argument is missing.

  3. An argument cannot be parsed from its string value to its desired type.

See also

parseResult for a version of this function which does not exit

Inherited from
ArgumentParser
def parseResult(args: Iterable[String], env: Map[String, String]): Result

Parse the given arguments with respect to the parameters defined by param, requiredParam, repeatedParam and command.

Parse the given arguments with respect to the parameters defined by param, requiredParam, repeatedParam and command.

Inherited from
ArgumentParser
def printBashCompletion(programName: String): Unit
Inherited from
ArgumentParser
def repeatedParam[A](name: String, aliases: Seq[String], help: String, flag: Boolean, completer: String => Seq[String], bashCompleter: BashCompleter)(implicit reader: Reader[A]): () => Seq[A]

Define a parameter that may be repeated.

Define a parameter that may be repeated.

Note that all named parameters may always be repeated, regardless if they are defined as repeated or not. The difference is that for non-repeat-defined parameters the last value is used, whereas repeat-defined parameters accumulate values. (This is why repeatedParam takes an A but gives back a Seq[A], while other params take A and give back A).

E.g. consider the command line --foo=1 --foo=2 --foo=3

In case foo is a regular named parameter, then, after parsing, the value will be 3. In case it is defined as a repeating parameter, its value will be Seq(1,2,3).

Repeated positional parameters consume all remaining positional command line arguments.

Inherited from
ArgumentParser
def requiredParam[A](name: String, env: String, aliases: Seq[String], help: String, flag: Boolean, absorbRemaining: Boolean, completer: String => Seq[String], bashCompleter: BashCompleter)(implicit reader: Reader[A]): () => A

Define a required parameter.

Define a required parameter.

This method is similar to param, except that it does not accept a default value. Instead, missing arguments for this parameter will cause the parser to fail.

ErgoTip: avoid named parameters that are required. Only require positional parameters.

See also

param

Inherited from
ArgumentParser
def singleParam[A](name: String, default: Option[() => A], env: Option[String], aliases: Seq[String], help: String, flag: Boolean, absorbRemaining: Boolean, completer: Option[String => Seq[String]], bashCompleter: Option[BashCompleter])(implicit reader: Reader[A]): () => A
Inherited from
ArgumentParser

Deprecated and Inherited methods

@deprecated("use parseOrExit instead", "0.7.2")
def parse(args: Array[String]): Unit
Deprecated
Inherited from
ArgumentParser
@deprecated("use parseOrExit instead", "0.7.2")
def parse(args: Iterable[String]): Unit
Deprecated
Inherited from
ArgumentParser

Inherited fields

var commandInfos: ListBuffer[CommandInfo]
Inherited from
ArgumentParser
val description: String
Inherited from
ArgumentParser
Inherited from
ArgumentParser
val enableHelpFlag: Boolean
Inherited from
ArgumentParser
val paramInfos: ListBuffer[ParamInfo]
Inherited from
ArgumentParser
val stderr: PrintStream
Inherited from
ArgumentParser
val stdout: PrintStream
Inherited from
ArgumentParser