Skip to content

Commit

Permalink
tcptop: Cleanup argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gobenji authored and drzaeus77 committed Aug 11, 2017
1 parent 890c76a commit 8e86b9e
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions tools/tcptop.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
import ctypes as ct

# 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:
./tcptop # trace TCP send/recv by host
./tcptop -C # don't clear the screen
Expand All @@ -49,15 +56,11 @@
help="skip system summary line")
parser.add_argument("-p", "--pid",
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)")
parser.add_argument("count", nargs="?", default=99999999,
parser.add_argument("count", nargs="?", default=-1, type=range_check,
help="number of outputs")
args = parser.parse_args()
countdown = int(args.count)
if args.interval and int(args.interval) == 0:
print("ERROR: interval 0. Exiting.")
exit()
debug = 0

# linux stats
Expand Down Expand Up @@ -204,15 +207,13 @@ def pid_to_comm(pid):
print('Tracing... Output every %s secs. Hit Ctrl-C to end' % args.interval)

# output
exiting = 0
while (1):
i = 0
exiting = False
while i != args.count and not exiting:
try:
if args.interval:
sleep(int(args.interval))
else:
sleep(99999999)
sleep(args.interval)
except KeyboardInterrupt:
exiting = 1
exiting = True

# header
if args.noclear:
Expand Down Expand Up @@ -282,6 +283,4 @@ def pid_to_comm(pid):
ipv6_send_bytes.clear()
ipv6_recv_bytes.clear()

countdown -= 1
if exiting or countdown == 0:
exit()
i += 1

0 comments on commit 8e86b9e

Please sign in to comment.