-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzeek-script
executable file
·75 lines (56 loc) · 1.96 KB
/
zeek-script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
"""
This is a tool for processing Zeek scripts. It currently supports formatting
scripts according to codified rules (no optionas at all atm), and showing a
parse tree for the script.
"""
# https://pypi.org/project/argcomplete/#global-completion
# PYTHON_ARGCOMPLETE_OK
import argparse
import sys
try:
# Argcomplete provides command-line completion for users of argparse.
# We support it if available, but don't complain when it isn't.
import argcomplete # pylint: disable=import-error
except ImportError:
pass
import zeekscript
# ---- Helper functions --------------------------------------------------------
def create_parser():
parser = argparse.ArgumentParser(description="A Zeek script analyzer")
zeekscript.add_version_arg(parser)
command_parser = parser.add_subparsers(
title="commands",
dest="command",
help="See `%(prog)s <command> -h` for per-command usage info.",
)
sub_parser = command_parser.add_parser("format", help="Format/indent Zeek scripts")
zeekscript.add_format_cmd(sub_parser)
sub_parser = command_parser.add_parser(
"parse",
help="Show Zeek script parse tree with parser metadata.",
epilog="Exits with 0 on success, 1 on a hard parse error that "
"does not yield a parse tree, and 2 when parsing succeeds but "
"the parse tree contains erroneous nodes.",
)
zeekscript.add_parse_cmd(sub_parser)
if "argcomplete" in sys.modules:
argcomplete.autocomplete(parser)
return parser
def main():
parser = create_parser()
args = parser.parse_args()
if args.version:
print(zeekscript.__version__)
return 0
if not args.command:
zeekscript.print_error(
"error: please provide a command to execute. See --help."
)
return 1
try:
return args.run_cmd(args)
except KeyboardInterrupt:
return 0
if __name__ == "__main__":
sys.exit(main())