Repository: mapbox/mapbox-sdk-py

Issue:
Move --access-token option from command to group
New commands won't have to implement the option if it's on the group.

This means `mbx --access-token MY_TOKEN geocode` instead of `mbx geocode --access-token MY_TOKEN`.

Support for the token environment variable will be unchanged.

Discussion:

Patch:
diff --git a/mapbox/compat.py b/mapbox/compat.py
index baf551f..a46774e 100644
--- a/mapbox/compat.py
+++ b/mapbox/compat.py
@@ -4,7 +4,4 @@ import itertools
 import sys
 
 
-if sys.version_info < (3,):
-    map = itertools.imap
-else:
-    map = map
+map = itertools.imap if sys.version_info < (3,) else map
diff --git a/mapbox/scripts/cli.py b/mapbox/scripts/cli.py
index 7a9f867..5159758 100644
--- a/mapbox/scripts/cli.py
+++ b/mapbox/scripts/cli.py
@@ -23,12 +23,27 @@ def configure_logging(verbosity):
 @click.group()
 @cligj.verbose_opt
 @cligj.quiet_opt
+@click.option('--access-token', help="Your Mapbox access token.")
 @click.version_option(version=mapbox.__version__, message='%(version)s')
 @click.pass_context
-def main_group(ctx, verbose, quiet):
-    """Command line interface to Mapbox web services.
+def main_group(ctx, verbose, quiet, access_token):
+    """This is the command line interface to Mapbox web services.
+
+    Mapbox web services require an access token. Your token is shown
+    on the https://www.mapbox.com/developers/api/ page when you are
+    logged in. The token can be provided on the command line
+
+      $ mbx --access-token MY_TOKEN ...
+
+    or as an environment variable.
+
+    \b
+      $ export MapboxAccessToken=MY_TOKEN
+      $ mbx ...
+
     """
     verbosity = verbose - quiet
     configure_logging(verbosity)
     ctx.obj = {}
     ctx.obj['verbosity'] = verbosity
+    ctx.obj['access_token'] = access_token
diff --git a/mapbox/scripts/geocoder.py b/mapbox/scripts/geocoder.py
index 3348b7f..4705242 100644
--- a/mapbox/scripts/geocoder.py
+++ b/mapbox/scripts/geocoder.py
@@ -34,15 +34,14 @@ def coords_from_query(query):
 
 @click.command(short_help="Geocode an address or coordinates.")
 @click.argument('query', default='-', required=False)
-@click.option('--access-token', help="Your access token")
 @click.option(
     '--forward/--reverse',
     default=True,
     help="Perform a forward or reverse geocode. [default: forward]")
 @click.pass_context
-def geocode(ctx, query, access_token, forward):
-    """Get coordinates for an address (forward geocoding) or addresses
-    for coordinates (reverse geocoding)
+def geocode(ctx, query, forward):
+    """This command gets coordinates for an address (forward mode) or
+    addresses for coordinates (reverse mode).
 
     In forward (the default) mode the query argument shall be an address
     such as '1600 pennsylvania ave nw'.
@@ -57,6 +56,8 @@ def geocode(ctx, query, access_token, forward):
     """
     verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 2
     logger = logging.getLogger('mapbox')
+
+    access_token = (ctx.obj and ctx.obj.get('access_token')) or None
     geocoder = mapbox.Geocoder(access_token=access_token)
 
     if forward:


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index ad1a75d..278edfe 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -41,7 +41,7 @@ def test_cli_geocode_fwd():
     runner = CliRunner()
     result = runner.invoke(
         main_group,
-        ['geocode', '--forward', '1600 pennsylvania ave nw', '--access-token', 'bogus'])
+        ['--access-token', 'bogus', 'geocode', '--forward', '1600 pennsylvania ave nw'])
     assert result.exit_code == 0
     assert result.output == '{"query": ["1600", "pennsylvania", "ave", "nw"]}\n'
 
@@ -81,7 +81,7 @@ def test_cli_geocode_reverse():
     runner = CliRunner()
     result = runner.invoke(
         main_group,
-        ['geocode', '--reverse', '--access-token', 'pk.test'],
+        ['--access-token', 'pk.test', 'geocode', '--reverse'],
         input=','.join([str(x) for x in coords]))
     assert result.exit_code == 0
     assert result.output == '{"query": %s}\n' % json.dumps(coords)