Skip to content

Commit

Permalink
Add --minimal flag to chardetect command
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-blanchard committed Sep 7, 2021
1 parent 49b8341 commit f838799
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions chardet/cli/chardetect.py
Expand Up @@ -20,7 +20,7 @@
from ..universaldetector import UniversalDetector


def description_of(lines, name="stdin"):
def description_of(lines, name="stdin", minimal=False):
"""
Return a string describing the probable encoding of a file or
list of strings.
Expand All @@ -39,10 +39,11 @@ def description_of(lines, name="stdin"):
break
u.close()
result = u.result
if minimal:
return result["encoding"]
if result["encoding"]:
return f'{name}: {result["encoding"]} with confidence {result["confidence"]}'
else:
return f"{name}: no result"
return f"{name}: no result"


def main(argv=None):
Expand All @@ -55,17 +56,22 @@ def main(argv=None):
"""
# Get command line arguments
parser = argparse.ArgumentParser(
description="Takes one or more file paths and reports their detected \
encodings"
description=(
"Takes one or more file paths and reports their detected encodings"
)
)
parser.add_argument(
"input",
help="File whose encoding we would like to determine. \
(default: stdin)",
help="File whose encoding we would like to determine. (default: stdin)",
type=argparse.FileType("rb"),
nargs="*",
default=[sys.stdin.buffer],
)
parser.add_argument(
"--minimal",
help="Print only the encoding to standard output",
action="store_true",
)
parser.add_argument(
"--version", action="version", version=f"%(prog)s {__version__}"
)
Expand All @@ -80,7 +86,7 @@ def main(argv=None):
"--help\n",
file=sys.stderr,
)
print(description_of(f, f.name))
print(description_of(f, f.name, minimal=args.minimal))


if __name__ == "__main__":
Expand Down

0 comments on commit f838799

Please sign in to comment.