Skip to content

Commit fc027fe

Browse files
committed
Reverse the logic of --no-XXX option
We avoid propagating the negation and instead use a meaningful name as "dest", set the action to "store_false" and the default to True. Also the "dest" for "--no-database" is changed to "database" for consistency with other options. When building the Flag value, in Flag.load(), we now follow the order of options definition. Finally, we define a flag() helper to factor out the common definition pattern of these options.
1 parent 65a825d commit fc027fe

File tree

6 files changed

+91
-167
lines changed

6 files changed

+91
-167
lines changed

pgactivity/cli.py

Lines changed: 21 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from argparse import ArgumentParser
99
from io import StringIO
10+
from typing import Any
1011

1112
from blessed import Terminal
1213

@@ -44,6 +45,10 @@ def configure_logger(debug_file: str | None = None) -> StringIO:
4445
return memory_string
4546

4647

48+
def flag(p: Any, spec: str, *, dest: str, help: str) -> None:
49+
p.add_argument(spec, dest=dest, help=help, action="store_false", default=True)
50+
51+
4752
def get_parser() -> ArgumentParser:
4853
parser = ArgumentParser(
4954
usage="%(prog)s [options] [connection string]",
@@ -102,29 +107,13 @@ def get_parser() -> ArgumentParser:
102107
default=None,
103108
)
104109
# --no-db-size
105-
group.add_argument(
106-
"--no-db-size",
107-
dest="nodbsize",
108-
action="store_true",
109-
help="Skip total size of DB.",
110-
default=False,
111-
)
110+
flag(group, "--no-db-size", dest="dbsize", help="Skip total size of DB.")
112111
# --no-tempfiles
113-
group.add_argument(
114-
"--no-tempfiles",
115-
dest="notempfiles",
116-
action="store_true",
117-
help="Skip tempfile count and size.",
118-
default=False,
112+
flag(
113+
group, "--no-tempfiles", dest="tempfiles", help="Skip tempfile count and size."
119114
)
120115
# --no-walreceiver
121-
group.add_argument(
122-
"--no-walreceiver",
123-
dest="nowalreceiver",
124-
action="store_true",
125-
help="Skip walreceiver checks.",
126-
default=False,
127-
)
116+
flag(group, "--no-walreceiver", dest="walreceiver", help="Skip walreceiver checks.")
128117
# --wrap-query
129118
group.add_argument(
130119
"-w",
@@ -239,94 +228,29 @@ def get_parser() -> ArgumentParser:
239228
"Process table display options",
240229
"These options may be used hide some columns from the processes table.",
241230
)
231+
242232
# --no-pid
243-
group.add_argument(
244-
"--no-pid",
245-
dest="nopid",
246-
action="store_true",
247-
help="Disable PID.",
248-
default=False,
249-
)
233+
flag(group, "--no-pid", dest="pid", help="Disable PID.")
250234
# --no-database
251-
group.add_argument(
252-
"--no-database",
253-
dest="nodb",
254-
action="store_true",
255-
help="Disable DATABASE.",
256-
default=False,
257-
)
235+
flag(group, "--no-database", dest="database", help="Disable DATABASE.")
258236
# --no-user
259-
group.add_argument(
260-
"--no-user",
261-
dest="nouser",
262-
action="store_true",
263-
help="Disable USER.",
264-
default=False,
265-
)
237+
flag(group, "--no-user", dest="user", help="Disable USER.")
266238
# --no-client
267-
group.add_argument(
268-
"--no-client",
269-
dest="noclient",
270-
action="store_true",
271-
help="Disable CLIENT.",
272-
default=False,
273-
)
239+
flag(group, "--no-client", dest="client", help="Disable CLIENT.")
274240
# --no-cpu
275-
group.add_argument(
276-
"--no-cpu",
277-
dest="nocpu",
278-
action="store_true",
279-
help="Disable CPU%%.",
280-
default=False,
281-
)
241+
flag(group, "--no-cpu", dest="cpu", help="Disable CPU%%.")
282242
# --no-mem
283-
group.add_argument(
284-
"--no-mem",
285-
dest="nomem",
286-
action="store_true",
287-
help="Disable MEM%%.",
288-
default=False,
289-
)
243+
flag(group, "--no-mem", dest="mem", help="Disable MEM%%.")
290244
# --no-read
291-
group.add_argument(
292-
"--no-read",
293-
dest="noread",
294-
action="store_true",
295-
help="Disable READ/s.",
296-
default=False,
297-
)
245+
flag(group, "--no-read", dest="read", help="Disable READ/s.")
298246
# --no-write
299-
group.add_argument(
300-
"--no-write",
301-
dest="nowrite",
302-
action="store_true",
303-
help="Disable WRITE/s.",
304-
default=False,
305-
)
247+
flag(group, "--no-write", dest="write", help="Disable WRITE/s.")
306248
# --no-time
307-
group.add_argument(
308-
"--no-time",
309-
dest="notime",
310-
action="store_true",
311-
help="Disable TIME+.",
312-
default=False,
313-
)
249+
flag(group, "--no-time", dest="time", help="Disable TIME+.")
314250
# --no-wait
315-
group.add_argument(
316-
"--no-wait",
317-
dest="nowait",
318-
action="store_true",
319-
help="Disable W.",
320-
default=False,
321-
)
251+
flag(group, "--no-wait", dest="wait", help="Disable W.")
322252
# --no-app-name
323-
group.add_argument(
324-
"--no-app-name",
325-
dest="noappname",
326-
action="store_true",
327-
help="Disable APP.",
328-
default=False,
329-
)
253+
flag(group, "--no-app-name", dest="appname", help="Disable APP.")
330254

331255
group = parser.add_argument_group("Header display options")
332256
# --no-inst-info

pgactivity/config.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,46 +110,46 @@ def load(
110110
config: Configuration | None,
111111
*,
112112
is_local: bool,
113-
noappname: bool,
114-
noclient: bool,
115-
nocpu: bool,
116-
nodb: bool,
117-
nomem: bool,
118-
nopid: bool,
119-
noread: bool,
120-
notime: bool,
121-
nouser: bool,
122-
nowait: bool,
123-
nowrite: bool,
113+
appname: bool,
114+
client: bool,
115+
cpu: bool,
116+
database: bool,
117+
mem: bool,
118+
pid: bool,
119+
read: bool,
120+
time: bool,
121+
user: bool,
122+
wait: bool,
123+
write: bool,
124124
**kwargs: Any,
125125
) -> Flag:
126126
"""Build a Flag value from command line options."""
127127
if config:
128128
flag = cls.from_config(config)
129129
else:
130130
flag = cls.all()
131-
if nodb:
131+
if not pid:
132+
flag ^= cls.PID
133+
if not database:
132134
flag ^= cls.DATABASE
133-
if nouser:
135+
if not user:
134136
flag ^= cls.USER
135-
if nocpu:
136-
flag ^= cls.CPU
137-
if noclient:
137+
if not client:
138138
flag ^= cls.CLIENT
139-
if nomem:
139+
if not cpu:
140+
flag ^= cls.CPU
141+
if not mem:
140142
flag ^= cls.MEM
141-
if noread:
143+
if not read:
142144
flag ^= cls.READ
143-
if nowrite:
145+
if not write:
144146
flag ^= cls.WRITE
145-
if notime:
147+
if not time:
146148
flag ^= cls.TIME
147-
if nowait:
149+
if not wait:
148150
flag ^= cls.WAIT
149-
if noappname:
151+
if not appname:
150152
flag ^= cls.APPNAME
151-
if nopid:
152-
flag ^= cls.PID
153153

154154
# Remove some if no running against local pg server.
155155
if not is_local and (flag & cls.CPU):

pgactivity/ui.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ def main(
2929

3030
is_local = data.pg_is_local() and data.pg_is_local_access()
3131

32-
skip_db_size = options.nodbsize
32+
skip_db_size = not options.dbsize
3333
server_information = data.pg_get_server_information(
3434
prev_server_info=None,
3535
using_rds=options.rds,
36-
skip_db_size=options.nodbsize,
37-
skip_tempfile=options.notempfiles,
38-
skip_walreceiver=options.nowalreceiver,
36+
skip_db_size=skip_db_size,
37+
skip_tempfile=not options.tempfiles,
38+
skip_walreceiver=not options.walreceiver,
3939
)
4040

4141
flag = Flag.load(config, is_local=is_local, **vars(options))
@@ -74,7 +74,7 @@ def main(
7474
break
7575
elif not ui.interactive() and key == keys.SPACE:
7676
ui.toggle_pause()
77-
elif options.nodbsize and key == keys.REFRESH_DB_SIZE:
77+
elif not options.dbsize and key == keys.REFRESH_DB_SIZE:
7878
skip_db_size = False
7979
elif key is not None:
8080
if keys.is_process_next(key):
@@ -184,15 +184,15 @@ def main(
184184

185185
else:
186186
if not ui.in_pause and not ui.interactive():
187-
if options.nodbsize and not skip_db_size:
187+
if not options.dbsize and not skip_db_size:
188188
skip_db_size = True
189189

190190
server_information = data.pg_get_server_information(
191191
prev_server_info=server_information,
192192
using_rds=options.rds,
193193
skip_db_size=skip_db_size,
194-
skip_tempfile=options.notempfiles,
195-
skip_walreceiver=options.nowalreceiver,
194+
skip_tempfile=not options.tempfiles,
195+
skip_walreceiver=not options.walreceiver,
196196
)
197197
memory, swap, load = activities.mem_swap_load()
198198
system_info = types.SystemInfo.default(

tests/test_cli.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ libpq 9.2).
8181

8282
>>> ns = parser.parse_args(["--no-db-size", "-w", "-p", "5433", "--no-pid", "--no-app-name"])
8383
>>> vars(ns)
84-
{'profile': None, 'blocksize': 4096, 'rds': False, 'output': None, 'nodbsize': True, 'notempfiles': False, 'nowalreceiver': False, 'wrap_query': True, 'durationmode': '1', 'minduration': 0, 'filters': [], 'debug_file': None, 'help': False, 'connection_string': '', 'host': None, 'port': '5433', 'username': None, 'dbname': None, 'nopid': True, 'nodb': False, 'nouser': False, 'noclient': False, 'nocpu': False, 'nomem': False, 'noread': False, 'nowrite': False, 'notime': False, 'nowait': False, 'noappname': True, 'header_show_instance': None, 'header_show_system': None, 'header_show_workers': None, 'hide_queries_in_logs': False, 'refresh': 2}
84+
{'profile': None, 'blocksize': 4096, 'rds': False, 'output': None, 'dbsize': False, 'tempfiles': True, 'walreceiver': True, 'wrap_query': True, 'durationmode': '1', 'minduration': 0, 'filters': [], 'debug_file': None, 'help': False, 'connection_string': '', 'host': None, 'port': '5433', 'username': None, 'dbname': None, 'pid': False, 'database': True, 'user': True, 'client': True, 'cpu': True, 'mem': True, 'read': True, 'write': True, 'time': True, 'wait': True, 'appname': False, 'header_show_instance': None, 'header_show_system': None, 'header_show_workers': None, 'hide_queries_in_logs': False, 'refresh': 2}

tests/test_config.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ def test_flag():
1919

2020
def test_flag_load():
2121
options = {
22-
"noappname": False,
23-
"noclient": False,
24-
"nocpu": False,
25-
"nodb": False,
26-
"nomem": False,
27-
"nopid": False,
28-
"noread": False,
29-
"notime": False,
30-
"nouser": False,
31-
"nowait": False,
32-
"nowrite": False,
22+
"appname": True,
23+
"client": True,
24+
"cpu": True,
25+
"database": True,
26+
"mem": True,
27+
"pid": True,
28+
"read": True,
29+
"time": True,
30+
"user": True,
31+
"wait": True,
32+
"write": True,
3333
}
3434
flag = Flag.load(None, is_local=True, **options)
3535
assert (
@@ -67,9 +67,9 @@ def test_flag_load():
6767
| Flag.APPNAME
6868
| Flag.DATABASE
6969
)
70-
options["nodb"] = True
71-
options["notime"] = True
72-
options["nopid"] = True
70+
options["database"] = False
71+
options["time"] = False
72+
options["pid"] = False
7373
cfg = Configuration(
7474
name="test",
7575
values=dict(database=UISection(hidden=False), relation=UISection(hidden=True)),

tests/test_ui.txt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ Default CLI options, passed to ui.main():
2626
... "refresh": 2,
2727
... "minduration": 0,
2828
... "filters": [],
29-
... "noappname": True,
30-
... "noclient": False,
31-
... "nocolor": False,
32-
... "nocpu": True,
33-
... "nodb": False,
34-
... "nodbsize": False,
35-
... "notempfiles": False,
36-
... "nowalreceiver": False,
37-
... "nomem": True,
38-
... "nopid": False,
39-
... "noread": True,
40-
... "notime": True,
41-
... "nouser": False,
42-
... "nowait": True,
43-
... "nowrite": True,
29+
... "appname": False,
30+
... "client": True,
31+
... "color": True,
32+
... "cpu": False,
33+
... "database": True,
34+
... "dbsize": True,
35+
... "tempfiles": True,
36+
... "walreceiver": True,
37+
... "mem": False,
38+
... "pid": True,
39+
... "read": False,
40+
... "time": False,
41+
... "user": True,
42+
... "wait": False,
43+
... "write": False,
4444
... "output": None,
4545
... "port": f"{postgres.info.port}",
4646
... "rds": False,
@@ -431,7 +431,7 @@ PID DATABASE USER CLIENT state Query
431431
<BLANKLINE>
432432
------------------------------------------------------------------------------------------- sending key 'q' --------------------------------------------------------------------------------------------
433433

434-
>>> defaults["nopid"] = True
434+
>>> defaults["pid"] = False
435435
>>> defaults["header_show_instance"] = False
436436
>>> defaults["header_show_workers"] = False
437437
>>> defaults["header_show_system"] = False
@@ -823,10 +823,10 @@ Use another set of options:
823823
... defaults,
824824
... durationmode="2",
825825
... wrap_query=True,
826-
... noclient=True,
827-
... nodbsize=True,
828-
... notempfiles=False,
829-
... nowalreceiver=False,
826+
... client=False,
827+
... dbsize=False,
828+
... tempfiles=True,
829+
... walreceiver=True,
830830
... output=str(output),
831831
... ),
832832
... )
@@ -940,9 +940,9 @@ Interactive mode:
940940
... defaults,
941941
... durationmode="1",
942942
... wrap_query=True,
943-
... nopid=False,
944-
... noclient=True,
945-
... nouser=True,
943+
... pid=True,
944+
... client=False,
945+
... user=False,
946946
... output=None,
947947
... ),
948948
... )

0 commit comments

Comments
 (0)