Skip to content

Commit 6d1f294

Browse files
committed
Merge pull request googleapis#71 from craigcitro/argparse
Handle a missing argparse more gracefully.
2 parents 3956b95 + 94bbf43 commit 6d1f294

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

oauth2client/tools.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
__author__ = '[email protected] (Joe Gregorio)'
2525
__all__ = ['argparser', 'run_flow', 'run', 'message_if_missing']
2626

27-
import argparse
2827
import BaseHTTPServer
2928
import logging
3029
import socket
@@ -51,17 +50,25 @@
5150
# argparser is an ArgumentParser that contains command-line options expected
5251
# by tools.run(). Pass it in as part of the 'parents' argument to your own
5352
# ArgumentParser.
54-
argparser = argparse.ArgumentParser(add_help=False)
55-
argparser.add_argument('--auth_host_name', default='localhost',
56-
help='Hostname when running a local web server.')
57-
argparser.add_argument('--noauth_local_webserver', action='store_true',
58-
default=False, help='Do not run a local web server.')
59-
argparser.add_argument('--auth_host_port', default=[8080, 8090], type=int,
60-
nargs='*', help='Port web server should listen on.')
61-
argparser.add_argument('--logging_level', default='ERROR',
62-
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR',
63-
'CRITICAL'],
64-
help='Set the logging level of detail.')
53+
argparser = _CreateArgumentParser()
54+
55+
def _CreateArgumentParser():
56+
try:
57+
import argparse
58+
except ImportError:
59+
return None
60+
argparser = argparse.ArgumentParser(add_help=False)
61+
argparser.add_argument('--auth_host_name', default='localhost',
62+
help='Hostname when running a local web server.')
63+
argparser.add_argument('--noauth_local_webserver', action='store_true',
64+
default=False, help='Do not run a local web server.')
65+
argparser.add_argument('--auth_host_port', default=[8080, 8090], type=int,
66+
nargs='*', help='Port web server should listen on.')
67+
argparser.add_argument('--logging_level', default='ERROR',
68+
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR',
69+
'CRITICAL'],
70+
help='Set the logging level of detail.')
71+
return argparser
6572

6673

6774
class ClientRedirectServer(BaseHTTPServer.HTTPServer):

0 commit comments

Comments
 (0)