Skip to content

Commit

Permalink
Use argparse instead of optparse (jendrikseipp#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ722 authored and jendrikseipp committed Jun 5, 2018
1 parent 5f77d81 commit e3ea074
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ News
0.27 (2018-06-05)
-----------------
* Report ``while (True): ... else: ...`` as unreachable (thanks @RJ722).
* Use ``argparse`` instead of ``optparse``.
* Whitelist Mock.return_value and Mock.side_effect in unittest.mock module.
* Drop support for Python 2.6 and 3.3.
* Improve documentation and test coverage (thanks @RJ722).
Expand Down
1 change: 0 additions & 1 deletion TODO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ TODOs
* Uuse coverage.py to detect false-positives (#109).
* Write script for turning SIP files into whitelists (#8).
* Add PyQT whitelist to Vulture repo.
* Use argparse instead of optparse.


Non-TODOs
Expand Down
11 changes: 11 additions & 0 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ def test_min_confidence():
assert call_vulture([
'vulture/core.py', '--exclude', 'whitelists',
'--min-confidence', '100']) == 0


def test_exclude():
def mix_path(filenames):
return ",".join(
[os.path.join('vulture', filename) for filename in filenames])
assert call_vulture([
'vulture/', '--exclude', mix_path(['core.py', 'utils.py'])]) == 1
assert call_vulture([
'vulture/', '--exclude',
mix_path(['core.py', 'utils.py', 'lines.py'])]) == 0
62 changes: 29 additions & 33 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

from __future__ import print_function

import argparse
import ast
from fnmatch import fnmatchcase
import optparse
import os.path
import pkgutil
import re
Expand Down Expand Up @@ -491,40 +491,36 @@ def generic_visit(self, node):


def _parse_args():
def csv(option, _, value, parser):
setattr(parser.values, option.dest, value.split(','))
usage = """\
usage: %prog [options] PATH [PATH ...]
Paths may be Python files or directories. For each directory vulture
analyzes all contained *.py files.
"""
def csv(exclude):
return exclude.split(',')
usage = "%(prog)s [options] PATH [PATH ...]"
version = "vulture {0}".format(__version__)
parser = optparse.OptionParser(usage=usage, version=version)
parser.add_option(
'--exclude', action='callback', callback=csv, metavar='PATTERN',
type='string', default=[],
help=(
'Comma-separated list of paths to ignore (e.g.,'
' *settings.py,docs/*.py). PATTERNs can contain globbing'
' characters (*, ?, [, ]). Treat PATTERNs without globbing'
' characters as *PATTERN*.'))
parser.add_option(
"--sort-by-size", action="store_true",
help="Sort unused functions and classes by their lines of code")
parser.add_option('-v', '--verbose', action='store_true')
parser.add_option(
'--min-confidence', action='store', type='int', default=0, help=(
'Minimum confidence (between 0 and 100) for code to be'
' reported as unused.'))
options, args = parser.parse_args()
return options, args
parser = argparse.ArgumentParser(prog='vulture', usage=usage)
parser.add_argument(
'paths', action='store', nargs='+', metavar='PATH', help='Paths'
' may be Python files or directories. For each directory'
' Vulture analyzes all contained *.py files.')
parser.add_argument(
'--exclude', metavar='PATTERN', type=csv, help='Comma-separated list'
' of paths to ignore (e.g., *settings.py,docs/*.py). PATTERNs can'
' contain globbing characters (*, ?, [, ]). Treat PATTERNs without'
' globbing characters as *PATTERN*.')
parser.add_argument(
'--min-confidence', action='store', type=int, default=0, help='Minimum'
' confidence (between 0 and 100) for code to be'
' reported as unused.')
parser.add_argument(
"--sort-by-size", action="store_true", help='Sort'
' unused functions and classes by their lines of code')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('--version', action='version', version=version)
return parser.parse_args()


def main():
options, args = _parse_args()
vulture = Vulture(verbose=options.verbose)
vulture.scavenge(args, exclude=options.exclude)
args = _parse_args()
vulture = Vulture(verbose=args.verbose)
vulture.scavenge(args.paths, exclude=args.exclude)
sys.exit(vulture.report(
min_confidence=options.min_confidence,
sort_by_size=options.sort_by_size))
min_confidence=args.min_confidence,
sort_by_size=args.sort_by_size))

0 comments on commit e3ea074

Please sign in to comment.