Skip to content

Commit 9e17e52

Browse files
matthijskooijmancmaglie
authored andcommitted
Parse --preferences-file in main instead of Preferences.init
Parsing commandline arguments inside Preferences isn't very elegant, this is better suited for the main function. Also, this change prepares for taking --curdir into account for --preferences-file as well.
1 parent e494f39 commit 9e17e52

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

app/src/processing/app/Base.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,20 @@ static public void main(String args[]) throws Exception {
139139
if (!portableFolder.exists())
140140
portableFolder = null;
141141

142+
File preferencesFile = null;
143+
144+
// Do a first pass over the commandline arguments, the rest of them
145+
// will be processed by the Base constructor. Note that this loop
146+
// does not look at the last element of args, to prevent crashing
147+
// when no parameter was specified to an option. Later, Base() will
148+
// then show an error for these.
149+
for (int i = 0; i < args.length - 1; i++) {
150+
if (args[i].equals("--preferences-file"))
151+
preferencesFile = new File(args[i + 1]);
152+
}
153+
142154
// run static initialization that grabs all the prefs
143-
Preferences.init(args);
155+
Preferences.init(preferencesFile);
144156

145157
try {
146158
File versionFile = getContentFile("lib/version.txt");
@@ -402,7 +414,7 @@ public Base(String[] args) throws Exception {
402414
i++;
403415
if (i >= args.length)
404416
showError(null, _("Argument required for --preferences-file"), 3);
405-
// Argument should be already processed by Preferences.init(...)
417+
// Argument should be already processed by Base.main(...)
406418
continue;
407419
}
408420
if (args[i].startsWith("--"))

app/src/processing/app/Preferences.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ public String toString() {
223223
static boolean doSave = true;
224224

225225

226-
static protected void init(String args[]) {
226+
static protected void init(File file) {
227+
if (file != null)
228+
preferencesFile = file;
229+
else
230+
preferencesFile = Base.getSettingsFile(Preferences.PREFS_FILE);
227231

228232
// start by loading the defaults, in case something
229233
// important was deleted from the user prefs
@@ -255,17 +259,6 @@ static protected void init(String args[]) {
255259
// clone the hash table
256260
defaults = new Hashtable<String, String>(table);
257261

258-
// next load user preferences file
259-
preferencesFile = Base.getSettingsFile(PREFS_FILE);
260-
261-
// load a preferences file if specified on the command line
262-
if (args != null) {
263-
for (int i = 0; i < args.length - 1; i++) {
264-
if (args[i].equals("--preferences-file"))
265-
preferencesFile = new File(args[i + 1]);
266-
}
267-
}
268-
269262
if (preferencesFile.exists()) {
270263
// load the previous preferences file
271264
try {

0 commit comments

Comments
 (0)