fmap.py: use argparse for parsing the command line

This makes the code easier to manage and provides automatic support
for things like -h/--help.

BUG=chromium:982465
TEST=`./fmap.py bin/example.bin` works

Change-Id: I7a45d414cb2ed2d23bf65978b9dd06e05fc99cd7
Reviewed-on: https://chromium-review.googlesource.com/1693883
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Duncan Laurie <dlaurie@google.com>
diff --git a/fmap.py b/fmap.py
index 982d1a5..a3ae343 100755
--- a/fmap.py
+++ b/fmap.py
@@ -33,9 +33,7 @@
 # GNU General Public License ("GPL") version 2 as published by the Free
 # Software Foundation.
 
-"""
-This module provides basic encode and decode functionality to the flashrom
-memory map (FMAP) structure.
+"""Basic encode/decode functionality of flashrom memory map (FMAP) structures.
 
 Usage:
   (decode)
@@ -52,6 +50,7 @@
 """
 
 
+import argparse
 import logging
 import struct
 import sys
@@ -237,18 +236,25 @@
   return blob
 
 
-def main():
-  """Decode FMAP from supplied file and print."""
-  if len(sys.argv) < 2:
-    print 'Usage: fmap.py <file>'
-    sys.exit(1)
+def get_parser():
+  """Return a command line parser."""
+  parser = argparse.ArgumentParser(
+      description=__doc__,
+      formatter_class=argparse.RawTextHelpFormatter)
+  parser.add_argument('file', help='The file to decode & print.')
+  return parser
 
-  filename = sys.argv[1]
-  print 'Decoding FMAP from: %s' % filename
-  blob = open(filename).read()
+
+def main(argv):
+  """Decode FMAP from supplied file and print."""
+  parser = get_parser()
+  opts = parser.parse_args(argv)
+
+  print('Decoding FMAP from: %s' % opts.file)
+  blob = open(opts.file, 'rb').read()
   obj = fmap_decode(blob)
   print obj
 
 
 if __name__ == '__main__':
-  main()
+  sys.exit(main(sys.argv[1:]))