How to implement the --verbose or -v option into a script?

后端 未结 9 1208
渐次进展
渐次进展 2020-12-12 10:35

I know the --verbose or -v from several tools and I\'d like to implement this into some of my own scripts and tools.

I thought of placing:<

9条回答
  •  粉色の甜心
    2020-12-12 10:56

    What I need is a function which prints an object (obj), but only if global variable verbose is true, else it does nothing.

    I want to be able to change the global parameter "verbose" at any time. Simplicity and readability to me are of paramount importance. So I would proceed as the following lines indicate:

    ak@HP2000:~$ python3
    Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> verbose = True
    >>> def vprint(obj):
    ...     if verbose:
    ...         print(obj)
    ...     return
    ... 
    >>> vprint('Norm and I')
    Norm and I
    >>> verbose = False
    >>> vprint('I and Norm')
    >>> 
    

    Global variable "verbose" can be set from the parameter list, too.

提交回复
热议问题