aiida_vasp.commands.option_parser#
Module for reading options for cmd interface.
This module provides the OptionParser class for parsing command-line options
in various formats including hierarchical dot notation, JSON, and YAML files.
Key Features:
Hierarchical dot notation parsing (e.g.,
incar.elem=1,options.resources.num_machines=2)Automatic type conversion (int, float, bool, None, str)
JSON and YAML file loading support
Backward compatibility functions
Example Usage:
from aiida_vasp.commands.option_parser import OptionParser
# Parse hierarchical settings
result = OptionParser.parse_hierarchical_dict("incar.elem=1,debug.enabled=true,solver.method=None")
# Returns: {'incar': {'elem': 1}, 'debug': {'enabled': True}, 'solver': {'method': None}}
# Process various option formats
result = OptionParser.process_dict_option("config.json") # Load from file
result = OptionParser.process_dict_option('{"key": "value"}') # Parse JSON
result = OptionParser.process_dict_option("key=value,nested.key=123") # Parse hierarchical
Module Contents#
Classes#
Parser for command-line options supporting various formats including hierarchical dot notation. |
Functions#
Backward compatibility wrapper for OptionParser.process_dict_option. |
|
Backward compatibility wrapper for OptionParser.parse_hierarchical_dict. |
|
Backward compatibility wrapper for OptionParser.convert_value. |
API#
- class aiida_vasp.commands.option_parser.OptionParser[source]#
Parser for command-line options supporting various formats including hierarchical dot notation.
- static nested_dict()[source]#
Create a nested defaultdict that automatically creates missing levels.
- Returns:
A nested defaultdict instance
- Return type:
defaultdict
- classmethod process_dict_option(value: str | None) dict[source]#
Process an option that can be a JSON string or a file path.
- classmethod parse_hierarchical_dict(settings_str: str) dict[source]#
Parse hierarchical settings with dot notation into nested dictionaries.
Uses defaultdict for more concise code and automatic creation of nested levels.
- Parameters:
settings_str (str) – String containing comma-separated key=value pairs, where keys can use dot notation for nesting
- Returns:
Nested dictionary structure based on the dot notation
- Return type:
Examples:
>>> OptionParser.parse_hierarchical_dict( ... "incar.elem=1,relax_settings.algo=rd,options.resources.num_machines=1" ... ) {'incar': {'elem': 1}, 'relax_settings': {'algo': 'rd'}, 'options': {'resources': {'num_machines': 1}}} >>> OptionParser.parse_hierarchical_dict("debug.enabled=true,solver.tolerance=1e-6,mesh.nx=100") {'debug': {'enabled': True}, 'solver': {'tolerance': 1e-06}, 'mesh': {'nx': 100}}
- static convert_value(value: str)[source]#
Convert string value to appropriate Python type.
- Parameters:
value (str) – String value to convert
- Returns:
Converted value (int, float, bool, None, or str)
- Return type:
Supported conversions:
Integers:
"123"→123Floats:
"1.5"→1.5Booleans:
"true","yes","on","1"→TrueBooleans:
"false","no","off","0"→FalseNone:
"None","null","nil"→NoneStrings: Everything else remains as string
- static _defaultdict_to_dict(d)[source]#
Recursively convert defaultdict to regular dict.
- Parameters:
d – Input data structure to convert
- Returns:
Regular dictionary with all defaultdicts converted
- Return type:
dict or original type
- classmethod _parse_text_as_dict(resources_str: str) dict[source]#
Parse resources from various formats defined directly as a string.
- static _load_dict_from_file(overrides_path: pathlib.Path | str) dict[source]#
Load some settings from a file.
- aiida_vasp.commands.option_parser.process_dict_option(value: str | None) dict[source]#
Backward compatibility wrapper for OptionParser.process_dict_option.