This repository was archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Script
More file actions
58 lines (50 loc) · 1.37 KB
/
Python Script
File metadata and controls
58 lines (50 loc) · 1.37 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
51
52
53
54
55
56
57
58
#!/usr/bin/env python
from __future__ import print_function
import sys, os, argparse
from io import DEFAULT_BUFFER_SIZE as DEFAULT
sys.tracebacklimit = 0
APP = "Python"
CODE = 0
def example(FILENAME):
print(FILENAME)
def example_with_option(FILENAME):
print("example:", FILENAME)
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(
description='Example description.')
group_one = parser.add_mutually_exclusive_group()
group_one.add_argument('-e', '--example', help='Example option', action="store_true", default=False)
parser.add_argument('FILE', action='store', nargs='*')
args = parser.parse_args()
STDIN = False
if len(args.FILE) == 0:
args.FILE = ['/dev/stdin']
STDIN = True
for ARG in args.FILE:
if STDIN == True:
ARGNAME = "-"
else:
ARGNAME = ARG
if os.path.isdir(ARG):
print(APP + ":", ARGNAME + ": Is a directory", file=sys.stderr)
CODE = 1
elif os.path.exists(ARG):
try:
with open(ARG) as F:
FILEPATH = ARG
except IOError:
print(APP + ":", ARGNAME + ": Permission denied", file=sys.stderr)
CODE = 1
continue
if args.example == False:
example(FILEPATH)
else:
example_with_option(FILEPATH)
else:
print (APP + ":", ARGNAME + ": No such file or directory", file=sys.stderr)
CODE = 1
except KeyboardInterrupt:
print ('')
sys.exit(130)
sys.exit(CODE)