Replace func/struct/enum with unified show command
- Add 'show' command to display code for functions/structs/enums by name - Remove separate func/struct/enum commands - Update usage message and docstring - Command format: c_util show name1 name2 ...
This commit is contained in:
1 parent
278851d89a
commit
bbff7aa8f0
1 file changed
+57
-58
@@ -4,9 +4,7 @@ c_util - C code navigation utility
|
||||
Commands:
|
||||
toc - Show table of contents for all files
|
||||
description <names...> - Show selected items with comments
|
||||
func <file> <name> - Show function code
|
||||
struct <file> <name> - Show struct definition
|
||||
enum <file> <name> - Show enum definition
|
||||
show <name1> [name2] - Show code for functions/structs/enums
|
||||
"""
|
||||
|
||||
import sys
|
||||
@@ -659,7 +657,7 @@ def get_function_comments(pre_lines):
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: c_util toc | description <name1> [name2] ... | func <file> <funcname> | struct <file> <structname> | enum <file> <enumname>")
|
||||
print("Usage: c_util toc | description <name1> [name2] ... | show <name1> [name2] ...")
|
||||
sys.exit(1)
|
||||
|
||||
cmd = sys.argv[1]
|
||||
@@ -784,65 +782,66 @@ def main():
|
||||
# struct or enum - print with preserved formatting on multiple lines
|
||||
print(f"[{start + 1}-{end + 1}] {extra}")
|
||||
|
||||
elif cmd == 'func':
|
||||
if len(sys.argv) < 4:
|
||||
print("Usage: c_util func <file> <funcname>")
|
||||
elif cmd == 'show':
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: c_util show <name1> [name2] ...")
|
||||
sys.exit(1)
|
||||
|
||||
file = sys.argv[2]
|
||||
funcname = sys.argv[3]
|
||||
target_names = set(sys.argv[2:])
|
||||
first_file = True
|
||||
found_any = False
|
||||
|
||||
if file in project_functions:
|
||||
for func in project_functions[file]:
|
||||
if func['name'] == funcname:
|
||||
lines = contents[file]
|
||||
pre = func['pre']
|
||||
body = lines[func['start']:func['end'] + 1]
|
||||
print('\n'.join(pre + body))
|
||||
sys.exit(0)
|
||||
for f in files:
|
||||
if f not in contents:
|
||||
continue
|
||||
|
||||
# Collect all matching items
|
||||
items = []
|
||||
|
||||
# Check functions
|
||||
for func in project_functions.get(f, []):
|
||||
if func['name'] in target_names:
|
||||
items.append(('function', func))
|
||||
found_any = True
|
||||
|
||||
# Check structs
|
||||
for s in project_structs.get(f, []):
|
||||
if s['name'] in target_names:
|
||||
items.append(('struct', s))
|
||||
found_any = True
|
||||
|
||||
# Check enums
|
||||
for e in project_enums.get(f, []):
|
||||
if e['name'] in target_names:
|
||||
items.append(('enum', e))
|
||||
found_any = True
|
||||
|
||||
if not items:
|
||||
continue
|
||||
|
||||
# Sort by line number
|
||||
items.sort(key=lambda x: x[1]['start'])
|
||||
|
||||
# Print empty line between files (except before first)
|
||||
if not first_file:
|
||||
print()
|
||||
first_file = False
|
||||
|
||||
# Print filename header
|
||||
print(f"{f}:")
|
||||
|
||||
# Print items
|
||||
for item_type, item in items:
|
||||
lines = contents[f]
|
||||
pre = item['pre']
|
||||
body = lines[item['start']:item['end'] + 1]
|
||||
if pre:
|
||||
print('\n'.join(pre))
|
||||
print('\n'.join(body))
|
||||
|
||||
print("Function not found")
|
||||
sys.exit(1)
|
||||
|
||||
elif cmd == 'struct':
|
||||
if len(sys.argv) < 4:
|
||||
print("Usage: c_util struct <file> <structname>")
|
||||
if not found_any:
|
||||
print("Not found")
|
||||
sys.exit(1)
|
||||
|
||||
file = sys.argv[2]
|
||||
structname = sys.argv[3]
|
||||
|
||||
if file in project_structs:
|
||||
for s in project_structs[file]:
|
||||
if s['name'] == structname:
|
||||
lines = contents[file]
|
||||
pre = s['pre']
|
||||
body = lines[s['start']:s['end'] + 1]
|
||||
print('\n'.join(pre + body))
|
||||
sys.exit(0)
|
||||
|
||||
print("Struct not found")
|
||||
sys.exit(1)
|
||||
|
||||
elif cmd == 'enum':
|
||||
if len(sys.argv) < 4:
|
||||
print("Usage: c_util enum <file> <enumname>")
|
||||
sys.exit(1)
|
||||
|
||||
file = sys.argv[2]
|
||||
enumname = sys.argv[3]
|
||||
|
||||
if file in project_enums:
|
||||
for e in project_enums[file]:
|
||||
if e['name'] == enumname:
|
||||
lines = contents[file]
|
||||
pre = e['pre']
|
||||
body = lines[e['start']:e['end'] + 1]
|
||||
print('\n'.join(pre + body))
|
||||
sys.exit(0)
|
||||
|
||||
print("Enum not found")
|
||||
sys.exit(1)
|
||||
|
||||
else:
|
||||
print("Unknown command")
|
||||
|
||||
Reference in new issue
Block a user