Helper module for simple command line Python scripts.
The documentation with inline examples can be found in pypi.
See example1.py
It just works. Try --verbose
and --quiet
command line options, too.
It uses colored log messages on a terminal.
See --help
for more information.
See example2.py
For bigger scripts it is good idea to have the logger at the very beginning, and encapsulate the argument parsing phase, which is typically in the main function:
See example2b.py
See example3.py
It is automatically disabled on non-tty stderr
by default.
See example4.py
See example5.py
See example6.py
The local variables will be displayed in stack trace, for example:
WARNING example6.py:6: UserWarning: This user warning will be captured.
scripthelper.warn("This user warning will be captured.")
CRITICAL Uncaught RuntimeError: This exception should be handled.
File "example6.py", line 10, in <module>
6 scripthelper.warn("This user warning will be captured.")
7
8 this_variable = "will be displayed in stack trace"
9 as_well_as = "the other variables"
--> 10 raise RuntimeError("This exception should be handled.")
..................................................
this_variable = 'will be displayed in stack trace'
as_well_as = 'the other variables'
..................................................
See example7.py
The state is persisted immediately in the background in YAML. Mutable objects (list
, dict
) also can be used.
See example9.py
$ python3 example9.py
INFO example9 Processing item #1
INFO example9 - Element 1
$ python3 example9.py
INFO example9 Processing item #2
INFO example9 - Element 1
INFO example9 - Element 2
$ python3 example9.py
INFO example9 Processing item #3
INFO example9 - Element 2
INFO example9 - Element 3
See example10.py