Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'ynl-cli-list-attrs-argument'

Gal Pressman says:

====================
YNL CLI --list-attrs argument

While experimenting with the YNL CLI, I found the process of going back
and forth to examine the YAML spec files in order to figure out how to
use each command quite tiring.

The addition of --list-attrs helps by providing all information needed
directly in the tool. I figured others would likely find it useful as
well.

v1: https://lore.kernel.org/all/20251116192845.1693119-1-gal@nvidia.com/
====================

Link: https://patch.msgid.link/20251118143208.2380814-1-gal@nvidia.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

+79
+79
tools/net/ynl/pyynl/cli.py
··· 7 7 import pathlib 8 8 import pprint 9 9 import sys 10 + import textwrap 10 11 11 12 sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix()) 12 13 from lib import YnlFamily, Netlink, NlError ··· 38 37 if isinstance(obj, set): 39 38 return list(obj) 40 39 return json.JSONEncoder.default(self, obj) 40 + 41 + 42 + def print_attr_list(ynl, attr_names, attr_set, indent=2): 43 + """Print a list of attributes with their types and documentation.""" 44 + prefix = ' ' * indent 45 + for attr_name in attr_names: 46 + if attr_name in attr_set.attrs: 47 + attr = attr_set.attrs[attr_name] 48 + attr_info = f'{prefix}- {attr_name}: {attr.type}' 49 + if 'enum' in attr.yaml: 50 + enum_name = attr.yaml['enum'] 51 + attr_info += f" (enum: {enum_name})" 52 + # Print enum values if available 53 + if enum_name in ynl.consts: 54 + const = ynl.consts[enum_name] 55 + enum_values = list(const.entries.keys()) 56 + attr_info += f"\n{prefix} {const.type.capitalize()}: {', '.join(enum_values)}" 57 + 58 + # Show nested attributes reference and recursively display them 59 + nested_set_name = None 60 + if attr.type == 'nest' and 'nested-attributes' in attr.yaml: 61 + nested_set_name = attr.yaml['nested-attributes'] 62 + attr_info += f" -> {nested_set_name}" 63 + 64 + if attr.yaml.get('doc'): 65 + doc_text = textwrap.indent(attr.yaml['doc'], prefix + ' ') 66 + attr_info += f"\n{doc_text}" 67 + print(attr_info) 68 + 69 + # Recursively show nested attributes 70 + if nested_set_name in ynl.attr_sets: 71 + nested_set = ynl.attr_sets[nested_set_name] 72 + # Filter out 'unspec' and other unused attrs 73 + nested_names = [n for n in nested_set.attrs.keys() 74 + if nested_set.attrs[n].type != 'unused'] 75 + if nested_names: 76 + print_attr_list(ynl, nested_names, nested_set, indent + 4) 77 + 78 + 79 + def print_mode_attrs(ynl, mode, mode_spec, attr_set, print_request=True): 80 + """Print a given mode (do/dump/event/notify).""" 81 + mode_title = mode.capitalize() 82 + 83 + if print_request and 'request' in mode_spec and 'attributes' in mode_spec['request']: 84 + print(f'\n{mode_title} request attributes:') 85 + print_attr_list(ynl, mode_spec['request']['attributes'], attr_set) 86 + 87 + if 'reply' in mode_spec and 'attributes' in mode_spec['reply']: 88 + print(f'\n{mode_title} reply attributes:') 89 + print_attr_list(ynl, mode_spec['reply']['attributes'], attr_set) 90 + 91 + if 'attributes' in mode_spec: 92 + print(f'\n{mode_title} attributes:') 93 + print_attr_list(ynl, mode_spec['attributes'], attr_set) 41 94 42 95 43 96 def main(): ··· 125 70 group.add_argument('--dump', dest='dump', metavar='DUMP-OPERATION', type=str) 126 71 group.add_argument('--list-ops', action='store_true') 127 72 group.add_argument('--list-msgs', action='store_true') 73 + group.add_argument('--list-attrs', dest='list_attrs', metavar='OPERATION', type=str, 74 + help='List attributes for an operation') 128 75 129 76 parser.add_argument('--duration', dest='duration', type=int, 130 77 help='when subscribed, watch for DURATION seconds') ··· 191 134 if args.list_msgs: 192 135 for op_name, op in ynl.msgs.items(): 193 136 print(op_name, " [", ", ".join(op.modes), "]") 137 + 138 + if args.list_attrs: 139 + op = ynl.msgs.get(args.list_attrs) 140 + if not op: 141 + print(f'Operation {args.list_attrs} not found') 142 + exit(1) 143 + 144 + print(f'Operation: {op.name}') 145 + print(op.yaml['doc']) 146 + 147 + for mode in ['do', 'dump', 'event']: 148 + if mode in op.yaml: 149 + print_mode_attrs(ynl, mode, op.yaml[mode], op.attr_set, True) 150 + 151 + if 'notify' in op.yaml: 152 + mode_spec = op.yaml['notify'] 153 + ref_spec = ynl.msgs.get(mode_spec).yaml.get('do') 154 + if ref_spec: 155 + print_mode_attrs(ynl, 'notify', ref_spec, op.attr_set, False) 156 + 157 + if 'mcgrp' in op.yaml: 158 + print(f"\nMulticast group: {op.yaml['mcgrp']}") 194 159 195 160 try: 196 161 if args.do: