- Define the overall argument parsing function:
def get_options():
- Create the parser object:
parser = argparse.ArgumentParser()
- Add the various types of arguments to the parser object. Sometimes this is difficult because we're still refining the user experience. It's difficult to imagine all the ways in which people will use a program and all of the questions they might have.
For our example, we have two mandatory, positional arguments, and an optional argument:
- Point 1 latitude and longitude
- Point 2 latitude and longitude
- Optional distance
We can use Nautical Miles as a handy default so that sailors get the answers they need:
parser.add_argument('-r', action='store', choices=('NM', 'MI', 'KM'), default='NM') ...