Create Professional Python Programs Using Argparse

Опубликовано: 25 Март 2022
на канале: theurbanpenguin
6,255
148

Using python and argparse we can create professional Python programs very quickly with the module argparse doing the heavy lifting. We have all seen the option --help / -h well they are built into argparse and of course we can create our own options. Managing data types is easy as we can set what type of data we can expect

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('val', help='A value to convert expressed an integer', type=int)
parser.add_argument('-v', '--verbose', help='Print more detail', action='store_true')
contype = parser.add_mutually_exclusive_group(required=True)
contype.add_argument('-f', '--fahr', help='Enable to covert from F to C', action='store_true')
contype.add_argument('-c', '--cent', help='Enable to covert from C to F', action='store_true')
args = parser.parse_args()

if args.verbose and args.fahr:
print("This is the result")
c = (args.val - 32)/1.8
print(f"{args.val}f is {c:.2f}c")
exit(0)
if args.fahr:
c = (args.val - 32)/1.8
print(f"{args.val}f is {c:.2f}c")
exit(0)
if args.verbose and args.cent:
print("This is the result")
f = (args.val * 9 / 5) + 32
print(f"{args.val}c is {f:.2f}f")
exit(0)
if args.cent:
f = (args.val * 9 / 5) + 32
print(f"{args.val}c is {f:.2f}f")
exit(0)


Additionally you can find my video courses on Pluralsight: http://pluralsight.com/training/Autho... and take time to see my own site http://www.theurbanpenguin.com

~-~~-~~~-~~-~
Please watch: "RHCSA 9 Working With Podman Containers"
   • How To Use Podman Containers  
~-~~-~~~-~~-~


На этой странице сайта вы можете посмотреть видео онлайн Create Professional Python Programs Using Argparse длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь theurbanpenguin 25 Март 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 6,255 раз и оно понравилось 148 зрителям. Приятного просмотра!