# Can't input boolean into module being wrong as a cli

**URL:** <https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534>\
**Category:** Support\
**Tags:** cli\
**Created:** [July 11, 2019, 3:44pm UTC](https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534 "2019-07-11T15:44:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrojasUNC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jrojasunc/32/4143_2.png) [@jrojasUNC](https://discourse.slicer.org/u/jrojasUNC)\
**Post date:** [July 11, 2019, 3:44pm UTC](https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534/1 "2019-07-11T15:44:16Z")

</div>

Operating system: Windows 10 64 bit  
Slicer version:4.11 nightly  
Expected behavior:  
Actual behavior:

Hi,

I’m trying to run my own scripted module as a cli. I can input a string into the module just fine, but when I try to input a boolean, the long flag appears in the argv but not the value.

My xml file looks like this:

```python
<?xml version="1.0" encoding="utf-8"?>
<executable>
  <category>filtering</category>
  <title>MyModule</title>
  <description>module to do things</description>
  <version>1.0</version>
  <parameters>
    <label>IO</label>
    <description>Input/output parameters</description>
    <string>
      <longflag>filename</longflag>
      <label>sequence filename</label>
      <channel>input</channel>
      <description>filename of sequence to be processed</description>
    </string>
    <boolean>
      <longflag>writefile</longflag>
      <label>write file</label>
      <channel>input</channel>
      <description>write output nrrd file</description>
    </boolean>
  </parameters>
</executable>

And my python file looks like:

#!/usr/bin/env python-real
# -*- coding: utf-8 -*-

import sys

if __name__ == ' __main__':

    FILENAME = sys.argv[sys.argv.index('--filename') + 1]
    WRITEFILE = sys.argv[sys.argv.index('--writefile') + 1]

    # Do stuff

```

In slicer, I add the additional module path and run the following code:

```python
parameters = {}
parameters["filename"] = "fullFilenameOfImage"
parameters["writefile"] = True
cli_node = slicer.cli.runSync(slicer.modules.mycli, None, parameters)

```

if I add a print statement to my py file to print argv and I use `cli_node.GetOutputText()`, I get:  
`MyModule standard output:\n\n['path/myCLI.py', '--filename', 'fullFilenameOfImage', '--writefile']\r\n`

The long flag is displayed but there is no value after it so when I do `cli_node.GetErrorText()` I get:  
`MyModule standard error:\n\nTraceback (most recent call last):\r\n File "path/myCLI.py", line 16, in <module>\r\n WRITEFILE = sys.argv[sys.argv.index(\'--writefile\') + 1]\r\nIndexError: list index out of range\r\n`

I tried `parameters["writefile"] = slicer.util.toBool(True)` and `slicer.util.toBool("true")` but I get the same result.

Thank you for your help,

Juan

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 11, 2019, 4:13pm UTC](https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534/2 "2019-07-11T16:13:56Z")

</div>

What you describe is the correct behavior. If you check the “write file” checkbox then `--writefile` will be added to the command-line argument list, if unchecked then the `--writefile` will not be added. You can check the flag like this:

```
writeFile = '--writefile' in sys.argv
```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 11, 2019, 4:25pm UTC](https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534/3 "2019-07-11T16:25:41Z")

</div>

Also this looks like a bug:

```auto
WRITEFILE = sys.argv[sys.argv.index('--writefile') + 1]

```

since writefile is a boolean it won’t have an extra argument.

---

<div class="post-metadata">

**Author:** ![jrojasUNC](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jrojasunc/32/4143_2.png) [@jrojasUNC](https://discourse.slicer.org/u/jrojasUNC)\
**Post date:** [July 11, 2019, 4:39pm UTC](https://discourse.slicer.org/t/cant-input-boolean-into-module-being-wrong-as-a-cli/7534/4 "2019-07-11T16:39:27Z")

</div>

I see. I didn’t see that in the wiki. Thank you both!
