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

New command line parameter --py-args #50092

merged 6 commits into from
Sep 10, 2022

Conversation

domi4484
Copy link
Contributor

@domi4484 domi4484 commented Sep 5, 2022

This PR will allow to pass arguments to the script provided via --code parameter and more generally to each python execution. All arguments after --py-args till -- are passed over to the python interpreter and ignored by QGIS.

Note: After --code-args is not possible to pass any other parameter than -- to QGIS

For example:

qgis --code /home/domi/test.py --py-args --specialScriptArguments "a text arg" 'and another arg' -- layer1 layer2
  • test.py will have this content in sys.argv: ['/home/domi/test.py', '--specialScriptArguments', 'a text arg', 'and another arg']
  • layer1 and layer2 will be normally handled by QGIS as layers to load

@github-actions github-actions bot added this to the 3.28.0 milestone Sep 5, 2022
src/app/main.cpp Outdated
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?

@domi4484 domi4484 changed the title New command line parameter --code-args New command line parameter --py-args Sep 6, 2022
@m-kuhn m-kuhn added Feature PyQGIS Related to the PyQGIS API Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Sep 7, 2022
@m-kuhn m-kuhn merged commit b0d6056 into qgis:master Sep 10, 2022
@zacharlie zacharlie added ChangelogHarvested This PR description has been harvested in the Changelog already. and removed Changelog Items that are queued to appear in the visual changelog - remove after harvesting labels Oct 16, 2022
@agiudiceandrea agiudiceandrea added the Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. label Jan 13, 2025
@qgis-bot
Copy link
Collaborator

@domi4484
This pull request has been tagged as requiring documentation.

A documentation ticket will be opened at https://github.com/qgis/QGIS-Documentation when this PR is merged.

Please update the description (not the comments) with helpful description and screenshot to help the work from documentors.
Also, any commit having [needs-doc] or [Needs Documentation] in will see its message pushed to the issue, so please be as verbose as you can.

Thank you!

@qgis-bot
Copy link
Collaborator

@domi4484
A documentation ticket has been opened at qgis/QGIS-Documentation#9544
It is your responsibility to visit this ticket and add as much detail as possible for the documentation team to correctly document this change.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ChangelogHarvested This PR description has been harvested in the Changelog already. Feature Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. PyQGIS Related to the PyQGIS API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants