cros_sdk: convert to commandline.ArgumentParser

BUG=chromium:496565
TEST=the tree doesn't explode and tests keep passing

Change-Id: I16c5dd4930b30e38eaeefd738cfba4609aa2ad82
Reviewed-on: https://chromium-review.googlesource.com/275182
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/cros_sdk.py b/scripts/cros_sdk.py
index 4eb18b7..3d70ff1 100644
--- a/scripts/cros_sdk.py
+++ b/scripts/cros_sdk.py
@@ -2,10 +2,18 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-"""This script fetches and prepares an SDK chroot."""
+"""Manage SDK chroots.
+
+This script is used for manipulating local chroot environments; creating,
+deleting, downloading, etc.  If given --enter (or no args), it defaults
+to an interactive bash shell within the chroot.
+
+If given args those are passed to the chroot environment, and executed.
+"""
 
 from __future__ import print_function
 
+import argparse
 import glob
 import os
 import pwd
@@ -464,57 +472,52 @@
 
 def _CreateParser(sdk_latest_version, bootstrap_latest_version):
   """Generate and return the parser with all the options."""
-  usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args]
-
-This script is used for manipulating local chroot environments; creating,
-deleting, downloading, etc.  If given --enter (or no args), it defaults
-to an interactive bash shell within the chroot.
-
-If given args those are passed to the chroot environment, and executed."""
-
-  parser = commandline.OptionParser(usage=usage, caching=True)
+  usage = ('usage: %(prog)s [options] '
+           '[VAR1=val1 ... VAR2=val2] [--] [command [args]]')
+  parser = commandline.ArgumentParser(usage=usage, description=__doc__,
+                                      caching=True)
 
   # Global options.
   default_chroot = os.path.join(constants.SOURCE_ROOT,
                                 constants.DEFAULT_CHROOT_DIR)
-  parser.add_option(
+  parser.add_argument(
       '--chroot', dest='chroot', default=default_chroot, type='path',
       help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR))
 
-  parser.add_option('--chrome_root', default=None, type='path',
-                    help='Mount this chrome root into the SDK chroot')
-  parser.add_option('--chrome_root_mount', default=None, type='path',
-                    help='Mount chrome into this path inside SDK chroot')
-  parser.add_option('--nousepkg', action='store_true', default=False,
-                    help='Do not use binary packages when creating a chroot.')
-  parser.add_option('-u', '--url',
-                    dest='sdk_url', default=None,
-                    help=('''Use sdk tarball located at this url.
-                             Use file:// for local files.'''))
-  parser.add_option('--sdk-version', default=None,
-                    help=('Use this sdk version.  For prebuilt, current is %r'
-                          ', for bootstrapping it is %r.'
-                          % (sdk_latest_version, bootstrap_latest_version)))
-  parser.add_option('--workspace', default=None,
-                    help='Workspace directory to mount into the chroot.')
-  parser.add_option('--board', default=None,
-                    help='The board we intend to be building in the chroot. '
-                    'Used for downloading an SDK board overlay (if one is '
-                    'found), which may speed up chroot initialization when '
-                    'building for the first time. Otherwise this has no effect '
-                    'and will not restrict the chroot in any way. Ignored if '
-                    'using --bootstrap.')
+  parser.add_argument('--chrome_root', type='path',
+                      help='Mount this chrome root into the SDK chroot')
+  parser.add_argument('--chrome_root_mount', type='path',
+                      help='Mount chrome into this path inside SDK chroot')
+  parser.add_argument('--nousepkg', action='store_true', default=False,
+                      help='Do not use binary packages when creating a chroot.')
+  parser.add_argument('-u', '--url', dest='sdk_url',
+                      help='Use sdk tarball located at this url. Use file:// '
+                           'for local files.')
+  parser.add_argument('--sdk-version',
+                      help=('Use this sdk version.  For prebuilt, current is %r'
+                            ', for bootstrapping it is %r.'
+                            % (sdk_latest_version, bootstrap_latest_version)))
+  parser.add_argument('--workspace',
+                      help='Workspace directory to mount into the chroot.')
+  parser.add_argument('--board',
+                      help='The board we intend to be building in the chroot. '
+                      'Used for downloading an SDK board overlay (if one is '
+                      'found), which may speed up chroot initialization when '
+                      'building for the first time. Otherwise this has no '
+                      'effect and will not restrict the chroot in any way. '
+                      'Ignored if using --bootstrap.')
+  parser.add_argument('commands', nargs=argparse.REMAINDER)
 
   # Commands.
-  group = parser.add_option_group('Commands')
-  group.add_option(
+  group = parser.add_argument_group('Commands')
+  group.add_argument(
       '--enter', action='store_true', default=False,
       help='Enter the SDK chroot.  Implies --create.')
-  group.add_option(
+  group.add_argument(
       '--create', action='store_true', default=False,
       help='Create the chroot only if it does not already exist.  '
       'Implies --download.')
-  group.add_option(
+  group.add_argument(
       '--bootstrap', action='store_true', default=False,
       help='Build everything from scratch, including the sdk.  '
       'Use this only if you need to validate a change '
@@ -522,35 +525,36 @@
       'build are typically the only folk who need this).  '
       'Note this will quite heavily slow down the build.  '
       'This option implies --create --nousepkg.')
-  group.add_option(
+  group.add_argument(
       '-r', '--replace', action='store_true', default=False,
       help='Replace an existing SDK chroot.  Basically an alias '
       'for --delete --create.')
-  group.add_option(
+  group.add_argument(
       '--delete', action='store_true', default=False,
       help='Delete the current SDK chroot if it exists.')
-  group.add_option(
+  group.add_argument(
       '--download', action='store_true', default=False,
       help='Download the sdk.')
   commands = group
 
   # Namespace options.
-  group = parser.add_option_group('Namespaces')
-  group.add_option('--proxy-sim', action='store_true', default=False,
-                   help='Simulate a restrictive network requiring an outbound'
-                        ' proxy.')
-  group.add_option('--no-ns-pid', dest='ns_pid',
-                   default=True, action='store_false',
-                   help='Do not create a new PID namespace.')
+  group = parser.add_argument_group('Namespaces')
+  group.add_argument('--proxy-sim', action='store_true', default=False,
+                     help='Simulate a restrictive network requiring an outbound'
+                          ' proxy.')
+  group.add_argument('--no-ns-pid', dest='ns_pid',
+                     default=True, action='store_false',
+                     help='Do not create a new PID namespace.')
 
   # Internal options.
-  group = parser.add_option_group(
+  group = parser.add_argument_group(
       'Internal Chromium OS Build Team Options',
       'Caution: these are for meant for the Chromium OS build team only')
-  group.add_option('--buildbot-log-version', default=False, action='store_true',
-                   help='Log SDK version for buildbot consumption')
+  group.add_argument('--buildbot-log-version', default=False,
+                     action='store_true',
+                     help='Log SDK version for buildbot consumption')
 
-  return (parser, commands)
+  return parser, commands
 
 
 def main(argv):
@@ -560,7 +564,8 @@
   sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>')
   bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>')
   parser, commands = _CreateParser(sdk_latest_version, bootstrap_latest_version)
-  options, chroot_command = parser.parse_args(argv)
+  options = parser.parse_args(argv)
+  chroot_command = options.commands
 
   # Some sanity checks first, before we ask for sudo credentials.
   cros_build_lib.AssertOutsideChroot()
@@ -589,8 +594,12 @@
     options.create = True
 
   # If a command is not given, default to enter.
+  # pylint: disable=protected-access
+  # This _group_actions access sucks, but upstream decided to not include an
+  # alternative to optparse's option_list, and this is what they recommend.
   options.enter |= not any(getattr(options, x.dest)
-                           for x in commands.option_list)
+                           for x in commands._group_actions)
+  # pylint: enable=protected-access
   options.enter |= bool(chroot_command)
 
   if options.enter and options.delete and not options.create: