Skip to content

Instantly share code, notes, and snippets.

@ChrisK2
Created May 22, 2014 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisK2/91c6f48e984f02cd789f to your computer and use it in GitHub Desktop.
Save ChrisK2/91c6f48e984f02cd789f to your computer and use it in GitHub Desktop.

mp.option

mpv comes with a built-in module to manage options from config-files and the command-line. All you have to do is to supply a table with default options to the read_options function. The function will overwrite the default values with values found in the config-file and the command-line (in that order).

read_options(table, identifier)

A table with key-value pairs. The type of the default values is important for converting the values read from the config file or command-line back. Do not use nil as a default value!

The identifier is used to identify the config-file and the command-line option. These needs to unique to avoid collisions with other scripts. Defaults to mp.get_script_name().

Example implementation:

local opt = require 'mp.options'
local options = {
    optionA = "defaultvalueA",
    optionB = -0.5,
    optionC = true,
}
read_options(options, "myscript")
print(option.optionA)

The config file will be stored in lua-settings/identifier.conf in mpv's user folder. Comment lines can be started with # and stray spaces are not removed. Boolean values will be represented with yes/no.

Example config:

# comment
optionA=Hello World
optionB=9999
optionC=no

Command-line options are read from the --lua-opts parameter. To avoid collisions, all keys have to be prefixed with identifier-.

Example command-line:

--lua-opts=myscript-optionA=TEST:myscript-optionB=0:myscript-optionC=yes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment