diff options
| author | Claude Paroz <claude@2xlibre.net> | 2014-06-06 21:12:18 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2014-06-09 10:13:37 +0200 |
| commit | 96e4b52ab2c6fae3e46a197d44c42cb6ebde7d9c (patch) | |
| tree | be218b36b465879e20c34aff30c39ecdd777221a /extras | |
| parent | 7018bcfb71847a3f016d8f65cb60f72fcbb22f5a (diff) | |
Converted Django scripts to argparse
Refs #19973.
Diffstat (limited to 'extras')
| -rwxr-xr-x | extras/csrf_migration_helper.py | 31 |
1 files changed, 10 insertions, 21 deletions
diff --git a/extras/csrf_migration_helper.py b/extras/csrf_migration_helper.py index 9e7b04b85b..e7df65d056 100755 --- a/extras/csrf_migration_helper.py +++ b/extras/csrf_migration_helper.py @@ -112,25 +112,15 @@ PYTHON_ENCODING = "UTF-8" # and fail to find real instances. +from argparse import ArgumentParser import os import sys import re -from optparse import OptionParser -USAGE = """ -This tool helps to locate forms that need CSRF tokens added and the +DESCRIPTION = """This tool helps to locate forms that need CSRF tokens added and the corresponding view code. This processing is NOT fool proof, and you should read the help contained in the script itself. Also, this script may need configuring -(by editing the script) before use. - -Usage: - -python csrf_migration_helper.py [--settings=path.to.your.settings] /path/to/python/code [more paths...] - - Paths can be specified as relative paths. - - With no arguments, this help is printed. -""" +(by editing the script) before use.""" _POST_FORM_RE = \ re.compile(r'(<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE) @@ -347,21 +337,20 @@ def main(pythonpaths): print("----") -parser = OptionParser(usage=USAGE) -parser.add_option("", "--settings", action="store", dest="settings", help="Dotted path to settings file") - if __name__ == '__main__': - options, args = parser.parse_args() - if len(args) == 0: + parser = ArgumentParser(description=DESCRIPTION) + parser.add_argument('files', nargs='*', help='Paths can be specified as relative paths.') + parser.add_argument("--settings", help="Dotted path to settings file") + options = parser.parse_args() + if len(options.files) == 0: parser.print_help() sys.exit(1) - settings = getattr(options, 'settings', None) - if settings is None: + if options.settings is None: if os.environ.get("DJANGO_SETTINGS_MODULE", None) is None: print("You need to set DJANGO_SETTINGS_MODULE or use the '--settings' parameter") sys.exit(1) else: os.environ["DJANGO_SETTINGS_MODULE"] = settings - main(args) + main(options.files) |
