A couple options. The -vvv is more of Linux thing rather than Pythonic, to my 
way of thinking.



import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v',action='store_true')
parser.add_argument('-vv',action='store_true')
parser.add_argument('-vvv',action='store_true')
args = parser.parse_args()

parser = argparse.ArgumentParser()
parser.add_argument('-v','--verbose',type=int,default=0,nargs='?',help="verbose 
level")
args = parser.parse_args()
level = args.verbose if args.verbose is not None else 0
print(level)


Personally I just do:

import argparse
import logging


logging.basicConfig()
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--loglevel', default='WARN', help="Python logging 
level")
args = parser.parse_args()
logger = logging.getLogger(__name__)
logger.setLevel(getattr(logging,args.loglevel))

From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on 
behalf of c.bu...@posteo.jp <c.bu...@posteo.jp>
Date: Wednesday, January 4, 2023 at 8:55 AM
To: python-list@python.org <python-list@python.org>
Subject: No solution for "--verbose" (on stdout) output in Pythonds standard 
library?
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

Hello,

first I have to say that in my current and fresh humble opinion the
often seen "--verbose" switch in command line applications should
affect only the messages given to the users. This means messages on
"stdout". That is what this question is about.

The logging module is not an option because it works on stderr and is
not intended to offer messages for the user but just for the system and
its admins via logs (log-files, syslog, stderr redirection, ...).

Using logging handlers redirecting to stdout are considered as
workarounds by me and also not an option.

This is not only my opinion but also part of the Python documentation:
https://urldefense.com/v3/__https://docs.python.org/3/howto/logging.html*when-to-use-logging__;Iw!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLfNcgTsq$<https://urldefense.com/v3/__https:/docs.python.org/3/howto/logging.html*when-to-use-logging__;Iw!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLfNcgTsq$>

I'm so detailed here about that difference between stdout and stderr
because in the wild (e.g. on StackOverflow) you often find "use logging
log levels" as a solution for that problem, which IMHO isn't one.

Now the question:
>From my research on the docs it seems there is no feature in the
standard library which could help me to implement "--verbose" or
multiple verbosity levels like "-vvv"?
I found some workarounds/hacks.
https://urldefense.com/v3/__https://stackoverflow.com/q/5980042/4865723__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLU6LbUZG$<https://urldefense.com/v3/__https:/stackoverflow.com/q/5980042/4865723__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLU6LbUZG$>
But my experience with Python as a Swiss knife is that there is always
a standard solution for such basic and often reinvented things. I won't
believe that each Python developer writes its own verbose feature. ;)
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLYWBuS4g$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLYWBuS4g$>
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to