binman: Convert to use ArgumentParser

This class is the new way to handle arguments in Python. Convert binman
over to use it. At the same time, introduce commands so that we can
separate out the different parts of binman functionality.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/binman/cmdline.py b/tools/binman/cmdline.py
index 91e007e..a002105 100644
--- a/tools/binman/cmdline.py
+++ b/tools/binman/cmdline.py
@@ -5,7 +5,7 @@
 # Command-line parser for binman
 #
 
-from optparse import OptionParser
+from argparse import ArgumentParser
 
 def ParseArgs(argv):
     """Parse the binman command-line arguments
@@ -17,56 +17,64 @@
             options provides access to the options (e.g. option.debug)
             args is a list of string arguments
     """
-    parser = OptionParser()
-    parser.add_option('-a', '--entry-arg', type='string', action='append',
-            help='Set argument value arg=value')
-    parser.add_option('-b', '--board', type='string',
-            help='Board name to build')
-    parser.add_option('-B', '--build-dir', type='string', default='b',
-            help='Directory containing the build output')
-    parser.add_option('-d', '--dt', type='string',
-            help='Configuration file (.dtb) to use')
-    parser.add_option('-D', '--debug', action='store_true',
-            help='Enabling debugging (provides a full traceback on error)')
-    parser.add_option('-E', '--entry-docs', action='store_true',
-            help='Write out entry documentation (see README.entries)')
-    parser.add_option('--fake-dtb', action='store_true',
-            help='Use fake device tree contents (for testing only)')
-    parser.add_option('-i', '--image', type='string', action='append',
-            help='Image filename to build (if not specified, build all)')
-    parser.add_option('-I', '--indir', action='append',
-            help='Add a path to the list of directories to use for input files')
-    parser.add_option('-H', '--full-help', action='store_true',
+    if '-H' in argv:
+        argv.append('build')
+
+    epilog = '''Binman creates and manipulate images for a board from a set of binaries. Binman is
+controlled by a description in the board device tree.'''
+
+    parser = ArgumentParser(epilog=epilog)
+    parser.add_argument('-B', '--build-dir', type=str, default='b',
+        help='Directory containing the build output')
+    parser.add_argument('-D', '--debug', action='store_true',
+        help='Enabling debugging (provides a full traceback on error)')
+    parser.add_argument('-H', '--full-help', action='store_true',
         default=False, help='Display the README file')
-    parser.add_option('-m', '--map', action='store_true',
+    parser.add_argument('--toolpath', type=str, action='append',
+        help='Add a path to the directories containing tools')
+    parser.add_argument('-v', '--verbosity', default=1,
+        type=int, help='Control verbosity: 0=silent, 1=warnings, 2=notices, '
+        '3=info, 4=detail, 5=debug')
+
+    subparsers = parser.add_subparsers(dest='cmd')
+
+    build_parser = subparsers.add_parser('build', help='Build firmware image')
+    build_parser.add_argument('-a', '--entry-arg', type=str, action='append',
+            help='Set argument value arg=value')
+    build_parser.add_argument('-b', '--board', type=str,
+            help='Board name to build')
+    build_parser.add_argument('-d', '--dt', type=str,
+            help='Configuration file (.dtb) to use')
+    build_parser.add_argument('--fake-dtb', action='store_true',
+            help='Use fake device tree contents (for testing only)')
+    build_parser.add_argument('-i', '--image', type=str, action='append',
+            help='Image filename to build (if not specified, build all)')
+    build_parser.add_argument('-I', '--indir', action='append',
+            help='Add a path to the list of directories to use for input files')
+    build_parser.add_argument('-m', '--map', action='store_true',
         default=False, help='Output a map file for each image')
-    parser.add_option('-O', '--outdir', type='string',
+    build_parser.add_argument('-O', '--outdir', type=str,
         action='store', help='Path to directory to use for intermediate and '
         'output files')
-    parser.add_option('-p', '--preserve', action='store_true',\
+    build_parser.add_argument('-p', '--preserve', action='store_true',\
         help='Preserve temporary output directory even if option -O is not '
              'given')
-    parser.add_option('-P', '--processes', type=int,
-                      help='set number of processes to use for running tests')
-    parser.add_option('-t', '--test', action='store_true',
-                    default=False, help='run tests')
-    parser.add_option('-T', '--test-coverage', action='store_true',
-                    default=False, help='run tests and check for 100% coverage')
-    parser.add_option('--toolpath', type='string', action='append',
-            help='Add a path to the directories containing tools')
-    parser.add_option('-u', '--update-fdt', action='store_true',
+    build_parser.add_argument('-u', '--update-fdt', action='store_true',
         default=False, help='Update the binman node with offset/size info')
-    parser.add_option('-v', '--verbosity', default=1,
-        type='int', help='Control verbosity: 0=silent, 1=progress, 3=full, '
-        '4=debug')
-    parser.add_option('-X', '--test-preserve-dirs', action='store_true',
+
+    entry_parser = subparsers.add_parser('entry-docs',
+        help='Write out entry documentation (see README.entries)')
+
+    test_parser = subparsers.add_parser('test', help='Run tests')
+    test_parser.add_argument('-P', '--processes', type=int,
+        help='set number of processes to use for running tests')
+    test_parser.add_argument('-T', '--test-coverage', action='store_true',
+        default=False, help='run tests and check for 100%% coverage')
+    test_parser.add_argument('-X', '--test-preserve-dirs', action='store_true',
         help='Preserve and display test-created input directories; also '
              'preserve the output directory if a single test is run (pass test '
              'name at the end of the command line')
-
-    parser.usage += """
-
-Create images for a board from a set of binaries. It is controlled by a
-description in the board device tree."""
+    test_parser.add_argument('tests', nargs='*',
+                             help='Test names to run (omit for all)')
 
     return parser.parse_args(argv)