38 lines
1 KiB
Python
38 lines
1 KiB
Python
import argparse
|
|
from pathlib import Path
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(message)s',
|
|
)
|
|
|
|
logger: logging.Logger = logging.getLogger(__name__)
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='String counting tool')
|
|
parser.add_argument('filename', help='path to the file')
|
|
parser.add_argument('--lines', '-l', help='count lines', action="store_true")
|
|
parser.add_argument('--words', '-w', help='count words', action="store_true")
|
|
parser.add_argument('--chars', '-c', help='count chars', action="store_true")
|
|
|
|
args: argparse.Namespace = parser.parse_args()
|
|
|
|
try:
|
|
file: Path = Path(args.filename)
|
|
content: str = file.read_text()
|
|
|
|
if not (args.lines or args.words or args.chars):
|
|
args.lines = args.words = args.chars = True
|
|
|
|
if args.lines:
|
|
print(f"Lines: {len(content.splitlines())}")
|
|
|
|
if args.words:
|
|
print(f"Words: {len(content.split())}")
|
|
|
|
if args.chars:
|
|
print(f"Chars: {len(content)}")
|
|
|
|
except FileNotFoundError:
|
|
logger.error(f"File {args.filename} not found")
|