Parser

argparse.core.Parser$
object Parser

Low-level parsing functionality. See ArgumentParser for a user-friendly API.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Parser.type

Members list

Concise view

Type members

Classlikes

case object Continue extends ParamResult

Continue parsing of the next argument.

Continue parsing of the next argument.

Attributes

Graph
Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Self type
case class ParamDef(names: Seq[String], parseAndSet: (String, Option[String]) => ParamResult, missing: () => Unit, isFlag: Boolean, repeatPositional: Boolean, endOfNamed: Boolean)

A parameter definition is the low-level building block to define the grammar of a command-line and its functionality.

A parameter definition is the low-level building block to define the grammar of a command-line and its functionality.

ParamDefs associate parameter names to actions that are invoked by Parser.parse().

Attributes

endOfNamed

Treat all subsequent parameters as positionals, regardless of their name. This can be useful for constructing nested commands.

isFlag

Indicates if this named parameter is a flag, i.e. one that never accepts an argument. In case its name is encountered, its value is set to "true". Has no effect on positional parameters.

missing

A function that is invoked if this parameter has not been encountered at all.

names

All names that may be used by this parameter. If a name starts with -, it is considered a "named" parameter, otherwise it is considered a "positional" parameter. Arguments associated to named parameters may appear in any order on the command line, as long as they are prefixed by the parameter's name. Positional parameters are given arguments in the order they appear in. Special case for single-letter named parameters: one argument may specify multiple named parameters of a single letter. In this case, letters are read left to right, and the first non-flag parameter will consume the remaining letters. E.g. assume -i and -t are flags, and -I takes an argument, then -itIhello sets -i and -t and assigns hello to -I.

parseAndSet

A function that is invoked anytime this parameter is encountered on the command line. In case of a named param, the first element is the actual name used, and the second element is the argument or None if no argument followed. In case of a positional param, the parameter's first name is given and the argument value is always defined. This function must return either a Continue or Stop. The former will instruct the parser to continue parsing, whereas the latter will prevent any further argument parsing.

repeatPositional

If this is a positional parameter, the parser will repeat it indefinitely.

Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
sealed trait ParamResult

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object Continue.type
object Stop.type
case object Stop extends ParamResult

Stops parsing in its track. No further arguments will not be parsed.

Stops parsing in its track. No further arguments will not be parsed.

Attributes

Graph
Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Self type
Stop.type

Value members

Concrete methods

def parse(params: Seq[ParamDef], args: Iterable[String], reportUnknown: String => Unit): Boolean

Parse command line arguments according to some parameter definitions.

Parse command line arguments according to some parameter definitions.

The parser works in two passes.

  1. the first pass goes over all actual arguments and groups them into positional and named ones (and also detects any unknown arguments)

  2. the second pass then iterates over all parameter definitions, looks up the corresponding value from the previous pass, and calls the relevant functions of the parameter definition

Delegating parameter invocation to a second pass allows for them to be evaluated in order of definition, rather than order of appearance on the command line. This is important to allow "breaking" parameters such as --help to be on a command line with other "side-effecting" params, but yet avoid executing part of the command line (of course this example assumes that the --help parameter was defined before any others).

Attributes

args

the actual command-line arguments

params

the sequence of parameter definitions

reportUnknown

a function invoked when an extranous argument is encountered. An extranous argument can be either an unknown named argument, or a superfluous positional argument

Returns:

true if all arguments were parsed, false otherwise. In other words, returns true if no Stop was encountered while calling parameters' parseAndSet functions.