Page MenuHomePhabricator

flag to suppress processing command line arguments
Closed, ResolvedPublic

Description

The command line arguments are processed by the activator of the org.mitk.gui.qt.ext plugin. This means, if any plugin depends on this one, it will be loaded and it will process the command line arguments.

However, you might want to process the arguments yourself, e.g. because you want to introduce switches or options. The org.mitk.gui.qt.ext activator considers all the arguments as image files or MITK scenes, so it will print errors for any other options. If your app does not depend on org.mitk.gui.qt.ext then it can process the arguments in one of its own plugin, and everything is fine. However, this MITK plugin provides several useful classes, e.g. a workbench advisor that you won't be able to reuse.

My suggestion is to introduce a flag that can be set in the application launcher main cpp file, and can be read out from the plugin context in the activator. If the flag is set to 'false' then the MITK plugin does not process the arguments. If undefined, or set to any other value then yes.

Merge request follows.

Related Objects

Mentioned Here
T1: testbug1

Event Timeline

I do not know if there is already a convention about how to name this kind of flags. Feel free to change the name. I mean if you consider to merge this at all. :-)

espak claimed this task.

I close this because I found a workaround that does not need MITK modifications.

Would you mind describing that workaround? That way we could point future users to it, if they have the same issue.

I had to introduce some command line options, e.g. for opening images with a given data node name, or setting properties of the images (e.g. level-window) or define source node-derived node relationships for the images to open.

You can define new options if you derive a class from mitk::BaseApplication and override the 'defineOptions()' function. You can bind the arguments to names, and then they will be put in the framework context, and you can access them from the plugin context. E.g. I have a '--perspective <perspective name>' option that I bound to a name, and I could access it as a property of the plugin context. It's gonna be a QVariant that stores a QString.

Any command line argument that matches a defined option will be processed and removed from the argument list, berry::Platform::GetApplicationArgs(). The activator of the org.mitk.gui.qt.ext plugin goes through these arguments and tries to open each of them as an image file or MITK scene.

So far so good.

My problem was that I could not define every supported option in this way. You can mark options as 'repeatable', but if you bind name to a repeatable option, the plugin property will contain a QString with the value for the last occurence of the option on the command line, and not a QStringList with every value.

E.g. with this command:

NiftyView --open T1:image.nii.gz --open mask:segmentation.nii.gz

the context will store only "mask:segmentation.nii" and not a string list with both values.

Since I did not see a solution for this, I did not define this option, but then the arguments were kept in berry::Platform::GetApplicationArgs(), and I had to pick them out from there, so I needed to suppress the argument processing in org.mitk.gui.qt.ext and do the processing myself.

The solution was this little function in my application class:

void BaseApplication::HandleRepeatableOption(const std::string& name, const std::string& value)
 {
   QString propertyName = "applicationArgs.";
   propertyName.append(QString::fromStdString(name));
 
   QStringList valueList = this->getProperty(propertyName).toStringList();
   valueList.append(QString::fromStdString(value));
   this->setProperty(propertyName, QVariant::fromValue(valueList));
 }

And then instead of using the bind function, I could use this handler:

Poco::Util::Option openOption("open", "o", "opens a file with the given name");
 openOption.argument("<name>:<file>").repeatable(true);
 openOption.callback(Poco::Util::OptionCallback<BaseApplication>(this, &BaseApplication::HandleRepeatableOption));
 options.addOption(openOption);

And then from the plugin activator I could access the '--open' option arguments in a QStringList (in a QVariant), from the 'applicationArgs.open' context property.

So, at the end, I could define every option through Poco, nothing was left mixed with the image file names on the path, so I could just let org.mitk.gui.qt.ext to process them, as it would do.

Thanks for the detailed walkthrough!