Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command line parameter --py-args #50092

Merged
merged 6 commits into from
Sep 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void usage( const QString &appName )
<< QStringLiteral( "\t[-g, --globalsettingsfile path]\tuse the given ini file as Global Settings (defaults)\n" )
<< QStringLiteral( "\t[-a, --authdbdirectory path] use the given directory for authentication database\n" )
<< QStringLiteral( "\t[-f, --code path]\trun the given python file on load\n" )
<< QStringLiteral( "\t[-F, --code-args arguments]\targuments for the python file specified by --code. All arguments till '--' are passed to the python file and ignored by QGIS\n" )
<< QStringLiteral( "\t[-d, --defaultui]\tstart by resetting user ui settings to default\n" )
<< QStringLiteral( "\t[--hide-browser]\thide the browser widget\n" )
<< QStringLiteral( "\t[--dxf-export filename.dxf]\temit dxf output of loaded datasets to given file\n" )
Expand Down Expand Up @@ -616,6 +617,7 @@ int main( int argc, char *argv[] )
QString authdbdirectory;

QString pythonfile;
QStringList pythonfileArgs;

QString customizationfile;
QString globalsettingsfile;
Expand Down Expand Up @@ -736,6 +738,19 @@ int main( int argc, char *argv[] )
{
pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--code-args" ) || arg == QLatin1String( "-F" ) ) )
{
// Handle all parameters till '--' as code args
for ( i++; i < args.size(); ++i )
{
if ( args[i] == QLatin1String( "--" ) )
{
i--;
break;
}
pythonfileArgs << args[i];
}
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) )
{
customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
Expand Down Expand Up @@ -1538,7 +1553,10 @@ int main( int argc, char *argv[] )
//replace backslashes with forward slashes
pythonfile.replace( '\\', '/' );
#endif
QgsPythonRunner::run( QStringLiteral( "with open('%1','r') as f: exec(f.read())" ).arg( pythonfile ) );
pythonfileArgs.prepend( pythonfile );
QgsPythonRunner::run( QStringLiteral( "sys.argv = ['%1']\n"
"with open('%2','r') as f: exec(f.read())\n"
"sys.argv = []" ).arg( pythonfileArgs.join( "','" ), pythonfile ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not the best way to pass an array from c++ to python. Do someone know a better way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a raw string r'...' and escape any ' in the string?

}

/////////////////////////////////`////////////////////////////////////
Expand Down