Skip to content

Commit

Permalink
Add non-negative counterparts of --no-XXX options
Browse files Browse the repository at this point in the history
  • Loading branch information
dlax committed May 24, 2024
1 parent 7cd125e commit 0c228f6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 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. (This only works with Python 3.9 or higher.)

### Fixed

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} (default=%(default)s)."
p.add_argument(spec, dest=dest, help=help, action=action, default=True)


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
35 changes: 21 additions & 14 deletions tests/test_cli.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ Options:
filters out the rdsadmin database from space
calculation).
--output FILEPATH Store running queries as CSV.
--no-db-size Skip total size of DB.
--no-tempfiles Skip tempfile count and size.
--no-walreceiver Skip walreceiver checks.
--db-size, --no-db-size
Enable/disable total size of DB. (default=True).
--tempfiles, --no-tempfiles
Enable/disable tempfile count and size.
(default=True).
--walreceiver, --no-walreceiver
Enable/disable walreceiver checks. (default=True).
-w, --wrap-query Wrap query column instead of truncating.
--duration-mode DURATION_MODE
Duration mode. Values: 1-QUERY(default),
Expand Down Expand Up @@ -50,17 +54,20 @@ Connection Options:
Process table display options:
These options may be used hide some columns from the processes table.
<BLANKLINE>
--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 (default=True).
--database, --no-database
Enable/disable DATABASE (default=True).
--user, --no-user Enable/disable USER (default=True).
--client, --no-client
Enable/disable CLIENT (default=True).
--cpu, --no-cpu Enable/disable CPU% (default=True).
--mem, --no-mem Enable/disable MEM% (default=True).
--read, --no-read Enable/disable READ/s (default=True).
--write, --no-write Enable/disable WRITE/s (default=True).
--time, --no-time Enable/disable TIME+ (default=True).
--wait, --no-wait Enable/disable W (default=True).
--app-name, --no-app-name
Enable/disable APP (default=True).
<BLANKLINE>
Header display options:
--no-inst-info Hide instance information.
Expand Down

0 comments on commit 0c228f6

Please sign in to comment.