#!/usr/bin/env python3
import sys
import argparse
import logging
import prettytable
import csv
parser = argparse.ArgumentParser()
parser.add_argument(
"infile", nargs="?",
type=argparse.FileType("r"),
default=sys.stdin
)
parser.add_argument(
'-d', '--debug',
help="Print lots of debugging statements",
action="store_const", dest="loglevel", const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
'-v', '--verbose',
help="Be verbose",
action="store_const", dest="loglevel", const=logging.INFO,
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
if args.infile.isatty():
print("Please enter your data:")
try:
if not args.infile.seekable():
from io import StringIO
args.infile = StringIO(args.infile.read())
table = prettytable.from_csv(args.infile)
print(table)
except Exception as e:
logging.error(e, exc_info=(args.loglevel<=logging.INFO))
exit(1)
Explain why I wouldn't exit when reading stdin; trackback is debug level, not info
Toby Speight
- 88.4k
- 14
- 104
- 327