Skip to content

Commit bfa08c5

Browse files
committed
[FEATURE] Add --code arg to run python code on qgis load
1 parent e3d30af commit bfa08c5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/app/main.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "qgscustomization.h"
3636
#include "qgspluginregistry.h"
3737
#include "qgsmessagelog.h"
38+
#include "qgspythonrunner.h"
3839

3940
#include <cstdio>
4041
#include <stdio.h>
@@ -106,6 +107,7 @@ void usage( std::string const & appName )
106107
<< "\t[--nocustomization]\tdon't apply GUI customization\n"
107108
<< "\t[--optionspath path]\tuse the given QSettings path\n"
108109
<< "\t[--configpath path]\tuse the given path for all user configuration\n"
110+
<< "\t[--code path]\tRun the given python file on load. \n"
109111
<< "\t[--help]\t\tthis text\n\n"
110112
<< " FILES:\n"
111113
<< " Files specified on the command line can include rasters,\n"
@@ -266,6 +268,8 @@ int main( int argc, char *argv[] )
266268
QString configpath;
267269
QString optionpath;
268270

271+
QString pythonfile;
272+
269273
#if defined(ANDROID)
270274
QgsDebugMsg( QString( "Android: All params stripped" ) );// Param %1" ).arg( argv[0] ) );
271275
//put all QGIS settings in the same place
@@ -325,6 +329,10 @@ int main( int argc, char *argv[] )
325329
{
326330
configpath = argv[++i];
327331
}
332+
else if ( i + 1 < argc && ( arg == "--code" || arg == "-f" ) )
333+
{
334+
pythonfile = argv[++i];
335+
}
328336
else
329337
{
330338
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() ) );
@@ -358,6 +366,7 @@ int main( int argc, char *argv[] )
358366
{"extent", required_argument, 0, 'e'},
359367
{"optionspath", required_argument, 0, 'o'},
360368
{"configpath", required_argument, 0, 'c'},
369+
{"code", required_argument, 0, 'f'},
361370
{"android", required_argument, 0, 'a'},
362371
{0, 0, 0, 0}
363372
};
@@ -428,6 +437,10 @@ int main( int argc, char *argv[] )
428437
configpath = optarg;
429438
break;
430439

440+
case 'f':
441+
pythonfile = optarg;
442+
break;
443+
431444
case '?':
432445
usage( argv[0] );
433446
return 2; // XXX need standard exit codes
@@ -769,7 +782,16 @@ int main( int argc, char *argv[] )
769782
}
770783
}
771784

772-
/////////////////////////////////////////////////////////////////////
785+
if ( !pythonfile.isEmpty() )
786+
{
787+
#ifdef Q_WS_WIN
788+
//replace backslashes with forward slashes
789+
pythonfile.replace( "\\", "/" );
790+
#endif
791+
QgsPythonRunner::run(QString("execfile('%1')").arg(pythonfile));
792+
}
793+
794+
/////////////////////////////////`////////////////////////////////////
773795
// Take a snapshot of the map view then exit if snapshot mode requested
774796
/////////////////////////////////////////////////////////////////////
775797
if ( mySnapshotFileName != "" )
@@ -797,7 +819,6 @@ int main( int argc, char *argv[] )
797819
return 1;
798820
}
799821

800-
801822
/////////////////////////////////////////////////////////////////////
802823
// Continue on to interactive gui...
803824
/////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)