From 54c91f6c2ee5878ac4c99340c29ede3048d3481e Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 28 Jan 2020 15:09:15 +0100
Subject: [PATCH] simplify parsing of MATLAB command line arguments

closes #1696

includes preprocessor changes
- macro processor: simplify handling of `@#define`

(cherry picked from commit d527096e61ddcb0c05565b76e8bcd1b553fead3d)

# Conflicts:
#	preprocessor
---
 doc/manual/source/running-dynare.rst | 17 ++++++++++++++---
 matlab/dynare.m                      | 13 ++++++++++++-
 preprocessor                         |  2 +-
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/doc/manual/source/running-dynare.rst b/doc/manual/source/running-dynare.rst
index 1b6c0d4cfa..c85f0bf5cc 100644
--- a/doc/manual/source/running-dynare.rst
+++ b/doc/manual/source/running-dynare.rst
@@ -338,9 +338,20 @@ by the ``dynare`` command.
 
     .. option:: -DMACRO_VARIABLE=MACRO_EXPRESSION
 
-        Defines a macro-variable from the command line (the same
-        effect as using the Macro directive ``@#define`` in a model
-        file, see :ref:`macro-proc-lang`).
+        Defines a macro-variable from the command line (the same effect as
+        using the Macro directive ``@#define`` in a model file, see
+        :ref:`macro-proc-lang`). Note that when passing a MACRO_EXPRESSION that
+        contains a space, you must surround the entire ``-D`` flag with single
+        quotes, as in the following example. Also note that an expression
+        passed on the command line can reference variables defined before it.
+
+        *Example*
+
+        Call dynare with command line defines
+
+            .. code-block:: matlab
+
+               >> dynare <<modfile.mod>> -DA=true '-DB="A string with space"' -DC=[1,2,3] '-DD=[ i in C when i > 1 ]'
 
     .. option:: -I<<path>>
 
diff --git a/matlab/dynare.m b/matlab/dynare.m
index af5ca3f2e2..d429d54c22 100644
--- a/matlab/dynare.m
+++ b/matlab/dynare.m
@@ -213,7 +213,18 @@ end
 command = ['"' dynareroot 'preprocessor' arch_ext filesep 'dynare_m" ' fname] ;
 command = [ command ' mexext=' mexext ' "matlabroot=' matlabroot '"'];
 if ~isempty(varargin)
-    dynare_varargin = strjoin(varargin);
+    if ispc
+        varargincopy = varargin;
+        for i = 1:length(varargincopy)
+            if varargincopy{i}(end) == '\'
+                varargincopy{i} = [varargincopy{i} '\'];
+            end
+        end
+        varargincopy = strrep(varargincopy, '"', '\"');
+        dynare_varargin = ['"' strjoin(varargincopy, '" "') '"'];
+    else
+        dynare_varargin = ['''' strjoin(varargin, ''' ''') ''''];
+    end
     command = [command ' ' dynare_varargin];
 end
 
diff --git a/preprocessor b/preprocessor
index e137b2df62..e13e5a3244 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit e137b2df62b33731e1cc0cbad052e43eaa5a8836
+Subproject commit e13e5a3244f70789fcbf743aefaabbf9b4fee523
-- 
GitLab