YadOpt Documentation
Miscellaneous
Merge two YadOptArgs objects
The YadOptArgs class, which is the return value of the yadopt.parse function, supports the merge operator |, similar to Python dictionaries. This operator combines the two given YadOptArgs objects into a new one. In case of a key conflict, the values from the right-hand operand are used.
This merge operator is particularly useful for implementing a feature to read an existing configuration file (for example, a file specified with the --config argument) and then overwrite those settings with values explicitly provided in the command line arguments. For example:
"""
Train a neural network model.
Arguments:
config_path Path to base config file.
Training options:
--epochs INT The number of training epochs. [default: 100]
--model STR Neural network model name. [default: mlp]
--lr FLT Learning rate. [default: 1.0E-3]
"""
import yadopt
if __name__ == "__main__":
# Parse the command line arguments.
args = yadopt.parse(__doc__)
# Load the base config file.
args_base = yadopt.load(args.config_path)
# Update the parsed command line arguments.
args_updated = args_base | args
print(args_updated)
Backward compatibility of the load functions
The older versions of YadOpt (<= 2026.1.5) used a different TOML/JSON format in the save and load functions. The content of the TOML/JSON file was fundamentally different; older versions saved help messages and argument vector before parsing, and re-parsed them when loading. This can be seen as a YadOpt-style approach in which the help message is treated as the single source of truth. However, it makes it impossible to save YadOptArgs after operations such as merging | or group extraction yadopt.get_group, and it also fails to guarantee reproducibility if there is a bug in YadOpt’s parsing functionality (and there is no such thing as completely bug-free software, if anything, only TeX might come close). Therefore, the current version of YadOpt has shifted to an approach that directly stores the parsed argument vector, making the saving of the help message and argument vector optional. These are included in the saved file only when YadOptArgs is saved immediately after parsing, and they are not included if YadOptArgs is saved after performing operations such as merging or group extraction.