-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-cli.py
More file actions
50 lines (42 loc) · 1.65 KB
/
simple-cli.py
File metadata and controls
50 lines (42 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import argparse
import sys
""" Useful references
# Python Library: https://docs.python.org/dev/library/argparse.html
# Nargs usage: https://docs.python.org/dev/library/argparse.html#nargs
# Tutorial: https://docs.python.org/dev/howto/argparse.html
# PEP 8 – Style Guide for Python Code: https://peps.python.org/pep-0008/
# PyFormat Using % and .format() for great good!: https://pyformat.info/
# Search Python Standard Library: https://docs.python.org/3/library/index.html
# Logging Best Practices: https://www.loggly.com/use-cases/6-python-logging-best-practices-you-should-be-aware-of/
"""
if __name__ == '__main__':
arguments = None
parser = argparse.ArgumentParser(
description='Copy infile to outfile adding line numbers',
epilog="That's all Folks! ... Porky Pig"
)
parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument(
'infile', nargs='?', type=argparse.FileType('r', encoding='utf-8'), help='filename, omitted reads from STDIN',
default=sys.stdin
)
parser.add_argument(
'outfile', nargs='?', type=argparse.FileType('w', encoding='utf-8'), help='filename, omitted writes to STDOUT',
default=sys.stdout
)
args = parser.parse_args()
if args.verbose >= 1:
print(f"args: {args.__str__()}")
try:
count = 0
lines = args.infile.readlines()
for line in lines:
count += 1
args.outfile.write(f"{count:03d}: {line}")
args.outfile.flush()
args.outfile.close()
args.infile.close()
sys.exit(0)
except Exception as error:
print(f"{error}")
sys.exit(1)