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 usenil
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 tomp.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)