refactor(oom-sort): improve ArgParser usage

This commit is contained in:
actionless 2018-12-16 19:15:32 +01:00
parent 0b2be659d1
commit 58cfed83a2

View File

@ -19,15 +19,15 @@ def parse_arguments():
'--num', '--num',
'-n', '-n',
help="""max number of lines; default: 99999""", help="""max number of lines; default: 99999""",
default=None, default=99999,
type=str type=int
) )
parser.add_argument( parser.add_argument(
'--len', '--len',
'-l', '-l',
help="""max cmdline length; default: 99999""", help="""max cmdline length; default: 99999""",
default=None, default=99999,
type=str type=int
) )
parser.add_argument( parser.add_argument(
'--refresh', '--refresh',
@ -145,7 +145,7 @@ class ProcessInfo:
self.name.ljust(15), self.name.ljust(15),
human_readable(self.vm_rss), human_readable(self.vm_rss),
human_readable(self.vm_swap), human_readable(self.vm_swap),
self.cmdline[:int(display_cmdline)] self.cmdline[:display_cmdline]
) )
@ -158,15 +158,8 @@ class Application:
def __init__(self): def __init__(self):
args = parse_arguments() args = parse_arguments()
self.num_lines = args.num self.num_lines = args.num
if self.num_lines is None:
self.num_lines = 99999
self.display_cmdline = args.len self.display_cmdline = args.len
if self.display_cmdline is None:
self.display_cmdline = 99999
self.refresh_interval = args.refresh self.refresh_interval = args.refresh
def print_stats(self): def print_stats(self):
@ -184,7 +177,7 @@ class Application:
oom_list.append(proc_info) oom_list.append(proc_info)
oom_list.sort(key=lambda p: p.oom_score, reverse=True) oom_list.sort(key=lambda p: p.oom_score, reverse=True)
if self.display_cmdline == '0': if self.display_cmdline == 0:
print('oom_score oom_score_adj UID PID Name VmRSS VmSwap') print('oom_score oom_score_adj UID PID Name VmRSS VmSwap')
print('--------- ------------- ----- ----- --------------- -------- --------') print('--------- ------------- ----- ----- --------------- -------- --------')
else: else:
@ -192,7 +185,7 @@ class Application:
print('--------- ------------- ----- ----- --------------- -------- -------- -------') print('--------- ------------- ----- ----- --------------- -------- -------- -------')
# iterate through list sorted by oom_score and print name, pid, etc # iterate through list sorted by oom_score and print name, pid, etc
for proc_info in oom_list[:int(self.num_lines)]: for proc_info in oom_list[:self.num_lines]:
if proc_info.read_status(): if proc_info.read_status():
print( print(
proc_info.format_output( proc_info.format_output(