[QGIS-Developer] passing parameters to QGIS Python-Script started via Command Line

Matthias Kuhn matthias at opengis.ch
Mon Aug 8 07:22:20 PDT 2022


Hi Peter,

You are correct with the analysis of the situation.

Even if `qgis --help` states the following:
 [--] treat all following arguments as FILEs

it does in fact already interpret parameters as files before the `--`
parameter.
Changing this behavior is risky (it will break many startup scripts out
there) and can be considered for QGIS 4.
For now, we could add another marker as a band aid, e.g. we add a `--ex`
(extra-parameters) that indicates that any parameters that follow should
NOT be interpreted as files:

    qgis -p 1234.qgs --code script.py [files could be here] --ex
--myparam=1324 -- [more files can still be here]

If that sounds like what you are looking for, we are happy to help you with
that

Best regards
Matthias

On Mon, Aug 8, 2022 at 3:55 PM Drexel Peter via QGIS-Developer <
qgis-developer at lists.osgeo.org> wrote:

> Hi everyone,
>
> My goal: I want to open QGIS using the Command-Line, load a specific
> project, select specific features and zoom to the selected features.
>
> What sounds rather difficult is actually mostly solvable using the
> following:
>
> "C:\Program Files\QGIS 3.16\bin\qgis-ltr-bin.exe" -p
> "d:\standardebenen.qgs" --code myscript.py
>
>
>
> This opens QGIS with the Project d:\standardebenen.qgs and after that it
> runs the script “myscript.py”.
>
> Within the scrip I can do everything what I can do in a Python-script, so
> there I can select some features in a layer and show that selection.
>
> But this selections are “hard-coded” in the script, I cannot pass over
> these parameters to the myscript.py.
>
> In “Pure Python” I can pass parameters to a Python Script using “python
> myscript.py arg1 arg2 arg3”.
>
> This seems to be not working in the QGIS-Command-Line  as the parameters
> are interpreted as “layers to load”.
>
> Am I right with my analysis?
>
> One workaround is to write the parameters to al file and use this file
> within the myscript.py to define the selections. But this is very unhandy…
>
> Are there any ideas from any QGIS-Expert to solve my problem?
>
>
>
> If this cannot be solved in the current version of QGIS is one of the
> developers willing to get in contact with me to make an offer the implement
> this feature ?
>
> Thanks
>
> Peter
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>

-- 
 <https://qfield.org/get/>

QFIELD 2.0 IS HERE! - Hold the power of QGIS in 
your hand - learn more 
<https://www.opengis.ch/2022/04/05/qfield-2-0-is-here/> - get it now 
<https://qfield.org/get>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220808/9341ff7b/attachment.htm>


More information about the QGIS-Developer mailing list