1、编辑文件
/usr/local/bin/astyle.sh
内容如下:
#!/bin/sh
ASTYLE=/usr/local/bin/astyle
# --style=linux OR --style=knf OR -A8
# Linux style formatting/indenting.
# Linux braces, minimum conditional indent is one-half indent.
FLAGS="--style=linux"
# --indent=spaces=# OR -s#
# Indent using # spaces per indent. Not specifying #
# will result in a default of 4 spaces per indent.
FLAGS="$FLAGS --indent=spaces=4"
# --pad-oper OR -p
# Insert space padding around operators.
FLAGS="$FLAGS -p"
# --break-blocks OR -f :
# Insert empty lines around unrelated blocks, labels, classes, ...
FLAGS="$FLAGS -f"
# --suffix=none OR -n
# Do not retain a backup of the original file.
FLAGS="$FLAGS -n"
# --unpad-paren OR -U
# Remove unnecessary space padding around parenthesis. This
# can be used in combination with the 'pad' options above.
FLAGS="$FLAGS -U"
echo $FLAGS
while [ $# -ne 0 ]
do
echo Processing $1...
# -A8 -s4 -pfn
echo $ASTYLE $FLAGS $1
$ASTYLE $FLAGS $1
shift
done
2、设置文件属性
$ chmod +x /usr/local/bin/astyle.sh
3、使用脚本
格式化当前目录下的所有.cpp文件
$ astyle.sh *.cpp
格式化当前目录及子目录下的所有.cpp文件
$ find ./ -name "*.cpp " | xargs astyle.sh
macos系统:
$ brew install a-style
5、安装brew
未安装brew的,请自行安装brew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"