How to do it...

  1. Define the overall argument parsing function:
        def get_options(): 
  1. Create the parser object:
        parser = argparse.ArgumentParser() 
  1. 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') ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.