Load Rougail configuration from Rougail command line informations ================================================================== There is a lot you can do with the Rougail command line (rougail-cli), but sometimes you need to do a more advanced script. Rather than duplicating the configuration, why not load the information from the configuration file, environment variables, or command line options? We can loading a combination of source information but always in this order: - configuration file - environment variables - commandline options .. warning:: specific options reserve for command line (in namespace "cli") are not available in script Then let's create an structual file:term:`structure file` :file:`dist/00-base.yml`: .. code-block:: yaml :caption: the :file:`dist/00-base.yml` file content \%YAML 1.2 --- version: 1.1 my_variable: my_value_extra # a simple variable ... Command line configuration file ------------------------------- Create a command line configuration file :file:`.rougailcli.yml`: .. code-block:: yaml :caption: the :file:`.rougailcli.yml` file content --- main_structural_directories: # directories where are place structural file - dist step.output: json # output is not console but json Let's execute Rougail command line: .. code-block:: bash $ rougail { "my_variable": "my_value_extra" } Then, let's create the :term:`Tiramisu` objects via the following :file:`script.py` script: .. code-block:: python :caption: the :file:`script.py` file content from rougail import Rougail rougail = Rougail() try: config = rougail.run() print(config.value.get()) except Exception as err: print(f"ERROR: {err}") exit(1) Let's execute `script.py`: .. code-block:: bash $ python3 script.py ERROR: option "Directories where structural files are placed" is mandatory but has no value As expected, the .rougailcli.yml file is not loaded because it is specific to the command line. Let's modifying the script to do this: .. code-block:: python :caption: the :file:`script.py` file content from rougail import Rougail, RougailConfig from rougail.cli.rougailconfig import load load(RougailConfig, yaml_file=".rougailcli.yml") rougail = Rougail() try: config = rougail.run() print(config.value.get()) except Exception as err: print(f"ERROR: {err}") exit(1) Let's execute `script.py`: .. code-block:: bash $ python3 script.py {: {: 'my_value_extra'}} Environment variables --------------------- If we don't have .rougailcli.yml, it's possible to set option with environment variables, like this: .. code-block:: bash $ env ROUGAILCLI_MAIN_STRUCTURAL_DIRECTORIES=dist/ ROUGAILCLI_STEP.OUTPUT=json ROUGAILCLI_MAIN_NAMESPACE=test bin/rougail { "test": { "my_variable": "my_value_extra" } } Do the same with a script: .. code-block:: python :caption: the :file:`script.py` file content from rougail import Rougail, RougailConfig from rougail.cli.rougailconfig import load load(RougailConfig, env_prefix="ROUGAILCLI") rougail = Rougail() try: config = rougail.run() print(config.value.get()) except Exception as err: print(f"ERROR: {err}") exit(1) Let's execute `script.py`: .. code-block:: bash $ env ROUGAILCLI_MAIN_STRUCTURAL_DIRECTORIES=dist/ ROUGAILCLI_STEP.OUTPUT=json ROUGAILCLI_MAIN_NAMESPACE=test python3 script.py {: {: 'my_value_extra'}} Command line option ------------------- To reproduce this: .. code-block:: bash ./bin/rougail --main_structural_directories dist/ --step.output json --main_namespace=new_test { "new_test": { "my_variable": "my_value_extra" } } Do this script: .. code-block:: python :caption: the :file:`script.py` file content from rougail import Rougail, RougailConfig from rougail.cli.rougailconfig import load load(RougailConfig, commandline=True) rougail = Rougail() try: config = rougail.run() print(config.value.get()) except Exception as err: print(f"ERROR: {err}") exit(1) .. code-block:: bash $ python3 script.py --main_structural_directories dist/ --step.output json --main_namespace=new_test {: {: 'my_value_extra'}