From: Shivani Bhardwaj Date: Wed, 20 Feb 2019 18:10:14 +0000 (+0530) Subject: suricatactl: Clean up parser, improve help X-Git-Tag: suricata-5.0.0-beta1~175 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=342f3d5eecf6636e4ca7fed469b21b3eb839ee91;p=thirdparty%2Fsuricata.git suricatactl: Clean up parser, improve help So far the suricatactl parser was unclear about the options to use and did not well display the required and optional param difference. Fix that to make it legible for any user. Before ``` └─ $ ▶ ./bin/suricatactl filestore -h usage: suricatactl filestore [-h] {prune} ... positional arguments: {prune} optional arguments: -h, --help show this help message and exit └─ $ ▶ ./bin/suricatactl filestore prune -h usage: suricatactl filestore prune [-h] [-d DIRECTORY] [--age AGE] [-n] [-v] [-q] optional arguments: -h, --help show this help message and exit -d DIRECTORY, --directory DIRECTORY filestore directory --age AGE prune files older than age -n, --dry-run only print what would happen -v, --verbose increase verbosity -q, --quiet be quiet, log warnings and errors only ``` After ``` └─ $ ▶ ./bin/suricatactl filestore -h usage: suricatactl filestore [-h] {prune} ... positional arguments: {prune} sub-command help prune Remove files in specified directory older than specified age optional arguments: -h, --help show this help message and exit └─ $ ▶ ./bin/suricatactl filestore prune -h usage: suricatactl filestore prune [-h] -d DIRECTORY [--age AGE] [-n] [-v] [-q] optional arguments: -h, --help show this help message and exit -n, --dry-run only print what would happen -v, --verbose increase verbosity -q, --quiet be quiet, log warnings and errors only required arguments: -d DIRECTORY, --directory DIRECTORY filestore directory --age AGE prune files older than age, units: s, m, h, d ``` --- diff --git a/python/suricata/ctl/filestore.py b/python/suricata/ctl/filestore.py index 76ca5cb8b3..076e4ef310 100644 --- a/python/suricata/ctl/filestore.py +++ b/python/suricata/ctl/filestore.py @@ -31,10 +31,14 @@ class InvalidAgeFormatError(Exception): def register_args(parser): - parsers = parser.add_subparsers() - prune_parser = parsers.add_parser("prune") - prune_parser.add_argument("-d", "--directory", help="filestore directory") - prune_parser.add_argument("--age", help="prune files older than age") + subparser = parser.add_subparsers(help="sub-command help") + prune_parser = subparser.add_parser("prune", + help="Remove files in specified directory older than specified age") + required_args = prune_parser.add_argument_group("required arguments") + required_args.add_argument("-d", "--directory", + help="filestore directory", required=True) + required_args.add_argument("--age", + help="prune files older than age, units: s, m, h, d") prune_parser.add_argument( "-n", "--dry-run", action="store_true", default=False, help="only print what would happen") @@ -57,15 +61,13 @@ def parse_age(age): raise InvalidAgeFormatError(age) val = int(matched_age.group(1)) unit = matched_age.group(2) - if unit == "s": - return val - if unit == "m": - return val * 60 - if unit == "h": - return val * 60 * 60 - if unit == "d": - return val * 60 * 60 * 24 - raise InvalidAgeFormatError("bad unit: %s" % (unit)) + ts_units = ["s", "m", "h", "d"] + try: + idx = ts_units.index(unit) + except ValueError: + raise InvalidAgeFormatError("bad unit: %s" % (unit)) + multiplier = 60 ** idx if idx != 3 else 24 * 60 ** 2 + return val * multiplier def get_filesize(path): @@ -81,7 +83,6 @@ def remove_file(path, dry_run): def prune(args): - if args.verbose: logger.setLevel(logging.DEBUG) if args.quiet: @@ -106,7 +107,6 @@ def prune(args): # Do not go into the tmp directory. if "tmp" in dirnames: dirnames.remove("tmp") - for filename in filenames: path = os.path.join(dirpath, filename) mtime = os.path.getmtime(path) @@ -115,6 +115,5 @@ def prune(args): logger.debug("Deleting %s; age=%ds", path, this_age) size += remove_file(path, args.dry_run) count += 1 - logger.info("Removed %d files; %d bytes.", count, size) return 0 diff --git a/python/suricata/ctl/main.py b/python/suricata/ctl/main.py index a8125f96c1..d51a5be61e 100644 --- a/python/suricata/ctl/main.py +++ b/python/suricata/ctl/main.py @@ -34,11 +34,10 @@ def init_logger(): def main(): init_logger() - parser = argparse.ArgumentParser(description="Suricata Control Tool") - subparsers = parser.add_subparsers( - title="subcommands", - description="Commands") - filestore.register_args(subparsers.add_parser("filestore")) + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers(help='sub-command help') + fs_parser = subparsers.add_parser("filestore", help="Filestore related commands") + filestore.register_args(parser=fs_parser) args = parser.parse_args() try: func = args.func