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 Jan 19, 2021
1 parent 2b65ae9 commit 02f1062
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 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 @@ -74,13 +80,15 @@ def main(argv=None):
for f in args.input:
if f.isatty():
print(
"You are running chardetect interactively. Press "
+ "CTRL-D twice at the start of a blank line to signal the "
+ "end of your input. If you want help, run chardetect "
+ "--help\n",
(
"You are running chardetect interactively. Press "
"CTRL-D twice at the start of a blank line to signal the "
"end of your input. If you want help, run chardetect "
"--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 02f1062

Please sign in to comment.