# Tips: Factorize options between several analyses

{% hint style="danger" %}
This tip can only be achieved with a **Committed configuration**. See the [Configuration files](https://docs.ci.trust-in-soft.com/configuration-file) section to learn more about the different kinds of configuration.
{% endhint %}

{% hint style="success" %}
All examples in this section can be replayed with our [demo-caesar](https://github.com/TrustInSoft-CI/demo-caesar) repository, used for our [Introduction tutorial](https://docs.ci.trust-in-soft.com/tutorial). Feel free to fork this repository to try it by yourself.
{% endhint %}

## With the `include` option

The `"include"` option allows to inline another configuration object(s) where the `"include"` option is.

The `"include"` option requires one or several paths to other existing files. These files should only **only a single** configuration object:

```javascript
  "include": "another_config.json"
  // or
  "include": [ "another_config_1.json", "another_config_2.json" ]
```

For each option in the included configuration:

* If the option has not been declared **before** the `"include"`, it will be **added** to the parent configuration.
* If the option has already been declared *before* the special option `"include"`:
  * If the option type is a list of values (such as `"cpp-extra-args"` or `"files"`), the value of the option in the included configuration will be **concatenated** to the existing one of the parent configuration.
  * Otherwise, the value of the option in the included configuration will **replace** the value in the main configuration.

For any option declared *after* the `"include"` option in the parent configuration, the value of the option will either be added, be concatenated or will replace the one declared in the included configuration by following the same rules as above.

{% hint style="info" %}
The type of options can be checked in the [TrustInSoft Analyzer documentation](https://man.trust-in-soft.com/ref/tis-config.html#list-of-options).
{% endhint %}

{% hint style="info" %}
In the case of several included configuration, the first configuration to include is processed according to the above rules. Then the resulting configuration will be considered as the parent one for the second configuration to include, and so on...
{% endhint %}

{% hint style="success" %}
For a better clarity and readability of your analysis configuration objects, it is recommended to write any `"include"`option first and then the other options.

In this case, any option that is declared several times will either replace the included configuration value or be concatenated according to the option type.
{% endhint %}

{% code title=".trustinsoft/config.json" %}

```javascript
[
  {
    "include": "common.json",
    "name": "Test shift values 7 and -3",
    "cpp-extra-args": "-DFOO" // will be appended to "-I ."
  },
  {
    "include": "common.json",
    "name": "Test from program inputs",
    "main": "main_with_input", // will overwrite "main": "main"
    "val-program-name": "a.out",
    "val-args": "|People of Earth, your attention please|7"
  }
]
```

{% endcode %}

{% code title=".trustinsoft/common.json" %}

```javascript
{
    "prefix_path": "..",
    "files": [ "caesar.c", "main.c" ],
    "cpp-extra-args": "-I .",
    "main": "main"
}
```

{% endcode %}

## With a Build preparation script

Since the [Build preparation stage](https://docs.ci.trust-in-soft.com/configuration-file/build-preparation-script) allows to generate the `.trustinsoft/config.json` file, it can also be used to add options to any analysis configuration on the fly.

First, rename your existing `.trustinsoft/config.json` file into `.trustinsoft/orig_config.json` (or any other name, but make sure to modify the `INPUT_FILE` variable in the Build preparation script below).

Then, remove all the options you want to factorize for all the configuration objects.

{% code title=".trustinsoft/config.json" %}

```javascript
[
  {
    "name": "Test shift values 7 and -3",
  },
  {
    "name": "Test from program inputs",
    "main": "main_with_input",
    "val-program-name": "a.out",
    "val-args": "|People of Earth, your attention please|7"
  }
]

/* Options to factorize:

   "files": [ "caesar.c", "main.c" ]
   "cpp-extra-args": "-I ."
   "prefix_path": ".."
*/
```

{% endcode %}

Then, create the `.trustinsoft/prepare.sh` script (or adapt your existing one with the following script):

{% code title=".trustinsoft/prepare.sh" %}

```bash
#!/bin/bash

set -e

readonly INPUT_FILE="orig_config.json"
readonly COMMON_OPTIONS='{
   "files": [ "caesar.c", "main.c" ],
   "cpp-extra-args": "-I .",
   "prefix_path": ".."
}'

test -e "$INPUT_FILE" || { echo "$INPUT_FILE not found"; exit 1; }

minify --type js "$INPUT_FILE" \
  | jq --argjson common "$COMMON_OPTIONS" 'map($common + .)' \
  > config.json
```

{% endcode %}

Finally, update the `COMMON_OPTIONS` variable to add the options to add to each analysis configuration of `.trustinsoft/orig_config.json`, commit, push the changes and that's all.

{% hint style="info" %}
The`COMMON_OPTIONS` are concatenated in each analysis configuration before any options of your `.trustinsoft/orig_config.json`, hence any duplicated option will either be concatenated to the previous one (for options like`"files"` or`"cpp-extra-args"`) or will replace the previous one (for options like`"main"`or`"val-args"`).

See the [With the include option](#with-the-include-option) section for the exact rules when a same option is given several times.&#x20;
{% endhint %}
