Browse Source

Add -arg switch to set current working dir in command line and GUI.

This sets the working directory natively, which is used to read or
write files specified with relative locations. For example:

  prism -dir /some/dir model.prism model.props

is the same as:

  prism /some/dir/model.prism /some/dir/model.props

Exported files also go to the same directory.

The same applies when passing model/property files to the GUI via
the command-line and when specifying export filenames as options.
The GUI file chooser is also initialised to the -dir location.
accumulation-v4.7
Dave Parker 6 years ago
parent
commit
67adfc9ae7
  1. 12
      prism/src/prism/PrismCL.java
  2. 70
      prism/src/userinterface/GUIPrism.java

12
prism/src/prism/PrismCL.java

@ -1137,6 +1137,17 @@ public class PrismCL implements PrismModelListener
printVersion();
exit();
}
// set working directory
else if (sw.equals("dir")) {
if (i < args.length - 1) {
String workingDir = args[++i];
if (PrismNative.setWorkingDirectory(workingDir) != 0) {
errorAndExit("Could not change working directory to " + workingDir);
}
} else {
errorAndExit("No property specified for -" + sw + " switch");
}
}
// load settings
else if (sw.equals("settings")) {
if (i < args.length - 1) {
@ -2394,6 +2405,7 @@ public class PrismCL implements PrismModelListener
mainLog.println();
mainLog.println("-help .......................... Display this help message");
mainLog.println("-version ....................... Display PRISM version info");
mainLog.println("-dir <dir> ..................... Set current working directory");
mainLog.println("-settings <file>................ Load settings from <file>");
mainLog.println();
mainLog.println("-pf <props> (or -pctl or -csl) . Model check properties <props>");

70
prism/src/userinterface/GUIPrism.java

@ -69,6 +69,7 @@ import prism.Prism;
import prism.PrismException;
import prism.PrismFileLog;
import prism.PrismLog;
import prism.PrismNative;
import userinterface.util.GUIComputationEvent;
import userinterface.util.GUIEvent;
import userinterface.util.GUIEventHandler;
@ -116,10 +117,10 @@ public class GUIPrism extends JFrame
//Show the splash screen
splash = new GUIPrismSplash("images/splash.png");
splash.display();
gui = new GUIPrism();
gui = new GUIPrism(args);
gui.setVisible(true);
EventQueue.invokeLater(new GUIPrism.SplashScreenCloser());
gui.passCLArgs(args);
gui.passCLArgs();
} catch (GUIException e) {
System.err.println("Error: Could not load the PRISM GUI: " + e.getMessage());
System.exit(1);
@ -178,6 +179,11 @@ public class GUIPrism extends JFrame
//properties
private Prism prism;
private PrismLog theLog;
// command-line args to be passed on to components
private String args[];
// initial directory for file chooser (optional)
private String chooserDir;
//gui components
private ArrayList<GUIPlugin> plugs;
@ -200,8 +206,18 @@ public class GUIPrism extends JFrame
* @throws GUIException Thrown if there is an error in initialising the user interface.
*/
public GUIPrism() throws GUIException, PrismException
{
this(new String[0]);
}
/** Creates a new instance of GUIPrism. By calling setupResources(), setupPrism()
* and then initComponents().
* @throws GUIException Thrown if there is an error in initialising the user interface.
*/
public GUIPrism(String args[]) throws GUIException, PrismException
{
super();
this.args = processCLArgs(args);
setupResources();
setupPrism();
initComponents();
@ -226,7 +242,8 @@ public class GUIPrism extends JFrame
}
// Create new file chooser which starts in current directory
File currentDir = new File(".");
// (or in the directory specified with command-line arg -dir)
File currentDir = new File(chooserDir == null ? "." : chooserDir);
// If current directory is the bin directory, go up one level (mainly for Windows version)
try {
currentDir = currentDir.getCanonicalFile();
@ -402,22 +419,51 @@ public class GUIPrism extends JFrame
}
}
public void passCLArgs(String args[])
public String[] processCLArgs(String args[])
{
// just before we get started, pass any command-line args to all plugins
// we first remove the -javamaxmem/-javastack arguments, if present
// before (later) passing any command-line args to all plugins
// we first remove any -javamaxmem/-javastack arguments (ignored)
// and any -dir argument (processed here first)
List<String> argsCopy = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-javamaxmem") || args[i].equals("-javastack")) {
// ignore argument and subsequent value
i++;
if (args[i].charAt(0) == '-') {
String sw = args[i].substring(1);
// remove optional second "-" (i.e. we allow switches of the form --sw too)
if (sw.charAt(0) == '-')
sw = sw.substring(1);
if (sw.equals("javamaxmem") || sw.equals("javastack")) {
// ignore argument and subsequent value
i++;
} else if (sw.equals("dir")) {
if (i < args.length - 1) {
String workingDir = args[++i];
// set working dir natively for PRISM stuff
if (PrismNative.setWorkingDirectory(workingDir) != 0) {
System.err.println("Error: Could not change working directory to " + workingDir);
System.exit(1);
}
// also store locally to initialise file chooser
chooserDir = workingDir;
// NB: we avoid setting system property "user.dir"
// since this may break loading of shared libraries etc.
} else {
System.err.println("Error: No value specified for -" + sw + " switch");
System.exit(1);
}
} else {
argsCopy.add(args[i]);
}
} else {
argsCopy.add(args[i]);
}
}
for (int i = 0; i < plugs.size(); i++) {
GUIPlugin plug = (GUIPlugin) plugs.get(i);
plug.takeCLArgs(argsCopy.toArray(new String[0]));
return argsCopy.toArray(new String[0]);
}
public void passCLArgs()
{
for (GUIPlugin plug : plugs) {
plug.takeCLArgs(args);
}
}

Loading…
Cancel
Save