Skip to content

Common CLI behavior#1198

Merged
woutdenolf merged 14 commits into
masterfrom
1196-harmonize-cli-with-argparse
Mar 6, 2026
Merged

Common CLI behavior#1198
woutdenolf merged 14 commits into
masterfrom
1196-harmonize-cli-with-argparse

Conversation

@woutdenolf

@woutdenolf woutdenolf commented Feb 21, 2026

Copy link
Copy Markdown
Collaborator

Closes #1141

Two new modules for common behavior across all PyMca applications:

  • PyMca5.PyMcaGui.PyMcaAppInit: common behavior for multiprocessing, Qt, HDF5, matplotlib, logging.
    Manage common calls at these stages of the application launch:
    • before importing application dependencies
    • after importing application dependencies and before instantiating the application
    • after instantiating and before starting the application
  • PyMca5.PyMcaMisc.CliUtils: common CLI behavior in terms of logging, common arguments.
    Uses argparse instead of getopt which provides proper --help for users.

The docstring of the two modules show the usage pattern.

Currently we have 28 CLI's of which 20 are Qt apps. There are many more files with a __main__, many of which using sys.argv directly, but I only handled the ones that already had an explicit CLI by using getopt or argparse.

In addition I added unit tests for the CLI: python -m PyMca5.tests.CliTest

Main CLI as an example
pymca --help

usage: pymca [-h] [--debug DEBUG] [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [--version] [--qt {5,6}] [--binding {pyqt5,pyqt6,pyside2,pyside6}]
             [--nativefiledialogs NATIVEFILEDIALOGS] [-b {matplotlib,mpl,gl,opengl,glut,osmesa,mesa,silx,silx-mpl,silxmpl,silx-gl,silxgl}] [--spec SPEC]
             [--shm SHM] [--fresh] [--profiling] [--test]
             [files ...]

Main PyMca GUI

positional arguments:
  files                 Optional list of data files to open (default: None)

options:
  -h, --help            show this help message and exit
  --debug DEBUG         Enable debug mode (default: 0)
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Logging level (default: WARNING)
  --version             Show version and exit (default: False)
  --qt {5,6}            Force Qt version (default: None)
  --binding {pyqt5,pyqt6,pyside2,pyside6}
                        Qt binding (default: None)
  --nativefiledialogs NATIVEFILEDIALOGS
                        Use native file dialogs (default: None)
  -b {matplotlib,mpl,gl,opengl,glut,osmesa,mesa,silx,silx-mpl,silxmpl,silx-gl,silxgl}, --backend {matplotlib,mpl,gl,opengl,glut,osmesa,mesa,silx,silx-mpl,silxmpl,silx-gl,silxgl}
                        The plot backend to use: Matplotlib, OpenGL 2.1 (requires appropriate OpenGL drivers), or Off-screen Mesa OpenGL software pipeline
                        (requires OSMesa library). (default: mpl)
  --spec SPEC           Spec file (default: None)
  --shm SHM             Shared memory spec (default: None)
  --fresh, -f           Clear configuration (default: False)
  --profiling           Run main loop under profiler (default: False)
  --test                Run unit tests (default: False)

@woutdenolf woutdenolf linked an issue Feb 21, 2026 that may be closed by this pull request
@woutdenolf woutdenolf marked this pull request as draft February 21, 2026 22:41
@woutdenolf woutdenolf force-pushed the 1196-harmonize-cli-with-argparse branch 14 times, most recently from 865f053 to 6a97616 Compare February 28, 2026 14:54
@woutdenolf woutdenolf force-pushed the 1196-harmonize-cli-with-argparse branch from 6a97616 to 6ed2089 Compare February 28, 2026 15:09
@woutdenolf

woutdenolf commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator Author

I ran the release in dry-run but it stopped at the first step: https://github.com/silx-kit/pymca/actions/runs/22523143704 . In dry-run we can skip the version checks I think.

@woutdenolf woutdenolf marked this pull request as ready for review February 28, 2026 15:18
@sergey-yaroslavtsev

sergey-yaroslavtsev commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator

I ran the release in dry-run but it stopped at the first step: https://github.com/silx-kit/pymca/actions/runs/22523143704 . In dry-run we can skip the version checks I think.

I think you've started "force" not a "dry-run". I started the dry-run, it skips the version check.

Or maybe GitHub again get crazy - actions are behaving weirdly in the last ~month...

@vasole

vasole commented Mar 2, 2026

Copy link
Copy Markdown
Member

I find very weird to have a main function that can be called by anybody instantiating a QApplication

I would either:

  • Instantiate the QApplication after if __name__ == "__main__"
  • Check for qt.QApplication.instance() is not None prior to instantiate the QApplication.

@woutdenolf

Copy link
Copy Markdown
Collaborator Author

I find very weird to have a main function

We do that for all our CLI's.

if __name__ == "__main__" only contains pure CLI logic and calls main.

Allows things to be testable.

@vasole

vasole commented Mar 2, 2026

Copy link
Copy Markdown
Member

I find very weird to have a main function

We do that for all our CLI's.

if __name__ == "__main__" only contains pure CLI logic and calls main.

Allows things to be testable.

Thank you for taking things out of context.

Where the that can be called by anybody instantiating a QApplication part has gone?

You should not try to instantiate a QApplication inside something not protected by if__name__ == "__main__" or without checking that a QApplication is already existing. Anything else is wrong.

@woutdenolf

Copy link
Copy Markdown
Collaborator Author

Thank you for taking things out of context.

You are welcome.

Comment thread src/PyMca5/PyMcaGui/PyMcaAppInit.py Outdated
@vasole

vasole commented Mar 2, 2026

Copy link
Copy Markdown
Member

I think you are missing the point that you can have those main functions be called by a GUI application. If there is already a QApplication, you simply do not try to create one. Return qt.QApplication.instance() instead of raising an exception is perfectly valid.

@woutdenolf

Copy link
Copy Markdown
Collaborator Author

you can have those main functions be called by a GUI application

Can you give an example?

These main functions take parsed and validated CLI arguments so that would be a surprising use case.

@vasole

vasole commented Mar 2, 2026

Copy link
Copy Markdown
Member

you can have those main functions be called by a GUI application

Can you give an example?

These main functions take parsed and validated CLI arguments so that would be a surprising use case.

I can imagine to instantiate a QApplication and to loop for all the modules to call their main function.
Many of them did not need any arguments and they were more to illustrate their use.

@woutdenolf woutdenolf force-pushed the 1196-harmonize-cli-with-argparse branch from aa0444d to 3556ad8 Compare March 3, 2026 23:32
Comment thread src/PyMca5/PyMcaCore/XiaCorrect.py Outdated
Comment thread src/PyMca5/PyMcaCore/XiaCorrect.py
Comment thread src/PyMca5/PyMcaGui/physics/xrf/ConcentrationsWidget.py
Comment thread src/PyMca5/PyMcaGui/physics/xrf/McaCalWidget.py
Comment thread src/PyMca5/PyMcaGui/physics/xrf/McaCalWidget.py Outdated
Comment thread src/PyMca5/PyMcaGui/pymca/EdfFileSimpleViewer.py Outdated
Comment thread src/PyMca5/PyMcaGui/pymca/Fit2Spec.py
Comment thread src/PyMca5/PyMcaGui/pymca/Fit2Spec.py
Comment thread src/PyMca5/PyMcaPhysics/xrf/McaAdvancedFitBatch.py Outdated
Comment thread src/PyMca5/PyMcaGui/pymca/PyMcaPostBatch.py Outdated
Comment thread src/PyMca5/PyMcaPhysics/xrf/FastXRFLinearFit.py Outdated
Comment thread src/PyMca5/PyMcaGui/pymca/PyMcaBatch.py
Comment thread src/PyMca5/PyMcaMisc/CliUtils.py Outdated
@sergey-yaroslavtsev

Copy link
Copy Markdown
Collaborator

Probably #1183 should be closed without merge - even if it is OK there is no point in it after this one.

@sergey-yaroslavtsev sergey-yaroslavtsev mentioned this pull request Mar 5, 2026
@woutdenolf woutdenolf merged commit 29d9366 into master Mar 6, 2026
10 checks passed
@woutdenolf woutdenolf mentioned this pull request Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harmonize CLI with argparse Add help to all CLI tools provides by pymca

3 participants