I'd like to check if action='store_true
dependent script arguments were provided. For example,
run.py --save_files --out_dir "saved_files" --files_to_save 10
Such that whenever run.py --save_files
is called without --out_dir
or --files_to_save
, it fails and shows an error message.
I am trying to do it the following way:
class CheckedStoreTrue(argparse._StoreTrueAction):
def __call__(self, parser, namespace, values, option_string=None):
if not namespace.out_dir:
parser.error("Please provide an output dir!")
if not namespace.files_to_save:
parser.error("Please provide a number of files to save!")
setattr(namespace, self.dest, values)
then in
prs.add_argument(
"--save_files",
action=CheckedStoreTrue,
required=False,
help="Save files.",
)
This does not work and keeps throwing Please provide an output dir!
even after I provide the out_dir
parameter. If I check the namespace
content, I see out_dir=None
. I wonder why?