Commit 8e86b9e9 authored by Benjamin Poirier's avatar Benjamin Poirier Committed by Brenden Blanco

tcptop: Cleanup argument parsing

parent 890c76ab
...@@ -34,6 +34,13 @@ from subprocess import call ...@@ -34,6 +34,13 @@ from subprocess import call
import ctypes as ct import ctypes as ct
# arguments # arguments
def range_check(string):
value = int(string)
if value < 1:
msg = "value must be stricly positive, got %d" % (value,)
raise argparse.ArgumentTypeError(msg)
return value
examples = """examples: examples = """examples:
./tcptop # trace TCP send/recv by host ./tcptop # trace TCP send/recv by host
./tcptop -C # don't clear the screen ./tcptop -C # don't clear the screen
...@@ -49,15 +56,11 @@ parser.add_argument("-S", "--nosummary", action="store_true", ...@@ -49,15 +56,11 @@ parser.add_argument("-S", "--nosummary", action="store_true",
help="skip system summary line") help="skip system summary line")
parser.add_argument("-p", "--pid", parser.add_argument("-p", "--pid",
help="trace this PID only") help="trace this PID only")
parser.add_argument("interval", nargs="?", default=1, parser.add_argument("interval", nargs="?", default=1, type=range_check,
help="output interval, in seconds (default 1)") help="output interval, in seconds (default 1)")
parser.add_argument("count", nargs="?", default=99999999, parser.add_argument("count", nargs="?", default=-1, type=range_check,
help="number of outputs") help="number of outputs")
args = parser.parse_args() args = parser.parse_args()
countdown = int(args.count)
if args.interval and int(args.interval) == 0:
print("ERROR: interval 0. Exiting.")
exit()
debug = 0 debug = 0
# linux stats # linux stats
...@@ -204,15 +207,13 @@ ipv6_recv_bytes = b["ipv6_recv_bytes"] ...@@ -204,15 +207,13 @@ ipv6_recv_bytes = b["ipv6_recv_bytes"]
print('Tracing... Output every %s secs. Hit Ctrl-C to end' % args.interval) print('Tracing... Output every %s secs. Hit Ctrl-C to end' % args.interval)
# output # output
exiting = 0 i = 0
while (1): exiting = False
while i != args.count and not exiting:
try: try:
if args.interval: sleep(args.interval)
sleep(int(args.interval))
else:
sleep(99999999)
except KeyboardInterrupt: except KeyboardInterrupt:
exiting = 1 exiting = True
# header # header
if args.noclear: if args.noclear:
...@@ -282,6 +283,4 @@ while (1): ...@@ -282,6 +283,4 @@ while (1):
ipv6_send_bytes.clear() ipv6_send_bytes.clear()
ipv6_recv_bytes.clear() ipv6_recv_bytes.clear()
countdown -= 1 i += 1
if exiting or countdown == 0:
exit()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment