Port from optparse to argparse

Why now?

* argparse was introduced in Python 3.2 and back-ported to Python 2.7.
  Until we dropped Python 2.6 support, we were stuck on optparse.

* keystoneauth.loading.cli provides register_argparse_arguments and
  load_from_argparse_arguments helper methods. Now that we're moving
  toward Keystone Session support, argparse seems required.

Closes-Bug: 1553030
Change-Id: I5139fb64a8631a3010680090fd04345f95c55c7b
This commit is contained in:
Tim Burke 2016-03-02 16:02:28 +00:00
parent 985c038209
commit 17aa6c789e
2 changed files with 316 additions and 278 deletions
swiftclient
tests/unit

@ -16,11 +16,11 @@
from __future__ import print_function, unicode_literals from __future__ import print_function, unicode_literals
import argparse
import logging import logging
import signal import signal
import socket import socket
from optparse import OptionParser, OptionGroup, SUPPRESS_HELP
from os import environ, walk, _exit as os_exit from os import environ, walk, _exit as os_exit
from os.path import isfile, isdir, join from os.path import isfile, isdir, join
from six import text_type, PY2 from six import text_type, PY2
@ -80,22 +80,22 @@ Optional arguments:
def st_delete(parser, args, output_manager): def st_delete(parser, args, output_manager):
parser.add_option( parser.add_argument(
'-a', '--all', action='store_true', dest='yes_all', '-a', '--all', action='store_true', dest='yes_all',
default=False, help='Delete all containers and objects.') default=False, help='Delete all containers and objects.')
parser.add_option( parser.add_argument(
'-p', '--prefix', dest='prefix', '-p', '--prefix', dest='prefix',
help='Only delete items beginning with the <prefix>.') help='Only delete items beginning with the <prefix>.')
parser.add_option( parser.add_argument(
'', '--leave-segments', action='store_true', '--leave-segments', action='store_true',
dest='leave_segments', default=False, dest='leave_segments', default=False,
help='Do not delete segments of manifest objects.') help='Do not delete segments of manifest objects.')
parser.add_option( parser.add_argument(
'', '--object-threads', type=int, '--object-threads', type=int,
default=10, help='Number of threads to use for deleting objects. ' default=10, help='Number of threads to use for deleting objects. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
parser.add_option( parser.add_argument(
'', '--container-threads', type=int, '--container-threads', type=int,
default=10, help='Number of threads to use for deleting containers. ' default=10, help='Number of threads to use for deleting containers. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
(options, args) = parse_args(parser, args) (options, args) = parse_args(parser, args)
@ -263,52 +263,52 @@ Optional arguments:
def st_download(parser, args, output_manager): def st_download(parser, args, output_manager):
parser.add_option( parser.add_argument(
'-a', '--all', action='store_true', dest='yes_all', '-a', '--all', action='store_true', dest='yes_all',
default=False, help='Indicates that you really want to download ' default=False, help='Indicates that you really want to download '
'everything in the account.') 'everything in the account.')
parser.add_option( parser.add_argument(
'-m', '--marker', dest='marker', '-m', '--marker', dest='marker',
default='', help='Marker to use when starting a container or ' default='', help='Marker to use when starting a container or '
'account download.') 'account download.')
parser.add_option( parser.add_argument(
'-p', '--prefix', dest='prefix', '-p', '--prefix', dest='prefix',
help='Only download items beginning with the <prefix>.') help='Only download items beginning with the <prefix>.')
parser.add_option( parser.add_argument(
'-o', '--output', dest='out_file', help='For a single ' '-o', '--output', dest='out_file', help='For a single '
'download, stream the output to <out_file>. ' 'download, stream the output to <out_file>. '
'Specifying "-" as <out_file> will redirect to stdout.') 'Specifying "-" as <out_file> will redirect to stdout.')
parser.add_option( parser.add_argument(
'-D', '--output-dir', dest='out_directory', '-D', '--output-dir', dest='out_directory',
help='An optional directory to which to store objects. ' help='An optional directory to which to store objects. '
'By default, all objects are recreated in the current directory.') 'By default, all objects are recreated in the current directory.')
parser.add_option( parser.add_argument(
'-r', '--remove-prefix', action='store_true', dest='remove_prefix', '-r', '--remove-prefix', action='store_true', dest='remove_prefix',
default=False, help='An optional flag for --prefix <prefix>, ' default=False, help='An optional flag for --prefix <prefix>, '
'use this option to download items without <prefix>.') 'use this option to download items without <prefix>.')
parser.add_option( parser.add_argument(
'', '--object-threads', type=int, '--object-threads', type=int,
default=10, help='Number of threads to use for downloading objects. ' default=10, help='Number of threads to use for downloading objects. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
parser.add_option( parser.add_argument(
'', '--container-threads', type=int, default=10, '--container-threads', type=int, default=10,
help='Number of threads to use for downloading containers. ' help='Number of threads to use for downloading containers. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
parser.add_option( parser.add_argument(
'', '--no-download', action='store_true', '--no-download', action='store_true',
default=False, default=False,
help="Perform download(s), but don't actually write anything to disk.") help="Perform download(s), but don't actually write anything to disk.")
parser.add_option( parser.add_argument(
'-H', '--header', action='append', dest='header', '-H', '--header', action='append', dest='header',
default=[], default=[],
help='Adds a customized request header to the query, like "Range" or ' help='Adds a customized request header to the query, like "Range" or '
'"If-Match". This option may be repeated. ' '"If-Match". This option may be repeated. '
'Example: --header "content-type:text/plain"') 'Example: --header "content-type:text/plain"')
parser.add_option( parser.add_argument(
'--skip-identical', action='store_true', dest='skip_identical', '--skip-identical', action='store_true', dest='skip_identical',
default=False, help='Skip downloading files that are identical on ' default=False, help='Skip downloading files that are identical on '
'both sides.') 'both sides.')
parser.add_option( parser.add_argument(
'--no-shuffle', action='store_false', dest='shuffle', '--no-shuffle', action='store_false', dest='shuffle',
default=True, help='By default, download order is randomised in order ' default=True, help='By default, download order is randomised in order '
'to reduce the load on individual drives when multiple clients are ' 'to reduce the load on individual drives when multiple clients are '
@ -518,26 +518,26 @@ def st_list(parser, args, output_manager):
output_manager.print_msg( output_manager.print_msg(
prt_bytes(total_bytes, options.human)) prt_bytes(total_bytes, options.human))
parser.add_option( parser.add_argument(
'-l', '--long', dest='long', action='store_true', default=False, '-l', '--long', dest='long', action='store_true', default=False,
help='Long listing format, similar to ls -l.') help='Long listing format, similar to ls -l.')
parser.add_option( parser.add_argument(
'--lh', dest='human', action='store_true', '--lh', dest='human', action='store_true',
default=False, help='Report sizes in human readable format, ' default=False, help='Report sizes in human readable format, '
"similar to ls -lh.") "similar to ls -lh.")
parser.add_option( parser.add_argument(
'-t', '--totals', dest='totals', '-t', '--totals', dest='totals',
help='used with -l or --lh, only report totals.', help='used with -l or --lh, only report totals.',
action='store_true', default=False) action='store_true', default=False)
parser.add_option( parser.add_argument(
'-p', '--prefix', dest='prefix', '-p', '--prefix', dest='prefix',
help='Only list items beginning with the prefix.') help='Only list items beginning with the prefix.')
parser.add_option( parser.add_argument(
'-d', '--delimiter', dest='delimiter', '-d', '--delimiter', dest='delimiter',
help='Roll up items with the given delimiter. For containers ' help='Roll up items with the given delimiter. For containers '
'only. See OpenStack Swift API documentation for ' 'only. See OpenStack Swift API documentation for '
'what this means.') 'what this means.')
(options, args) = parse_args(parser, args) options, args = parse_args(parser, args)
args = args[1:] args = args[1:]
if options.delimiter and not args: if options.delimiter and not args:
exit('-d option only allowed for container listings') exit('-d option only allowed for container listings')
@ -595,10 +595,10 @@ Optional arguments:
def st_stat(parser, args, output_manager): def st_stat(parser, args, output_manager):
parser.add_option( parser.add_argument(
'--lh', dest='human', action='store_true', default=False, '--lh', dest='human', action='store_true', default=False,
help='Report sizes in human readable format similar to ls -lh.') help='Report sizes in human readable format similar to ls -lh.')
(options, args) = parse_args(parser, args) options, args = parse_args(parser, args)
args = args[1:] args = args[1:]
_opts = vars(options) _opts = vars(options)
@ -686,25 +686,25 @@ Optional arguments:
def st_post(parser, args, output_manager): def st_post(parser, args, output_manager):
parser.add_option( parser.add_argument(
'-r', '--read-acl', dest='read_acl', help='Read ACL for containers. ' '-r', '--read-acl', dest='read_acl', help='Read ACL for containers. '
'Quick summary of ACL syntax: .r:*, .r:-.example.com, ' 'Quick summary of ACL syntax: .r:*, .r:-.example.com, '
'.r:www.example.com, account1, account2:user2') '.r:www.example.com, account1, account2:user2')
parser.add_option( parser.add_argument(
'-w', '--write-acl', dest='write_acl', help='Write ACL for ' '-w', '--write-acl', dest='write_acl', help='Write ACL for '
'containers. Quick summary of ACL syntax: account1, ' 'containers. Quick summary of ACL syntax: account1, '
'account2:user2') 'account2:user2')
parser.add_option( parser.add_argument(
'-t', '--sync-to', dest='sync_to', help='Sets the ' '-t', '--sync-to', dest='sync_to', help='Sets the '
'Sync To for containers, for multi-cluster replication.') 'Sync To for containers, for multi-cluster replication.')
parser.add_option( parser.add_argument(
'-k', '--sync-key', dest='sync_key', help='Sets the ' '-k', '--sync-key', dest='sync_key', help='Sets the '
'Sync Key for containers, for multi-cluster replication.') 'Sync Key for containers, for multi-cluster replication.')
parser.add_option( parser.add_argument(
'-m', '--meta', action='append', dest='meta', default=[], '-m', '--meta', action='append', dest='meta', default=[],
help='Sets a meta data item. This option may be repeated. ' help='Sets a meta data item. This option may be repeated. '
'Example: -m Color:Blue -m Size:Large') 'Example: -m Color:Blue -m Size:Large')
parser.add_option( parser.add_argument(
'-H', '--header', action='append', dest='header', '-H', '--header', action='append', dest='header',
default=[], help='Adds a customized request header. ' default=[], help='Adds a customized request header. '
'This option may be repeated. ' 'This option may be repeated. '
@ -805,58 +805,58 @@ Optional arguments:
def st_upload(parser, args, output_manager): def st_upload(parser, args, output_manager):
parser.add_option( parser.add_argument(
'-c', '--changed', action='store_true', dest='changed', '-c', '--changed', action='store_true', dest='changed',
default=False, help='Only upload files that have changed since ' default=False, help='Only upload files that have changed since '
'the last upload.') 'the last upload.')
parser.add_option( parser.add_argument(
'--skip-identical', action='store_true', dest='skip_identical', '--skip-identical', action='store_true', dest='skip_identical',
default=False, help='Skip uploading files that are identical on ' default=False, help='Skip uploading files that are identical on '
'both sides.') 'both sides.')
parser.add_option( parser.add_argument(
'-S', '--segment-size', dest='segment_size', help='Upload files ' '-S', '--segment-size', dest='segment_size', help='Upload files '
'in segments no larger than <size> (in Bytes) and then create a ' 'in segments no larger than <size> (in Bytes) and then create a '
'"manifest" file that will download all the segments as if it were ' '"manifest" file that will download all the segments as if it were '
'the original file. Sizes may also be expressed as bytes with the ' 'the original file. Sizes may also be expressed as bytes with the '
'B suffix, kilobytes with the K suffix, megabytes with the M suffix ' 'B suffix, kilobytes with the K suffix, megabytes with the M suffix '
'or gigabytes with the G suffix.') 'or gigabytes with the G suffix.')
parser.add_option( parser.add_argument(
'-C', '--segment-container', dest='segment_container', '-C', '--segment-container', dest='segment_container',
help='Upload the segments into the specified container. ' help='Upload the segments into the specified container. '
'If not specified, the segments will be uploaded to a ' 'If not specified, the segments will be uploaded to a '
'<container>_segments container to not pollute the main ' '<container>_segments container to not pollute the main '
'<container> listings.') '<container> listings.')
parser.add_option( parser.add_argument(
'', '--leave-segments', action='store_true', '--leave-segments', action='store_true',
dest='leave_segments', default=False, help='Indicates that you want ' dest='leave_segments', default=False, help='Indicates that you want '
'the older segments of manifest objects left alone (in the case of ' 'the older segments of manifest objects left alone (in the case of '
'overwrites).') 'overwrites).')
parser.add_option( parser.add_argument(
'', '--object-threads', type=int, default=10, '--object-threads', type=int, default=10,
help='Number of threads to use for uploading full objects. ' help='Number of threads to use for uploading full objects. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
parser.add_option( parser.add_argument(
'', '--segment-threads', type=int, default=10, '--segment-threads', type=int, default=10,
help='Number of threads to use for uploading object segments. ' help='Number of threads to use for uploading object segments. '
'Its value must be a positive integer. Default is 10.') 'Its value must be a positive integer. Default is 10.')
parser.add_option( parser.add_argument(
'-H', '--header', action='append', dest='header', '-H', '--header', action='append', dest='header',
default=[], help='Set request headers with the syntax header:value. ' default=[], help='Set request headers with the syntax header:value. '
' This option may be repeated. Example -H "content-type:text/plain" ' ' This option may be repeated. Example -H "content-type:text/plain" '
'-H "Content-Length: 4000"') '-H "Content-Length: 4000"')
parser.add_option( parser.add_argument(
'', '--use-slo', action='store_true', default=False, '--use-slo', action='store_true', default=False,
help='When used in conjunction with --segment-size, it will ' help='When used in conjunction with --segment-size, it will '
'create a Static Large Object instead of the default ' 'create a Static Large Object instead of the default '
'Dynamic Large Object.') 'Dynamic Large Object.')
parser.add_option( parser.add_argument(
'', '--object-name', dest='object_name', '--object-name', dest='object_name',
help='Upload file and name object to <object-name> or upload dir and ' help='Upload file and name object to <object-name> or upload dir and '
'use <object-name> as object prefix instead of folder name.') 'use <object-name> as object prefix instead of folder name.')
parser.add_option( parser.add_argument(
'', '--ignore-checksum', dest='checksum', default=True, '--ignore-checksum', dest='checksum', default=True,
action='store_false', help='Turn off checksum validation for uploads.') action='store_false', help='Turn off checksum validation for uploads.')
(options, args) = parse_args(parser, args) options, args = parse_args(parser, args)
args = args[1:] args = args[1:]
if len(args) < 2: if len(args) < 2:
output_manager.error( output_manager.error(
@ -1115,7 +1115,7 @@ Optional arguments:
def st_tempurl(parser, args, thread_manager): def st_tempurl(parser, args, thread_manager):
parser.add_option( parser.add_argument(
'--absolute', action='store_true', '--absolute', action='store_true',
dest='absolute_expiry', default=False, dest='absolute_expiry', default=False,
help=("If present, seconds argument will be interpreted as a Unix " help=("If present, seconds argument will be interpreted as a Unix "
@ -1143,10 +1143,41 @@ def st_tempurl(parser, args, thread_manager):
thread_manager.print_msg(url) thread_manager.print_msg(url)
class HelpFormatter(argparse.HelpFormatter):
def _format_action_invocation(self, action):
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
metavar, = self._metavar_formatter(action, default)(1)
return metavar
else:
parts = []
# if the Optional doesn't take a value, format is:
# -s, --long
if action.nargs == 0:
parts.extend(action.option_strings)
# if the Optional takes a value, format is:
# -s=ARGS, --long=ARGS
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
for option_string in action.option_strings:
parts.append('%s=%s' % (option_string, args_string))
return ', '.join(parts)
# Back-port py3 methods
def _get_default_metavar_for_optional(self, action):
return action.dest.upper()
def _get_default_metavar_for_positional(self, action):
return action.dest
def parse_args(parser, args, enforce_requires=True): def parse_args(parser, args, enforce_requires=True):
if not args: options, args = parser.parse_known_args(args or ['-h'])
args = ['-h']
(options, args) = parser.parse_args(args)
if enforce_requires and (options.debug or options.info): if enforce_requires and (options.debug or options.info):
logging.getLogger("swiftclient") logging.getLogger("swiftclient")
if options.debug: if options.debug:
@ -1156,14 +1187,14 @@ def parse_args(parser, args, enforce_requires=True):
elif options.info: elif options.info:
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
if len(args) > 1 and args[1] == '--help': if args and options.help:
_help = globals().get('st_%s_help' % args[0], _help = globals().get('st_%s_help' % args[0],
"no help for %s" % args[0]) "no help for %s" % args[0])
print(_help) print(_help)
exit() exit()
# Short circuit for tempurl, which doesn't need auth # Short circuit for tempurl, which doesn't need auth
if len(args) > 0 and args[0] == 'tempurl': if args and args[0] == 'tempurl':
return options, args return options, args
if options.auth_version == '3.0': if options.auth_version == '3.0':
@ -1206,7 +1237,7 @@ def parse_args(parser, args, enforce_requires=True):
if (options.os_options.get('object_storage_url') and if (options.os_options.get('object_storage_url') and
options.os_options.get('auth_token') and options.os_options.get('auth_token') and
(options.auth_version == '2.0' or options.auth_version == '3')): options.auth_version in ('2.0', '3')):
return options, args return options, args
if enforce_requires: if enforce_requires:
@ -1234,17 +1265,14 @@ adding "-V 2" is necessary for this.'''.strip('\n'))
def main(arguments=None): def main(arguments=None):
if arguments: argv = sys_argv if arguments is None else arguments
argv = arguments
else:
argv = sys_argv
argv = [a if isinstance(a, text_type) else a.decode('utf-8') for a in argv] argv = [a if isinstance(a, text_type) else a.decode('utf-8') for a in argv]
version = client_version version = client_version
parser = OptionParser(version='python-swiftclient %s' % version, parser = argparse.ArgumentParser(
usage=''' add_help=False, formatter_class=HelpFormatter, usage='''
usage: %prog [--version] [--help] [--os-help] [--snet] [--verbose] %(prog)s [--version] [--help] [--os-help] [--snet] [--verbose]
[--debug] [--info] [--quiet] [--auth <auth_url>] [--debug] [--info] [--quiet] [--auth <auth_url>]
[--auth-version <auth_version> | [--auth-version <auth_version> |
--os-identity-api-version <auth_version> ] --os-identity-api-version <auth_version> ]
@ -1286,29 +1314,34 @@ Positional arguments:
auth Display auth related environment variables. auth Display auth related environment variables.
Examples: Examples:
%prog download --help %(prog)s download --help
%prog -A https://auth.api.rackspacecloud.com/v1.0 -U user -K api_key stat -v %(prog)s -A https://auth.api.rackspacecloud.com/v1.0 \\
-U user -K api_key stat -v
%prog --os-auth-url https://api.example.com/v2.0 --os-tenant-name tenant \\ %(prog)s --os-auth-url https://api.example.com/v2.0 \\
--os-tenant-name tenant \\
--os-username user --os-password password list --os-username user --os-password password list
%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\ %(prog)s --os-auth-url https://api.example.com/v3 --auth-version 3\\
--os-project-name project1 --os-project-domain-name domain1 \\ --os-project-name project1 --os-project-domain-name domain1 \\
--os-username user --os-user-domain-name domain1 \\ --os-username user --os-user-domain-name domain1 \\
--os-password password list --os-password password list
%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\ %(prog)s --os-auth-url https://api.example.com/v3 --auth-version 3\\
--os-project-id 0123456789abcdef0123456789abcdef \\ --os-project-id 0123456789abcdef0123456789abcdef \\
--os-user-id abcdef0123456789abcdef0123456789 \\ --os-user-id abcdef0123456789abcdef0123456789 \\
--os-password password list --os-password password list
%prog --os-auth-token 6ee5eb33efad4e45ab46806eac010566 \\ %(prog)s --os-auth-token 6ee5eb33efad4e45ab46806eac010566 \\
--os-storage-url https://10.1.5.2:8080/v1/AUTH_ced809b6a4baea7aeab61a \\ --os-storage-url https://10.1.5.2:8080/v1/AUTH_ced809b6a4baea7aeab61a \\
list list
%prog list --lh %(prog)s list --lh
'''.strip('\n')) '''.strip('\n'))
parser.add_argument('--version', action='version',
version='python-swiftclient %s' % version)
parser.add_argument('-h', '--help', action='store_true')
default_auth_version = '1.0' default_auth_version = '1.0'
for k in ('ST_AUTH_VERSION', 'OS_AUTH_VERSION', 'OS_IDENTITY_API_VERSION'): for k in ('ST_AUTH_VERSION', 'OS_AUTH_VERSION', 'OS_IDENTITY_API_VERSION'):
@ -1318,193 +1351,196 @@ Examples:
except KeyError: except KeyError:
pass pass
parser.add_option('--os-help', action='store_true', dest='os_help', parser.add_argument('--os-help', action='store_true', dest='os_help',
help='Show OpenStack authentication options.') help='Show OpenStack authentication options.')
parser.add_option('--os_help', action='store_true', help=SUPPRESS_HELP) parser.add_argument('--os_help', action='store_true',
parser.add_option('-s', '--snet', action='store_true', dest='snet', help=argparse.SUPPRESS)
default=False, help='Use SERVICENET internal network.') parser.add_argument('-s', '--snet', action='store_true', dest='snet',
parser.add_option('-v', '--verbose', action='count', dest='verbose', default=False, help='Use SERVICENET internal network.')
default=1, help='Print more info.') parser.add_argument('-v', '--verbose', action='count', dest='verbose',
parser.add_option('--debug', action='store_true', dest='debug', default=1, help='Print more info.')
default=False, help='Show the curl commands and results ' parser.add_argument('--debug', action='store_true', dest='debug',
'of all http queries regardless of result status.') default=False, help='Show the curl commands and '
parser.add_option('--info', action='store_true', dest='info', 'results of all http queries regardless of result '
default=False, help='Show the curl commands and results ' 'status.')
'of all http queries which return an error.') parser.add_argument('--info', action='store_true', dest='info',
parser.add_option('-q', '--quiet', action='store_const', dest='verbose', default=False, help='Show the curl commands and '
const=0, default=1, help='Suppress status output.') 'results of all http queries which return an error.')
parser.add_option('-A', '--auth', dest='auth', parser.add_argument('-q', '--quiet', action='store_const', dest='verbose',
default=environ.get('ST_AUTH'), const=0, default=1, help='Suppress status output.')
help='URL for obtaining an auth token.') parser.add_argument('-A', '--auth', dest='auth',
parser.add_option('-V', '--auth-version', '--os-identity-api-version', default=environ.get('ST_AUTH'),
dest='auth_version', help='URL for obtaining an auth token.')
default=default_auth_version, parser.add_argument('-V', '--auth-version', '--os-identity-api-version',
type=str, dest='auth_version',
help='Specify a version for authentication. ' default=default_auth_version,
'Defaults to env[ST_AUTH_VERSION], ' type=str,
'env[OS_AUTH_VERSION], env[OS_IDENTITY_API_VERSION]' help='Specify a version for authentication. '
' or 1.0.') 'Defaults to env[ST_AUTH_VERSION], '
parser.add_option('-U', '--user', dest='user', 'env[OS_AUTH_VERSION], '
default=environ.get('ST_USER'), 'env[OS_IDENTITY_API_VERSION] or 1.0.')
help='User name for obtaining an auth token.') parser.add_argument('-U', '--user', dest='user',
parser.add_option('-K', '--key', dest='key', default=environ.get('ST_USER'),
default=environ.get('ST_KEY'), help='User name for obtaining an auth token.')
help='Key for obtaining an auth token.') parser.add_argument('-K', '--key', dest='key',
parser.add_option('-R', '--retries', type=int, default=5, dest='retries', default=environ.get('ST_KEY'),
help='The number of times to retry a failed connection.') help='Key for obtaining an auth token.')
parser.add_argument('-R', '--retries', type=int, default=5, dest='retries',
help='The number of times to retry a failed '
'connection.')
default_val = config_true_value(environ.get('SWIFTCLIENT_INSECURE')) default_val = config_true_value(environ.get('SWIFTCLIENT_INSECURE'))
parser.add_option('--insecure', parser.add_argument('--insecure',
action="store_true", dest="insecure", action="store_true", dest="insecure",
default=default_val, default=default_val,
help='Allow swiftclient to access servers without ' help='Allow swiftclient to access servers without '
'having to verify the SSL certificate. ' 'having to verify the SSL certificate. '
'Defaults to env[SWIFTCLIENT_INSECURE] ' 'Defaults to env[SWIFTCLIENT_INSECURE] '
'(set to \'true\' to enable).') '(set to \'true\' to enable).')
parser.add_option('--no-ssl-compression', parser.add_argument('--no-ssl-compression',
action='store_false', dest='ssl_compression', action='store_false', dest='ssl_compression',
default=True, default=True,
help='This option is deprecated and not used anymore. ' help='This option is deprecated and not used anymore. '
'SSL compression should be disabled by default ' 'SSL compression should be disabled by default '
'by the system SSL library.') 'by the system SSL library.')
os_grp = OptionGroup(parser, "OpenStack authentication options") os_grp = parser.add_argument_group("OpenStack authentication options")
os_grp.add_option('--os-username', os_grp.add_argument('--os-username',
metavar='<auth-user-name>', metavar='<auth-user-name>',
default=environ.get('OS_USERNAME'), default=environ.get('OS_USERNAME'),
help='OpenStack username. Defaults to env[OS_USERNAME].') help='OpenStack username. Defaults to '
os_grp.add_option('--os_username', 'env[OS_USERNAME].')
help=SUPPRESS_HELP) os_grp.add_argument('--os_username',
os_grp.add_option('--os-user-id', help=argparse.SUPPRESS)
metavar='<auth-user-id>', os_grp.add_argument('--os-user-id',
default=environ.get('OS_USER_ID'), metavar='<auth-user-id>',
help='OpenStack user ID. ' default=environ.get('OS_USER_ID'),
'Defaults to env[OS_USER_ID].') help='OpenStack user ID. '
os_grp.add_option('--os_user_id', 'Defaults to env[OS_USER_ID].')
help=SUPPRESS_HELP) os_grp.add_argument('--os_user_id',
os_grp.add_option('--os-user-domain-id', help=argparse.SUPPRESS)
metavar='<auth-user-domain-id>', os_grp.add_argument('--os-user-domain-id',
default=environ.get('OS_USER_DOMAIN_ID'), metavar='<auth-user-domain-id>',
help='OpenStack user domain ID. ' default=environ.get('OS_USER_DOMAIN_ID'),
'Defaults to env[OS_USER_DOMAIN_ID].') help='OpenStack user domain ID. '
os_grp.add_option('--os_user_domain_id', 'Defaults to env[OS_USER_DOMAIN_ID].')
help=SUPPRESS_HELP) os_grp.add_argument('--os_user_domain_id',
os_grp.add_option('--os-user-domain-name', help=argparse.SUPPRESS)
metavar='<auth-user-domain-name>', os_grp.add_argument('--os-user-domain-name',
default=environ.get('OS_USER_DOMAIN_NAME'), metavar='<auth-user-domain-name>',
help='OpenStack user domain name. ' default=environ.get('OS_USER_DOMAIN_NAME'),
'Defaults to env[OS_USER_DOMAIN_NAME].') help='OpenStack user domain name. '
os_grp.add_option('--os_user_domain_name', 'Defaults to env[OS_USER_DOMAIN_NAME].')
help=SUPPRESS_HELP) os_grp.add_argument('--os_user_domain_name',
os_grp.add_option('--os-password', help=argparse.SUPPRESS)
metavar='<auth-password>', os_grp.add_argument('--os-password',
default=environ.get('OS_PASSWORD'), metavar='<auth-password>',
help='OpenStack password. Defaults to env[OS_PASSWORD].') default=environ.get('OS_PASSWORD'),
os_grp.add_option('--os_password', help='OpenStack password. Defaults to '
help=SUPPRESS_HELP) 'env[OS_PASSWORD].')
os_grp.add_option('--os-tenant-id', os_grp.add_argument('--os_password',
metavar='<auth-tenant-id>', help=argparse.SUPPRESS)
default=environ.get('OS_TENANT_ID'), os_grp.add_argument('--os-tenant-id',
help='OpenStack tenant ID. ' metavar='<auth-tenant-id>',
'Defaults to env[OS_TENANT_ID].') default=environ.get('OS_TENANT_ID'),
os_grp.add_option('--os_tenant_id', help='OpenStack tenant ID. '
help=SUPPRESS_HELP) 'Defaults to env[OS_TENANT_ID].')
os_grp.add_option('--os-tenant-name', os_grp.add_argument('--os_tenant_id',
metavar='<auth-tenant-name>', help=argparse.SUPPRESS)
default=environ.get('OS_TENANT_NAME'), os_grp.add_argument('--os-tenant-name',
help='OpenStack tenant name. ' metavar='<auth-tenant-name>',
'Defaults to env[OS_TENANT_NAME].') default=environ.get('OS_TENANT_NAME'),
os_grp.add_option('--os_tenant_name', help='OpenStack tenant name. '
help=SUPPRESS_HELP) 'Defaults to env[OS_TENANT_NAME].')
os_grp.add_option('--os-project-id', os_grp.add_argument('--os_tenant_name',
metavar='<auth-project-id>', help=argparse.SUPPRESS)
default=environ.get('OS_PROJECT_ID'), os_grp.add_argument('--os-project-id',
help='OpenStack project ID. ' metavar='<auth-project-id>',
'Defaults to env[OS_PROJECT_ID].') default=environ.get('OS_PROJECT_ID'),
os_grp.add_option('--os_project_id', help='OpenStack project ID. '
help=SUPPRESS_HELP) 'Defaults to env[OS_PROJECT_ID].')
os_grp.add_option('--os-project-name', os_grp.add_argument('--os_project_id',
metavar='<auth-project-name>', help=argparse.SUPPRESS)
default=environ.get('OS_PROJECT_NAME'), os_grp.add_argument('--os-project-name',
help='OpenStack project name. ' metavar='<auth-project-name>',
'Defaults to env[OS_PROJECT_NAME].') default=environ.get('OS_PROJECT_NAME'),
os_grp.add_option('--os_project_name', help='OpenStack project name. '
help=SUPPRESS_HELP) 'Defaults to env[OS_PROJECT_NAME].')
os_grp.add_option('--os-project-domain-id', os_grp.add_argument('--os_project_name',
metavar='<auth-project-domain-id>', help=argparse.SUPPRESS)
default=environ.get('OS_PROJECT_DOMAIN_ID'), os_grp.add_argument('--os-project-domain-id',
help='OpenStack project domain ID. ' metavar='<auth-project-domain-id>',
'Defaults to env[OS_PROJECT_DOMAIN_ID].') default=environ.get('OS_PROJECT_DOMAIN_ID'),
os_grp.add_option('--os_project_domain_id', help='OpenStack project domain ID. '
help=SUPPRESS_HELP) 'Defaults to env[OS_PROJECT_DOMAIN_ID].')
os_grp.add_option('--os-project-domain-name', os_grp.add_argument('--os_project_domain_id',
metavar='<auth-project-domain-name>', help=argparse.SUPPRESS)
default=environ.get('OS_PROJECT_DOMAIN_NAME'), os_grp.add_argument('--os-project-domain-name',
help='OpenStack project domain name. ' metavar='<auth-project-domain-name>',
'Defaults to env[OS_PROJECT_DOMAIN_NAME].') default=environ.get('OS_PROJECT_DOMAIN_NAME'),
os_grp.add_option('--os_project_domain_name', help='OpenStack project domain name. '
help=SUPPRESS_HELP) 'Defaults to env[OS_PROJECT_DOMAIN_NAME].')
os_grp.add_option('--os-auth-url', os_grp.add_argument('--os_project_domain_name',
metavar='<auth-url>', help=argparse.SUPPRESS)
default=environ.get('OS_AUTH_URL'), os_grp.add_argument('--os-auth-url',
help='OpenStack auth URL. Defaults to env[OS_AUTH_URL].') metavar='<auth-url>',
os_grp.add_option('--os_auth_url', default=environ.get('OS_AUTH_URL'),
help=SUPPRESS_HELP) help='OpenStack auth URL. Defaults to '
os_grp.add_option('--os-auth-token', 'env[OS_AUTH_URL].')
metavar='<auth-token>', os_grp.add_argument('--os_auth_url',
default=environ.get('OS_AUTH_TOKEN'), help=argparse.SUPPRESS)
help='OpenStack token. Defaults to env[OS_AUTH_TOKEN]. ' os_grp.add_argument('--os-auth-token',
'Used with --os-storage-url to bypass the ' metavar='<auth-token>',
'usual username/password authentication.') default=environ.get('OS_AUTH_TOKEN'),
os_grp.add_option('--os_auth_token', help='OpenStack token. Defaults to '
help=SUPPRESS_HELP) 'env[OS_AUTH_TOKEN]. Used with --os-storage-url '
os_grp.add_option('--os-storage-url', 'to bypass the usual username/password '
metavar='<storage-url>', 'authentication.')
default=environ.get('OS_STORAGE_URL'), os_grp.add_argument('--os_auth_token',
help='OpenStack storage URL. ' help=argparse.SUPPRESS)
'Defaults to env[OS_STORAGE_URL]. ' os_grp.add_argument('--os-storage-url',
'Overrides the storage url returned during auth. ' metavar='<storage-url>',
'Will bypass authentication when used with ' default=environ.get('OS_STORAGE_URL'),
'--os-auth-token.') help='OpenStack storage URL. '
os_grp.add_option('--os_storage_url', 'Defaults to env[OS_STORAGE_URL]. '
help=SUPPRESS_HELP) 'Overrides the storage url returned during auth. '
os_grp.add_option('--os-region-name', 'Will bypass authentication when used with '
metavar='<region-name>', '--os-auth-token.')
default=environ.get('OS_REGION_NAME'), os_grp.add_argument('--os_storage_url',
help='OpenStack region name. ' help=argparse.SUPPRESS)
'Defaults to env[OS_REGION_NAME].') os_grp.add_argument('--os-region-name',
os_grp.add_option('--os_region_name', metavar='<region-name>',
help=SUPPRESS_HELP) default=environ.get('OS_REGION_NAME'),
os_grp.add_option('--os-service-type', help='OpenStack region name. '
metavar='<service-type>', 'Defaults to env[OS_REGION_NAME].')
default=environ.get('OS_SERVICE_TYPE'), os_grp.add_argument('--os_region_name',
help='OpenStack Service type. ' help=argparse.SUPPRESS)
'Defaults to env[OS_SERVICE_TYPE].') os_grp.add_argument('--os-service-type',
os_grp.add_option('--os_service_type', metavar='<service-type>',
help=SUPPRESS_HELP) default=environ.get('OS_SERVICE_TYPE'),
os_grp.add_option('--os-endpoint-type', help='OpenStack Service type. '
metavar='<endpoint-type>', 'Defaults to env[OS_SERVICE_TYPE].')
default=environ.get('OS_ENDPOINT_TYPE'), os_grp.add_argument('--os_service_type',
help='OpenStack Endpoint type. ' help=argparse.SUPPRESS)
'Defaults to env[OS_ENDPOINT_TYPE].') os_grp.add_argument('--os-endpoint-type',
os_grp.add_option('--os_endpoint_type', metavar='<endpoint-type>',
help=SUPPRESS_HELP) default=environ.get('OS_ENDPOINT_TYPE'),
os_grp.add_option('--os-cacert', help='OpenStack Endpoint type. '
metavar='<ca-certificate>', 'Defaults to env[OS_ENDPOINT_TYPE].')
default=environ.get('OS_CACERT'), os_grp.add_argument('--os_endpoint_type',
help='Specify a CA bundle file to use in verifying a ' help=argparse.SUPPRESS)
'TLS (https) server certificate. ' os_grp.add_argument('--os-cacert',
'Defaults to env[OS_CACERT].') metavar='<ca-certificate>',
parser.disable_interspersed_args() default=environ.get('OS_CACERT'),
# call parse_args before adding os options group so that -h, --help will help='Specify a CA bundle file to use in verifying a '
# print a condensed help message without the os options 'TLS (https) server certificate. '
(options, args) = parse_args(parser, argv[1:], enforce_requires=False) 'Defaults to env[OS_CACERT].')
parser.add_option_group(os_grp) options, args = parse_args(parser, argv[1:], enforce_requires=False)
if options.os_help:
# if openstack option help has been explicitly requested then force if options.help or options.os_help:
# help message, now that os_options group has been added to parser if options.help:
argv = ['-h'] parser._action_groups.pop()
(options, args) = parse_args(parser, argv[1:], enforce_requires=False) parser.print_help()
parser.enable_interspersed_args() exit()
if not args or args[0] not in commands: if not args or args[0] not in commands:
parser.print_usage() parser.print_usage()
@ -1515,7 +1551,6 @@ Examples:
signal.signal(signal.SIGINT, immediate_exit) signal.signal(signal.SIGINT, immediate_exit)
with OutputManager() as output: with OutputManager() as output:
parser.usage = globals()['st_%s_help' % args[0]] parser.usage = globals()['st_%s_help' % args[0]]
try: try:
globals()['st_%s' % args[0]](parser, argv[1:], output) globals()['st_%s' % args[0]](parser, argv[1:], output)

@ -62,16 +62,19 @@ def _make_args(cmd, opts, os_opts, separator='-', flags=None, cmd_args=None):
args = [""] args = [""]
flags = flags or [] flags = flags or []
for k, v in opts.items(): for k, v in opts.items():
arg = "--" + k.replace("_", "-") args.append("--" + k.replace("_", "-"))
args = args + [arg, v] if v is not None:
args.append(v)
for k, v in os_opts.items(): for k, v in os_opts.items():
arg = "--os" + separator + k.replace("_", separator) args.append("--os" + separator + k.replace("_", separator))
args = args + [arg, v] if v is not None:
args.append(v)
for flag in flags: for flag in flags:
args.append('--%s' % flag) args.append('--%s' % flag)
args = args + [cmd] if cmd:
args.append(cmd)
if cmd_args: if cmd_args:
args = args + cmd_args args.extend(cmd_args)
return args return args
@ -1261,17 +1264,17 @@ class TestShell(unittest.TestCase):
self.assertEqual(output.err, "segment-size should be positive\n") self.assertEqual(output.err, "segment-size should be positive\n")
output.clear() output.clear()
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
argv = ["", "upload", "-S", "-40K", "container", "object"] argv = ["", "upload", "-S=-40K", "container", "object"]
swiftclient.shell.main(argv) swiftclient.shell.main(argv)
self.assertEqual(output.err, "segment-size should be positive\n") self.assertEqual(output.err, "segment-size should be positive\n")
output.clear() output.clear()
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
argv = ["", "upload", "-S", "-40M", "container", "object"] argv = ["", "upload", "-S=-40M", "container", "object"]
swiftclient.shell.main(argv) swiftclient.shell.main(argv)
self.assertEqual(output.err, "segment-size should be positive\n") self.assertEqual(output.err, "segment-size should be positive\n")
output.clear() output.clear()
with self.assertRaises(SystemExit): with self.assertRaises(SystemExit):
argv = ["", "upload", "-S", "-40G", "container", "object"] argv = ["", "upload", "-S=-40G", "container", "object"]
swiftclient.shell.main(argv) swiftclient.shell.main(argv)
self.assertEqual(output.err, "segment-size should be positive\n") self.assertEqual(output.err, "segment-size should be positive\n")
output.clear() output.clear()
@ -1692,17 +1695,17 @@ class TestParsing(TestBase):
def test_help(self): def test_help(self):
# --help returns condensed help message # --help returns condensed help message
opts = {"help": ""} opts = {"help": None}
os_opts = {} os_opts = {}
args = _make_args("stat", opts, os_opts) args = _make_args(None, opts, os_opts)
with CaptureOutput() as out: with CaptureOutput() as out:
self.assertRaises(SystemExit, swiftclient.shell.main, args) self.assertRaises(SystemExit, swiftclient.shell.main, args)
self.assertTrue(out.find('[--key <api_key>]') > 0) self.assertTrue(out.find('[--key <api_key>]') > 0)
self.assertEqual(-1, out.find('--os-username=<auth-user-name>')) self.assertEqual(-1, out.find('--os-username=<auth-user-name>'))
# --help returns condensed help message, overrides --os-help # --help returns condensed help message, overrides --os-help
opts = {"help": ""} opts = {"help": None}
os_opts = {"help": ""} os_opts = {"help": None}
args = _make_args("", opts, os_opts) args = _make_args("", opts, os_opts)
with CaptureOutput() as out: with CaptureOutput() as out:
self.assertRaises(SystemExit, swiftclient.shell.main, args) self.assertRaises(SystemExit, swiftclient.shell.main, args)
@ -1711,8 +1714,8 @@ class TestParsing(TestBase):
# --os-password, --os-username and --os-auth_url should be ignored # --os-password, --os-username and --os-auth_url should be ignored
# because --help overrides it # because --help overrides it
opts = {"help": ""} opts = {"help": None}
os_opts = {"help": "", os_opts = {"help": None,
"password": "secret", "password": "secret",
"username": "user", "username": "user",
"auth_url": "http://example.com:5000/v3"} "auth_url": "http://example.com:5000/v3"}