Skip to content

Commit

Permalink
Add non-negative counterparts of --no-XXX options
Browse files Browse the repository at this point in the history
We need to set the default value of "flag" options (those built using
the flag() helper function) to None in order to discriminate unspecified
option (value=None) from unset option (value=False).
  • Loading branch information
dlax committed May 28, 2024
1 parent 38147a8 commit e5dd8ed
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 162 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

* The color of cells in the process table can now be customized through the
configuration file.
* Add non-negative counterparts of many `--no-...` command-line option, thus
allowing to enable respective feature/behaviour even if disabled in the
configuration. (Requires Python 3.9 or higher.)

### Fixed

Expand Down
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,20 @@ ex:
Process table display options:
These options may be used hide some columns from the processes table.

--no-pid Disable PID.
--no-database Disable DATABASE.
--no-user Disable USER.
--no-client Disable CLIENT.
--no-cpu Disable CPU%.
--no-mem Disable MEM%.
--no-read Disable READ/s.
--no-write Disable WRITE/s.
--no-time Disable TIME+.
--no-wait Disable W.
--no-app-name Disable APP.
--pid, --no-pid Enable/disable PID.
--database, --no-database
Enable/disable DATABASE.
--user, --no-user Enable/disable USER.
--client, --no-client
Enable/disable CLIENT.
--cpu, --no-cpu Enable/disable CPU%.
--mem, --no-mem Enable/disable MEM%.
--read, --no-read Enable/disable READ/s.
--write, --no-write Enable/disable WRITE/s.
--time, --no-time Enable/disable TIME+.
--wait, --no-wait Enable/disable W.
--app-name, --no-app-name
Enable/disable APP.

Header display options:
--no-inst-info Display instance information.
Expand Down
89 changes: 42 additions & 47 deletions docs/man/pg_activity.1
Original file line number Diff line number Diff line change
Expand Up @@ -371,25 +371,20 @@ required by another session. It shows following information:
.Vb 1
\& Store running queries as CSV.
.Ve
.IP "\fB\-\-no\-db\-size\fR" 2
.IX Item "--no-db-size"
.IP "\fB\-\-db\-size\fR, \fB\-\-no\-db\-size\fR" 2
.IX Item "--db-size, --no-db-size"
.Vb 1
\& Skip total size of DB.
\& Enable/disable total size of DB.
.Ve
.IP "\fB\-\-no\-tempfiles\fR" 2
.IX Item "--no-tempfiles"
.IP "\fB\-\-tempfiles\fR, \fB\-\-no\-tempfiles\fR" 2
.IX Item "--tempfiles, --no-tempfiles"
.Vb 1
\& Skip tempfile count and size.
\& Enable/disable tempfile count and size.
.Ve
.IP "\fB\-\-no\-walreceiver\fR" 2
.IX Item "--no-walreceiver"
.IP "\fB\-\-walreceiver\fR, \fB\-\-no\-walreceiver\fR" 2
.IX Item "--walreceiver, --no-walreceiver"
.Vb 1
\& Skip walreceiver checks.
.Ve
.IP "\fB\-\-no\-walreceiver\fR" 2
.IX Item "--no-walreceiver"
.Vb 1
\& Skip walreceiver checks.
\& Enable/disable walreceiver checks.
.Ve
.IP "\fB\-w, \-\-wrap\-query\fR" 2
.IX Item "-w, --wrap-query"
Expand Down Expand Up @@ -442,60 +437,60 @@ required by another session. It shows following information:
.Ve
.SS "\s-1PROCESS DISPLAY OPTIONS\s0"
.IX Subsection "PROCESS DISPLAY OPTIONS"
.IP "\fB\-\-no\-pid\fR" 2
.IX Item "--no-pid"
.IP "\fB\-\-pid\fR, \fB\-\-no\-pid\fR" 2
.IX Item "--pid, --no-pid"
.Vb 1
\& Disable PID.
\& Enable/disable PID.
.Ve
.IP "\fB\-\-no\-database\fR" 2
.IX Item "--no-database"
.IP "\fB\-\-database\fR, \fB\-\-no\-database\fR" 2
.IX Item "--database, --no-database"
.Vb 1
\& Disable DATABASE.
\& Enable/disable DATABASE.
.Ve
.IP "\fB\-\-no\-user\fR" 2
.IX Item "--no-user"
.IP "\fB\-\-user\fR, \fB\-\-no\-user\fR" 2
.IX Item "--user, --no-user"
.Vb 1
\& Disable USER.
\& Enable/disable USER.
.Ve
.IP "\fB\-\-no\-client\fR" 2
.IX Item "--no-client"
.IP "\fB\-\-client\fR, \fB\-\-no\-client\fR" 2
.IX Item "--client, --no-client"
.Vb 1
\& Disable CLIENT.
\& Enable/disable CLIENT.
.Ve
.IP "\fB\-\-no\-cpu\fR" 2
.IX Item "--no-cpu"
.IP "\fB\-\-cpu\fR, \fB\-\-no\-cpu\fR" 2
.IX Item "--cpu, --no-cpu"
.Vb 1
\& Disable CPU%.
\& Enable/disable CPU%.
.Ve
.IP "\fB\-\-no\-mem\fR" 2
.IX Item "--no-mem"
.IP "\fB\-\-mem\fR, \fB\-\-no\-mem\fR" 2
.IX Item "--mem, --no-mem"
.Vb 1
\& Disable MEM%.
\& Enable/disable MEM%.
.Ve
.IP "\fB\-\-no\-read\fR" 2
.IX Item "--no-read"
.IP "\fB\-\-read\fR, \fB\-\-no\-read\fR" 2
.IX Item "--read, --no-read"
.Vb 1
\& Disable READ/s.
\& Enable/disable READ/s.
.Ve
.IP "\fB\-\-no\-write\fR" 2
.IX Item "--no-write"
.IP "\fB\-\-write\fR, \fB\-\-no\-write\fR" 2
.IX Item "--write, --no-write"
.Vb 1
\& Disable WRITE/s.
\& Enable/disable WRITE/s.
.Ve
.IP "\fB\-\-no\-time\fR" 2
.IX Item "--no-time"
.IP "\fB\-\-time\fR, \fB\-\-no\-time\fR" 2
.IX Item "--time, --no-time"
.Vb 1
\& Disable TIME+.
\& Enable/disable TIME+.
.Ve
.IP "\fB\-\-no\-wait\fR" 2
.IX Item "--no-wait"
.IP "\fB\-\-wait\fR, \fB\-\-no\-wait\fR" 2
.IX Item "--wait, --no-wait"
.Vb 1
\& Disable W.
\& Enable/disable W.
.Ve
.IP "\fB\-\-no\-app\-name\fR" 2
.IX Item "--no-app-name"
.IP "\fB\-\-app\-name\fR, \fB\-\-no\-app\-name\fR" 2
.IX Item "--app-name, --no-app-name"
.Vb 1
\& Disable APP.
\& Enable/disable APP.
.Ve
.SS "\s-1HEADER DISPLAY OPTIONS\s0"
.IX Subsection "HEADER DISPLAY OPTIONS"
Expand Down
60 changes: 28 additions & 32 deletions docs/man/pg_activity.pod
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,17 @@ required by another session. It shows following information:

Store running queries as CSV.

=item B<--no-db-size>
=item B<--db-size>, B<--no-db-size>

Skip total size of DB.
Enable/disable total size of DB.

=item B<--no-tempfiles>
=item B<--tempfiles>, B<--no-tempfiles>

Skip tempfile count and size.
Enable/disable tempfile count and size.

=item B<--no-walreceiver>
=item B<--walreceiver>, B<--no-walreceiver>

Skip walreceiver checks.

=item B<--no-walreceiver>

Skip walreceiver checks.
Enable/disable walreceiver checks.

=item B<-w, --wrap-query>

Expand Down Expand Up @@ -322,49 +318,49 @@ required by another session. It shows following information:

=over 2

=item B<--no-pid>
=item B<--pid>, B<--no-pid>

Disable PID.
Enable/disable PID.

=item B<--no-database>
=item B<--database>, B<--no-database>

Disable DATABASE.
Enable/disable DATABASE.

=item B<--no-user>
=item B<--user>, B<--no-user>

Disable USER.
Enable/disable USER.

=item B<--no-client>
=item B<--client>, B<--no-client>

Disable CLIENT.
Enable/disable CLIENT.

=item B<--no-cpu>
=item B<--cpu>, B<--no-cpu>

Disable CPU%.
Enable/disable CPU%.

=item B<--no-mem>
=item B<--mem>, B<--no-mem>

Disable MEM%.
Enable/disable MEM%.

=item B<--no-read>
=item B<--read>, B<--no-read>

Disable READ/s.
Enable/disable READ/s.

=item B<--no-write>
=item B<--write>, B<--no-write>

Disable WRITE/s.
Enable/disable WRITE/s.

=item B<--no-time>
=item B<--time>, B<--no-time>

Disable TIME+.
Enable/disable TIME+.

=item B<--no-wait>
=item B<--wait>, B<--no-wait>

Disable W.
Enable/disable W.

=item B<--no-app-name>
=item B<--app-name>, B<--no-app-name>

Disable APP.
Enable/disable APP.

=back

Expand Down
48 changes: 27 additions & 21 deletions pgactivity/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import argparse
import logging
import os
import socket
import sys
import time
from argparse import ArgumentParser
from io import StringIO
from typing import Any

Expand Down Expand Up @@ -45,12 +45,20 @@ def configure_logger(debug_file: str | None = None) -> StringIO:
return memory_string


def flag(p: Any, spec: str, *, dest: str, help: str) -> None:
p.add_argument(spec, dest=dest, help=help, action="store_false", default=True)
def flag(p: Any, spec: str, *, dest: str, feature: str) -> None:
assert not spec.startswith("--no-") and spec.startswith("--"), spec
if sys.version_info < (3, 9):
spec = f"--no-{spec[2:]}"
action = "store_false"
help = f"Disable {feature}."
else:
action = argparse.BooleanOptionalAction
help = f"Enable/disable {feature}."
p.add_argument(spec, dest=dest, help=help, action=action, default=None)


def get_parser() -> ArgumentParser:
parser = ArgumentParser(
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [options] [connection string]",
description=(
"htop like application for PostgreSQL server activity monitoring."
Expand Down Expand Up @@ -103,11 +111,9 @@ def get_parser() -> ArgumentParser:
metavar="FILEPATH",
default=None,
)
flag(group, "--no-db-size", dest="dbsize", help="Skip total size of DB.")
flag(
group, "--no-tempfiles", dest="tempfiles", help="Skip tempfile count and size."
)
flag(group, "--no-walreceiver", dest="walreceiver", help="Skip walreceiver checks.")
flag(group, "--db-size", dest="dbsize", feature="total size of DB")
flag(group, "--tempfiles", dest="tempfiles", feature="tempfile count and size")
flag(group, "--walreceiver", dest="walreceiver", feature="walreceiver checks")
group.add_argument(
"-w",
"--wrap-query",
Expand Down Expand Up @@ -215,17 +221,17 @@ def get_parser() -> ArgumentParser:
"Process table display options",
"These options may be used hide some columns from the processes table.",
)
flag(group, "--no-pid", dest="pid", help="Disable PID.")
flag(group, "--no-database", dest="database", help="Disable DATABASE.")
flag(group, "--no-user", dest="user", help="Disable USER.")
flag(group, "--no-client", dest="client", help="Disable CLIENT.")
flag(group, "--no-cpu", dest="cpu", help="Disable CPU%%.")
flag(group, "--no-mem", dest="mem", help="Disable MEM%%.")
flag(group, "--no-read", dest="read", help="Disable READ/s.")
flag(group, "--no-write", dest="write", help="Disable WRITE/s.")
flag(group, "--no-time", dest="time", help="Disable TIME+.")
flag(group, "--no-wait", dest="wait", help="Disable W.")
flag(group, "--no-app-name", dest="appname", help="Disable APP.")
flag(group, "--pid", dest="pid", feature="PID")
flag(group, "--database", dest="database", feature="DATABASE")
flag(group, "--user", dest="user", feature="USER")
flag(group, "--client", dest="client", feature="CLIENT")
flag(group, "--cpu", dest="cpu", feature="CPU%%")
flag(group, "--mem", dest="mem", feature="MEM%%")
flag(group, "--read", dest="read", feature="READ/s")
flag(group, "--write", dest="write", feature="WRITE/s")
flag(group, "--time", dest="time", feature="TIME+")
flag(group, "--wait", dest="wait", feature="W")
flag(group, "--app-name", dest="appname", feature="APP")

group = parser.add_argument_group("Header display options")
group.add_argument(
Expand Down
Loading

0 comments on commit e5dd8ed

Please sign in to comment.