I must be missing something really obvious, but I can't for the life of me spot it.
import argparse
def parse_args():
parser = argparse.ArgumentParser(description="Copies selected files.")
parser.add_argument(
"-c", "--checksum", type=bool, default=False
)
parser.add_argument("source")
parser.add_argument("target")
return parser.parse_args()
def main():
args = parse_args()
print(args.checksum, args.source, args.target)
main()
>python file_proc.py source1 target1
False source1 target1
So far, so good.
>python file_proc.py -c source1 target1
usage: file_proc.py [-h] [-c CHECKSUM] source target
file_proc.py: error: the following arguments are required: target
Now, why doesn't it output True source1 target1? It must be right there in front of me, but I need another pair of eyes.
Thanks!
typeparameter is supposed to be a function, something that will transform the input string into a desired value. You have to write your own if you want to interpret strings like "yes" or "no" asTrue/Falsebooleans.