-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython script.py
More file actions
executable file
·52 lines (40 loc) · 1.67 KB
/
Copy pathPython script.py
File metadata and controls
executable file
·52 lines (40 loc) · 1.67 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
#!/usr/bin/env python3
import argparse
import logging
import sys
from typing import Union, Optional, NoReturn
DESCRIPTION = """"""
def make_argparser():
parser = argparse.ArgumentParser(add_help=False, description=DESCRIPTION)
options = parser.add_argument_group('Options')
options.add_argument('positional1', metavar='dispname',
help='')
options.add_argument('-h', '--help', action='help',
help='Print this argument help text and exit.')
logs = parser.add_argument_group('Logging')
logs.add_argument('-l', '--log', type=argparse.FileType('w'), default=sys.stderr,
help='Print log messages to this file instead of to stderr. Warning: Will overwrite the file.')
volume = logs.add_mutually_exclusive_group()
volume.add_argument('-q', '--quiet', dest='volume', action='store_const', const=logging.CRITICAL,
default=logging.WARNING)
volume.add_argument('-v', '--verbose', dest='volume', action='store_const', const=logging.INFO)
volume.add_argument('-D', '--debug', dest='volume', action='store_const', const=logging.DEBUG)
return parser
def main(*argv: str) -> Optional[int]:
parser = make_argparser()
args = parser.parse_args(argv[1:])
logging.basicConfig(stream=args.log, level=args.volume, format='%(message)s')
return None
def fail(error: Union[str,BaseException], code: int = 1) -> NoReturn:
if __name__ == '__main__':
logging.critical(f'Error: {error}')
sys.exit(code)
elif isinstance(error, BaseException):
raise error
else:
raise RuntimeError(error)
if __name__ == '__main__':
try:
sys.exit(main(*sys.argv))
except BrokenPipeError:
pass