# Using cling for scripting

**URL:** <https://root-forum.cern.ch/t/using-cling-for-scripting/51198>\
**Category:** Cling\
**Created:** [August 15, 2022, 5:55pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198 "2022-08-15T17:55:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimitry\_ishenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@dimitry\_ishenko](https://root-forum.cern.ch/u/dimitry_ishenko)\
**Post date:** [August 15, 2022, 5:55pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198/1 "2022-08-15T17:55:21Z")

</div>

I am experimenting with using cling as script interpreter, eg:

```c++
#!/usr/bin/cling

#include <iostream>
std::cout << "Hello world" << std::endl;

```

This works well, but I was wondering if there is a way to access args from within the script.

Is that possible?

---

<div class="post-metadata">

**Author:** ![dimitry\_ishenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@dimitry\_ishenko](https://root-forum.cern.ch/u/dimitry_ishenko)\
**Post date:** [August 15, 2022, 6:47pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198/2 "2022-08-15T18:47:31Z")

</div>

One way to do it would be through macros, eg:

_runme.cpp:_

```c++
#!/usr/bin/cling

#include <iostream>
std::cout << "The answer is " << x << std::endl;

```

And then:

```bash
user@laptop:~$ ./runme.cpp -Dx=42
The answer is 42

```

I suppose there is no way of passing args directly, as in:

```bash
user@laptop:~$ ./runme.cpp 42

```

---

<div class="post-metadata">

**Author:** ![dimitry\_ishenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@dimitry\_ishenko](https://root-forum.cern.ch/u/dimitry_ishenko)\
**Post date:** [August 15, 2022, 10:02pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198/3 "2022-08-15T22:02:14Z")

</div>

I ended up writing a small wrapper called _cliche_ to allow bash-like passing of args to a C++ script:

_cliche_

```bash
#!/bin/bash

escaped() {
    local p="$1"
    p=${p//$'\\'/\\\\} p=${p//$'\"'/\\\"}
    p=${p//$'\a'/\\a} p=${p//$'\b'/\\b} p=${p//$'\f'/\\f}
    p=${p//$'\n'/\\n} p=${p//$'\r'/\\r} p=${p//$'\t'/\\t} p=${p//$'\v'/\\v}
    echo "\"$p\""
}

size=$#
if ((size > 0)); then
    script="$1"
    shift
    
    args=$(escaped "$script")
    for arg in "$@"; do args+=", $(escaped "$arg")"; done
else
    script= args=
fi  

exec cling -std=c++17 -D"getargs()=std::array<const char*, $size>{ $args }" "$script"

```

Place _cliche_ into `/usr/bin` and then you can do this in your C++ script:

_runme.cpp_

```c++
#!/usr/bin/cliche

#include <array>
#include <iostream>

auto args = getargs();
for(auto n = 0; n < args.size(); ++n) {
    std::cout << "args[" << n << "] = " << args[n] << std::endl;
}

```

And pass any args to your script just like a bash script:

```bash
user@laptop:~$ ./runme.cpp foo bar baz
args[0] = ./runme.cpp
args[1] = foo
args[2] = bar
args[3] = baz

```

Share and enjoy.

D-mo

---

<div class="post-metadata">

**Author:** ![dimitry\_ishenko](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@dimitry\_ishenko](https://root-forum.cern.ch/u/dimitry_ishenko)\
**Post date:** [August 17, 2022, 6:59pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198/4 "2022-08-17T18:59:03Z")

</div>

As I’ve mentioned in my [other post](https://root-forum.cern.ch/t/cling-0-9-for-ubuntu-22-04/51220), I am now hosting Debian packages for Cling for Ubuntu 22.04 in my PPA: [ppa-verse/xeus-cling](https://launchpad.net/~ppa-verse/+archive/ubuntu/xeus-cling).

The package also includes the _cliche_ wrapper with a small man page for it.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 31, 2022, 6:59pm UTC](https://root-forum.cern.ch/t/using-cling-for-scripting/51198/5 "2022-08-31T18:59:29Z")

</div>

Closed on August 31, 2022, 6:59pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
