Skip to content

Commit

Permalink
better CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Jan 14, 2021
1 parent 8cb68be commit c1c76c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Empty file added flake8_codes/__init__.py
Empty file.
14 changes: 10 additions & 4 deletions flake8_codes/_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from argparse import ArgumentParser
from typing import Iterator, List, NamedTuple, NoReturn, TextIO

from flake8.main.application import Application
Expand Down Expand Up @@ -34,14 +35,14 @@ def get_codes(lookup_name: str) -> Iterator[Code]:

is_prefix = lookup_name.startswith(tuple(plugin.codes))
is_name = normalize(lookup_name) == normalize(plugin.name)
if not is_name and not is_prefix:
if lookup_name and not is_name and not is_prefix:
continue

try:
codes = extract(plugin.name)
except ImportError:
continue
for code in codes:
for code in sorted(codes):
if is_prefix and not code.startswith(lookup_name):
continue
yield Code(
Expand All @@ -55,12 +56,17 @@ def print_codes(lookup_name: str, stream: TextIO) -> int:
count = 0
for code in get_codes(lookup_name):
count += 1
print(TEMPLATE.format(c=code))
print(TEMPLATE.format(c=code), file=stream)
return count


def main(argv: List[str], stream: TextIO) -> int:
count = print_codes(argv[0], stream=stream)
parser = ArgumentParser()
parser.add_argument('lookup_name', nargs='?', help='plugin name, code, or prefix')
args = parser.parse_args(argv)
lookup_name = args.lookup_name or ''

count = print_codes(lookup_name, stream=stream)
return int(count == 0)


Expand Down

0 comments on commit c1c76c4

Please sign in to comment.