| 1 | # Demonstration of a program that uses command line arguments |
| 2 | import argparse |
| 3 | |
| 4 | parser = argparse.ArgumentParser() |
| 5 | parser.add_argument("numbers", nargs="*", help="List of numbers to add.") |
| 6 | parser.add_argument("--to_int", help="Converts the sum to integer", action="store_true") |
| 7 | |
| 8 | |
| 9 | total = 0 |
| 10 | args = parser.parse_args() |
| 11 | |
| 12 | for number in args.numbers: |
| 13 | total += float(number) |
| 14 | |
| 15 | if int(total) == total and args.to_int: |
| 16 | total = int(total) |
| 17 | |
| 18 | print(f"The sum is {total}.") |