xref: /linux/tools/net/ynl/cli.py (revision 1b0975ee3bdd3eb19a47371c26fd7ef8f7f6b599)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3
4import argparse
5import json
6import pprint
7import time
8
9from lib import YnlFamily
10
11
12def main():
13    parser = argparse.ArgumentParser(description='YNL CLI sample')
14    parser.add_argument('--spec', dest='spec', type=str, required=True)
15    parser.add_argument('--schema', dest='schema', type=str)
16    parser.add_argument('--no-schema', action='store_true')
17    parser.add_argument('--json', dest='json_text', type=str)
18    parser.add_argument('--do', dest='do', type=str)
19    parser.add_argument('--dump', dest='dump', type=str)
20    parser.add_argument('--sleep', dest='sleep', type=int)
21    parser.add_argument('--subscribe', dest='ntf', type=str)
22    args = parser.parse_args()
23
24    if args.no_schema:
25        args.schema = ''
26
27    attrs = {}
28    if args.json_text:
29        attrs = json.loads(args.json_text)
30
31    ynl = YnlFamily(args.spec, args.schema)
32
33    if args.ntf:
34        ynl.ntf_subscribe(args.ntf)
35
36    if args.sleep:
37        time.sleep(args.sleep)
38
39    if args.do:
40        reply = ynl.do(args.do, attrs)
41        pprint.PrettyPrinter().pprint(reply)
42    if args.dump:
43        reply = ynl.dump(args.dump, attrs)
44        pprint.PrettyPrinter().pprint(reply)
45
46    if args.ntf:
47        ynl.check_ntf()
48        pprint.PrettyPrinter().pprint(ynl.async_msg_queue)
49
50
51if __name__ == "__main__":
52    main()
53