Configuration system for CLI (#6708)
[jackhill/qmk/firmware.git] / lib / python / qmk / cli / cformat.py
1 """Format C code according to QMK's style.
2 """
3 import os
4 import subprocess
5
6 from milc import cli
7
8
9 @cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
10 @cli.subcommand("Format C code according to QMK's style.")
11 def cformat(cli):
12 """Format C code according to QMK's style.
13 """
14 clang_format = ['clang-format', '-i']
15
16 # Find the list of files to format
17 if not cli.args.files:
18 for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
19 for dirpath, dirnames, filenames in os.walk(dir):
20 if 'tmk_core/protocol/usb_hid' in dirpath:
21 continue
22
23 for name in filenames:
24 if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'):
25 cli.args.files.append(os.path.join(dirpath, name))
26
27 # Run clang-format on the files we've found
28 try:
29 subprocess.run(clang_format + cli.args.files, check=True)
30 cli.log.info('Successfully formatted the C code.')
31
32 except subprocess.CalledProcessError:
33 cli.log.error('Error formatting C code!')
34 return False