batocera-ES-onArch

Emulationstation from batocera with configs and scripts for integration as Arch-Linux package

This project is maintained by GB609

/opt/batocera-emulationstation/node_modules/cmdline/api.js

Overview

This file contains a generic utility to define command line actions + arguments for them, as well as the parser to map the input array into a more convenient structure.
It is also capable of some basic verifications to make sure required arguments were and - optionally - that they are of a certain type.
The idea of this utility was to separate handling the logic of argument parsing and validation from their definition to keep the command definitions as short and concise as possible. But it should also allow extended configuration when needed (rarely).

Basic flow:

  1. Define the action with action(optionSpec, handler).
    This will return a function mapping handler which takes care of the pre-processing.
  2. Call the mapper function with a raw array of arguments as coming from process.argv.
  3. The mapper uses processOptionConfig(optionSpec) to evaluate which options and arguments are expected.
    This yields an instance of [OptionConfig].
  4. The instance of OptionConfig will be passed to parseCmdLine(config, argv) to apply the configuration against the the actually passed console arguments. The resulting [ParsedOptionDict] and the list of transformed/filtered positional arguments are passed to the handler function.

See also

Index

class ParsedOptionDict

Any handler passed to api.action(optionSpec, handler) receives an instance of this class as first argument.
It’s mostly just a simple object, with a few convenience helpers for easier processing.

The keys used correspond to those of all named (=none-integer) options contained in optionSpec, but the values are converted according the the validators used (if any).

Example

 Input: api.action({ '--flag': 0 }, ...)
Output: { '--flag' : true|false }

class ParsedCmdLine

This is the main return value of parseCmdLine().
It is used internally in api.action() as intermediate class to transport the results of command line parsing.
Handler functions will get the contents of ParsedCmdLine.options as first argument and the entries of ParsedCmdLine.arguments unpacked into separate varArgs.

parseCmdLine

This function uses the preprocessed [OptionConfig] to parse and transform the actual command line.
Argument values are transformed according to the Validators specified in the initial optionSpec.

See also

VALIDATORS

This is a dictionary containing all known command line argument validators. These validators serve 2 purposes:

How to use:

But for most use cases, there is no need to use, touch or look at this dictionary.

Internally, validators can run in 2 different ‘modes’.

  1. ‘Name’ mode - The validator does not perform any action, but returns a string representation of its expectation/format.
    This is used in case of errors and when btc-config --help is used to print commands and their arguments.
  2. ‘Validation’ mode - Regular operation when command line arguments are evaluated.

The object VALIDATORS itself is a {Proxy} with an implementation of get() which returns bound functions. That way, argument configuration is attached to validators and the bound/pre-configured validators are used when parsing the command line.

argsRemaining

Check that the given number of arguments is still available to be parsed.
Configuration for optionSpec to use this is:

//simple variant - one argument
action({ optionName: # }, (options) => {})
//complex variant, allows to pass custom name prefix for the help function in addition to number of args
{ optionName: { argsRemaining: #, (#offset), string|function} }

_pseudoBind

Bind does not allow to change this, but the resulting validator must be able to receive another this.

processOptionConfig

This function unpacks and inteprets the optionSpec given to [action]. It receives the first argument passed to [action] and expects this to be a dictionary with a certain format.

{
<optionName> : validatorConfig|validatorName|typeSpec,
<#posInt>: validatorConfig|validatorName|typeSpec
#POS: int
}

Rules for keys:

Values:

Generated with shdoc from /opt/batocera-emulationstation/node_modules/cmdline/api.js