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

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


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:

<?xml version="1.0" encoding="utf-8"?>
  <description>module to do things</description>
    <description>Input/output parameters</description>
      <label>sequence filename</label>
      <description>filename of sequence to be processed</description>
      <label>write file</label>
      <description>write output nrrd file</description>

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:

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/', '--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/", 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,


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

Also this looks like a bug:

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

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

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