Skip to content

Help option#1183

Open
sergey-yaroslavtsev wants to merge 2 commits intomasterfrom
help_option
Open

Help option#1183
sergey-yaroslavtsev wants to merge 2 commits intomasterfrom
help_option

Conversation

@sergey-yaroslavtsev
Copy link
Collaborator

Closes #1141
By switching to argparse.

Copy link
Collaborator

@woutdenolf woutdenolf left a comment

Choose a reason for hiding this comment

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

Nice! It seems that getopt is used everywhere. Perhaps in another PR we could use it everywhere? Or this PR, as you want.

Comment on lines +107 to 114
opts = []
if args.logging:
opts.append(('--logging', args.logging))
if args.debug.lower() not in ['0', 'false']:
opts.append(('--debug', args.debug))

from PyMca5.PyMcaCore.LoggingLevel import getLoggingLevel
logging.basicConfig(level=getLoggingLevel(opts))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need getLoggingLevel here?

Copy link
Collaborator

@woutdenolf woutdenolf Feb 11, 2026

Choose a reason for hiding this comment

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

Essentially we have one log level

log_level = "debug" if args.debug else args.logging

Something like

import argparse
import logging

parser = argparse.ArgumentParser(description="Example script with logging.")
parser.add_argument(
    "--logging",
    default="warning",
    choices=["debug", "info", "warning", "error", "critical"],
    help="Set logging level (default: warning)"
)
parser.add_argument(
    "--debug",
    action="store_true",
    help="Force logging level to DEBUG"
)

args = parser.parse_args()

if args.debug:
    log_level = logging.DEBUG
else:
    log_level = getattr(logging, args.logging.upper(), logging.WARNING)

logging.basicConfig(level=log_level)

Copy link
Collaborator

@woutdenolf woutdenolf Feb 11, 2026

Choose a reason for hiding this comment

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

Maybe this logic can be implemented in a re-usable way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we need backward compatibility? if yes then to forbid numerical values will be an issue.
If not this is a good looking approach.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we need it.

@sergey-yaroslavtsev
Copy link
Collaborator Author

Nice! It seems that getopt is used everywhere. Perhaps in another PR we could use it everywhere? Or this PR, as you want.

Sry i am a bit confused with "use it". You mean substitute getopt with argparse everywhere?

Yea I can search and change it everywhere if it is better, should not be a big problem.

@woutdenolf
Copy link
Collaborator

You mean substitute getopt with argparse everywhere?

Yes

@woutdenolf
Copy link
Collaborator

woutdenolf commented Feb 12, 2026

May I also suggest to use

from argparse import ArgumentDefaultsHelpFormatter

parser = ArgumentParser(
        ..., formatter_class=ArgumentDefaultsHelpFormatter
    )

This shows the default values in --help.

Again you may want to implement common logic in PyMca5.PyMcaCore.LoggingLevel so we can re-use it in all the CLI's (there are many).

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.

Add help to all CLI tools provides by pymca

3 participants