From f6d0572984f78f400166fc585dd3b3a120101647 Mon Sep 17 00:00:00 2001 From: euxane Date: Sat, 30 Nov 2024 11:44:43 +0100 Subject: cli: turn action and target into positional arguments --- changelog.md | 1 + main.nim | 40 ++++++++++++++++++++++++++++++---------- readme.md | 18 +++++++++--------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/changelog.md b/changelog.md index b8af052..02a405f 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ Changelog ========= ## Next release +* The monitor and target are now positional CLI arguments instead of options * Fixed issue emitting pings when the system's IP address changed during exec * Added `--timestamp=datetime|unix|none` param to set line header format diff --git a/main.nim b/main.nim index 115808d..07686ed 100644 --- a/main.nim +++ b/main.nim @@ -9,6 +9,7 @@ import std/net import std/posix import std/paths import std/strutils +import std/sequtils import std/parseopt import file @@ -33,17 +34,24 @@ proc registerTerminationHandlers() = for signal in [SIGTERM, SIGHUP, SIGQUIT, SIGINT]: discard sigaction(signal, action) +func getArg( + args: seq[tuple[kind: CmdLineKind, key, val: string]], + index: int, + label: string +): string = + try: args[index].key + except: raise newException(ValueError, "Missing " & label & " argument") + proc main() = var scale: Scale = log2 symbols = UNICODE_SYMBOLS timestampHeader = formatTimestampDateTime (min, max) = (0, 1000) - probe: (timeout: Duration) -> int for optKind, key, val in getopt(): if optKind notin {cmdLongOption, cmdShortOption}: - raise newException(ValueError, "Invalid argument: " & key) + continue case key: @@ -81,21 +89,33 @@ proc main() = if parts.len != 2: raise newException(ValueError, "Invalid range") (min, max) = (parseInt(parts[0]), parseInt(parts[1])) + else: + raise newException(ValueError, "Unrecognised option") + + let + args = getopt().toSeq.filterIt(it.kind == cmdArgument) + monitor = args.getArg(0, "monitor") + target = args.getArg(1, "target") + + if args.len > 2: + raise newException(ValueError, "Invalid number of arguments") + + let probe = case monitor: of "ping": - let targetIp = resolve(val) + let targetIp = resolve(target) let mon = initPingMonitor(targetIp, procIdent()) - probe = (timeout: Duration) => mon.ping(timeout).inMilliseconds.int + (timeout: Duration) => mon.ping(timeout).inMilliseconds.int of "value": - let mon = initFileValueMonitor(Path val) - probe = (timeout: Duration) => mon.readValue() + let mon = initFileValueMonitor(Path target) + (timeout: Duration) => mon.readValue() of "change": - let mon = initFileChangeMonitor(Path val) - probe = (timeout: Duration) => mon.readValue() + let mon = initFileChangeMonitor(Path target) + (timeout: Duration) => mon.readValue() - if probe == nil: - raise newException(ValueError, "Missing monitor argument") + else: + raise newException(ValueError, "Unrecognised monitor argument") loop( probe, diff --git a/readme.md b/readme.md index a105b43..52c8a9e 100644 --- a/readme.md +++ b/readme.md @@ -22,13 +22,13 @@ CLI usage ```helptext tickwatch: A monitoring and visualization tool -Usage: tickwatch [OPTIONS] +Usage: tickwatch [OPTIONS] MONITOR TARGET -Monitor options: - --ping=HOST Monitor ping latency to specified IP or hostname - Prefix hostname with 4/ or 6/ to force IP version - --value=PATH Monitor a sysfs file's value - --change=PATH Monitor changes in a sysfs file's value +Monitors: + ping Monitor ping latency to specified IP or hostname + Prefix target hostname with 4/ or 6/ to force IP version + value Monitor a sysfs file's value + change Monitor changes in a sysfs file's value Display options: --range=MIN:MAX Set range of values (default: 0:1000) @@ -44,13 +44,13 @@ Other options: --version, -v Display the version Examples: - tickwatch --ping=6/euxane.net + tickwatch ping 6/euxane.net tickwatch --range=0:100000 \ - --change=/sys/class/net/eth0/statistics/rx_packets + change /sys/class/net/eth0/statistics/rx_packets tickwatch --scale=linear --range=0:50000 \ - --value=/sys/class/thermal/thermal_zone0/temp + value /sys/class/thermal/thermal_zone0/temp ``` -- cgit v1.2.3