6.4 Using argparse to get command-line input
For some applications, it can be better to get the user input from the OS command line without a lot of human interaction. We’d prefer to parse the command-line argument values and either perform the processing or report an error.
For example, at the OS level, we might want to run a program like this:
% python ch06/distance_app.py -u KM 36.12,-86.67 33.94,-118.40
From 36.12,-86.67 to 33.94,-118.4 in KM = 2886.90
At the OS prompt of %, we entered a command, python ch06/distance_app.py. This command had an optional argument, -u KM, and two positional arguments of 36.12,-86.67 and 33.94,-118.40.
If the user enters something incorrect, the interaction might look like this:
% python ch06/distance_app.py -u KM 36.12,-86.67 33.94,-118asd
usage: distance_app.py [-h] [-u {NM,MI,KM}] p1 p2
distance_app.py: error: argument p2: could not convert...