From 0c228f6488cbfd57090fe460489386b88344c7e9 Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Fri, 19 Apr 2024 12:14:13 +0200 Subject: [PATCH] Add non-negative counterparts of --no-XXX options --- CHANGELOG.md | 3 +++ pgactivity/cli.py | 48 ++++++++++++++++++++++++++-------------------- tests/test_cli.txt | 35 +++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 441b2692..1ab6d593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pgactivity/cli.py b/pgactivity/cli.py index af5d111c..3316d5e0 100755 --- a/pgactivity/cli.py +++ b/pgactivity/cli.py @@ -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 @@ -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." @@ -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", @@ -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( diff --git a/tests/test_cli.txt b/tests/test_cli.txt index eb0b7ead..0e84333b 100644 --- a/tests/test_cli.txt +++ b/tests/test_cli.txt @@ -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), @@ -50,17 +54,20 @@ Connection Options: 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 (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). Header display options: --no-inst-info Hide instance information.