I am trying to make a build script like this:
import glob
import os
import subprocess
import re
import argparse
import shutil
def create_parser():
parser = argparse.ArgumentParser(description='Build project')
parser.add_argument('--clean_logs', type=bool, default=True,
help='If true, old debug logs will be deleted.')
parser.add_argument('--run', type=bool, default=True,
help="If true, executable will run after compilation.")
parser.add_argument('--clean_build', type=bool, default=False,
help="If true, all generated files will be deleted and the"
" directory will be reset to a pristine condition.")
return parser.parse_args()
def main():
parser = create_parser()
print(parser)
However no matter how I try to pass the argument I only get the default values. I always get Namespace(clean_build=False, clean_logs=True, run=True)
.
I have tried:
python3 build.py --run False
python3 build.py --run=FALSE
python3 build.py --run FALSE
python3 build.py --run=False
python3 build.py --run false
python3 build.py --run 'False'
It's always the same thing. What am I missing?
action="store_true"
to deal with this kind of problem: docs.python.org/3.8/library/argparse.html#action – Candleberrybool("False")
orbool("FALSE")
. The only string that returnsFalse
isbool("")
. You have to write your own function that recognizes strings like 'False' or 'No' asFalse
. The builtinbool
does not do that for you. – Scoria