From aa56ac5ce10b783d2c9f009bb05ca0b9ad3dee62 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Wed, 12 Feb 2025 09:40:43 +0100 Subject: [PATCH] Replace optparse with argparse, make '--machine' and '--group' handle multiple arguments. --- src/allclients.py | 104 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/src/allclients.py b/src/allclients.py index 0e86f77..10b1784 100644 --- a/src/allclients.py +++ b/src/allclients.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -import optparse +import argparse import os import re import allclients.ssh @@ -10,62 +10,64 @@ import netgroup import socket import subprocess -class VerboseOptionParser(optparse.OptionParser): - def error(self, msg): - self.print_help() - optparse.OptionParser.error(self, msg) - pass - pass - def local_interfaces(): ip = subprocess.check_output([ 'ip', 'addr', 'show' ]).decode('utf8') return (re.findall('inet\\s+([0-9.]+)/[0-9]+', ip, re.MULTILINE) + re.findall('inet6\\s+([0-9a-f:]+)/[0-9]+', ip, re.MULTILINE)) if __name__ == '__main__': - optParser = VerboseOptionParser(usage="%prog [options] command*") - optParser.add_option("-4", "--ipv4", - action="store_true", - help="force connections to be over ipv4") - optParser.add_option("-6", "--ipv6", - action="store_true", - help="force connections to be over ipv6") - optParser.add_option("-a", "--active", - type=int, - action="store", default=10, - metavar="N", - help="run at most N commands in paralell [10]") - optParser.add_option("-g", "--group", - action="append", default=[], - metavar="GROUP", - help="apply actions to GROUP") - optParser.add_option("-m", "--machine", - action="append", default=[], - metavar="MACHINE", - help="apply actions to MACHINE") - optParser.add_option("-o", "--ordered", - action="store_true", default=False, - help="order result by hostname") - optParser.add_option("-n", "--dry-run", - action="store_true", default=False, - help="dont't actually run commands") - optParser.add_option("-q", "--quiet", - action="store_true", default=False, - help="dont't show progress or color") - optParser.add_option("-s", "--self", - action="store_true", default=False, - help="run command on invoking host also") - optParser.add_option("-t", "--timeout", - type=int, - action="store", default=5, - metavar="SECONDS", - help="wait at most SECONDS for a connection") - optParser.add_option("-u", "--user", - action="store", default=None, - metavar="USER", - help="run actions as USER") - optParser.disable_interspersed_args() - (options, args) = optParser.parse_args(sys.argv[1:]) + parser = argparse.ArgumentParser(usage="%(prog)s [options] [--] command*") + parser.add_argument("-4", "--ipv4", + action="store_true", + help="force connections to be over ipv4") + parser.add_argument("-6", "--ipv6", + action="store_true", + help="force connections to be over ipv6") + parser.add_argument("-a", "--active", + type=int, + action="store", default=16, + metavar="N", + help="run at most N commands in paralell [10]") + parser.add_argument("-g", "--group", + nargs='+', + action="extend", + default=[], + metavar="NETGROUP", + help="apply actions to members of NETGROUP") + parser.add_argument("-m", "--machine", + nargs='+', + default=[], + action="extend", + metavar="HOST", + help="apply actions to HOST") + parser.add_argument("-o", "--ordered", + action="store_true", default=False, + help="order result by hostname") + parser.add_argument("-n", "--dry-run", + action="store_true", default=False, + help="dont't actually run commands") + parser.add_argument("-q", "--quiet", + action="store_true", default=False, + help="dont't show progress or color") + parser.add_argument("-s", "--self", + action="store_true", default=False, + help="run command on invoking host also") + parser.add_argument("-t", "--timeout", + type=int, + action="store", default=5, + metavar="SECONDS", + help="wait at most SECONDS for a connection") + parser.add_argument("-u", "--user", + action="store", default=None, + metavar="USER", + help="run actions as USER") + (options,args) = parser.parse_known_intermixed_args(sys.argv[1:]) + if len(args) and args[0] == '--': + args = args[1:] + pass + if len(args) == 0 and not options.dry_run: + raise Exception("Commmand not given, if last argument is '--machine/--group', " + "add '--' before command") if options.ipv4 and options.ipv6: raise Exception('Only one --ipv4 or --ipv6 may be specified') -- GitLab