ZH: scriptlib nicer myargumentparser

This commit is contained in:
Eric Teunis de Boone 2023-02-02 16:42:16 +01:00
parent ad7a62604b
commit a66782064e

View file

@ -4,9 +4,13 @@ Some preconfigured ArgumentParser
from argparse import ArgumentParser from argparse import ArgumentParser
def MyArgumentParser(default_fig_dir='./figures', default_show_plots=False, **kwargs): def MyArgumentParser(
default_fig_dir='./figures',
default_show_plots=False,
**kwargs):
""" """
A somewhat preconfigured ArgumentParser A somewhat preconfigured ArgumentParser to be shared across
multiple scripts.
Set show_plots=True to by default enable showing plots. Set show_plots=True to by default enable showing plots.
Likewise, set fig_dir=None to by default disable saving figures. Likewise, set fig_dir=None to by default disable saving figures.
@ -14,11 +18,11 @@ def MyArgumentParser(default_fig_dir='./figures', default_show_plots=False, **kw
parser = ArgumentParser(**kwargs) parser = ArgumentParser(**kwargs)
# Whether to show plots # Whether to show plots
parser.add_argument('--show-plots', action='store_true') group1 = parser.add_mutually_exclusive_group(required=False)
parser.add_argument('--no-show-plots', dest='show-plots', action='store_false') group1.add_argument('--show-plots', action='store_true', default=default_show_plots, help='Default: %(default)s')
parser.set_defaults(show_plots=default_show_plots) group1.add_argument('--no-show-plots', dest='show-plots', action='store_false')
# Figures directory # Figures directory
parser.add_argument('--fig-dir', type=str, default=default_fig_dir) parser.add_argument('--fig-dir', type=str, default=default_fig_dir, help='Set None to disable saving figures. (Default: %(default)s)')
return parser return parser