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#

OptionParser

Parser for command-line options supporting various formats including hierarchical dot notation.

Functions#

process_dict_option

Backward compatibility wrapper for OptionParser.process_dict_option.

parse_hierarchical_dict

Backward compatibility wrapper for OptionParser.parse_hierarchical_dict.

convert_value

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.

Parameters:

value (str or None) – String value that can be JSON, file path, or hierarchical key=value pairs

Returns:

Parsed dictionary from the input value

Return type:

dict

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:

dict

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:

int or float or bool or None or str

Supported conversions:

  • Integers: "123"123

  • Floats: "1.5"1.5

  • Booleans: "true", "yes", "on", "1"True

  • Booleans: "false", "no", "off", "0"False

  • None: "None", "null", "nil"None

  • Strings: 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.

Parameters:

resources_str (str) – String to parse (JSON or key=value format)

Returns:

Parsed dictionary

Return type:

dict

static _load_dict_from_file(overrides_path: pathlib.Path | str) dict[source]#

Load some settings from a file.

Parameters:

overrides_path (Path or str) – Path to the file containing settings

Returns:

Dictionary loaded from file

Return type:

dict

Raises:

json.JSONDecodeError, yaml.YAMLError for malformed files

aiida_vasp.commands.option_parser.process_dict_option(value: str | None) dict[source]#

Backward compatibility wrapper for OptionParser.process_dict_option.

Parameters:

value (str or None) – String value that can be JSON, file path, or hierarchical key=value pairs

Returns:

Parsed dictionary from the input value

Return type:

dict

aiida_vasp.commands.option_parser.parse_hierarchical_dict(settings_str: str) dict[source]#

Backward compatibility wrapper for OptionParser.parse_hierarchical_dict.

Parameters:

settings_str (str) – String containing comma-separated key=value pairs

Returns:

Nested dictionary structure based on the dot notation

Return type:

dict

aiida_vasp.commands.option_parser.convert_value(value: str)[source]#

Backward compatibility wrapper for OptionParser.convert_value.

Parameters:

value (str) – String value to convert

Returns:

Converted value (int, float, bool, None, or str)

Return type:

int or float or bool or None or str