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:

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:

<?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:

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

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!