Write a Python program to accept multiple values for the same argument

python argument
Jan 2, 202405:01 PM139 views
0

Problem

The task here is to write a Python program to accept multiple values for the same argument. In the example below, we have an example program that is written to accept one value for the argument `--snapshot`. ```python import argparse import sys def parse_pargs(args): parser = argparse.ArgumentParser() parser.add_argument('-ss', '--snapshot', type=str, help='snapshots', required=True) return parser.parse_args(args) if __name__ == '__main__': args = parse_pargs(sys.argv[1:]) print(args.snapshot) ``` If we place this code in a file called `app.py`, a user might execute the program as follows. ```bash python app.py --snapshot 2023-01-01 ``` However, we want to enable the user the pass in multiple values for `--snapshot` as follows. ```bash python app.py --snapshot 2023-01-01 --snapshot 2023-02-01 --snapshot 2023-03-01 ``` Modify this program so that a user can pass in multiple values for `--snapshot`.
Menu