-
Notifications
You must be signed in to change notification settings - Fork 4
/
exit_test
executable file
·39 lines (34 loc) · 967 Bytes
/
exit_test
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
#!/usr/bin/env python3
import optparse
import os
import sys
def signal_exit(signal):
print('Sending ourself signal', signal)
os.kill(os.getpid(), signal)
def system_exit(status):
print('Exiting with status', status)
sys.exit(status)
def main():
p = optparse.OptionParser()
p.add_option('-s', '--signal', dest='signal', help='exit on SIGNAL')
p.add_option('-e', '--exit', dest='status',
help='exit with code STATUS')
opts, args = p.parse_args()
if opts.signal:
try:
signal = int(opts.signal)
except ValueError:
signal = opts.signal
signal_exit(signal)
elif opts.status:
try:
status = int(opts.status)
except ValueError:
status = opts.status
system_exit(status)
else:
p.print_help()
sys.stderr.write("\nError: must pass -s or -e\n")
sys.exit(2)
if __name__ == '__main__':
main()