关于代码格式化工具astyle

来源:互联网 发布:小米手环无法同步数据 编辑:程序博客网 时间:2024/05/17 14:17

function astyleformat()
{
    local ASTYLE=`which astyle`
    local FINDCMD=`which find`
    local OLDFOLDER=`pwd`
    if [ "x$ASTYLE" == "x" ] ; then
        mkdir -p /tmp/astyle
        local CMD_DLOAD="Y3VybCBmdHA6Ly8xMC4yNy4xNi45NS9hc3R5bGVfMi4wNF9saW51eC50YXIuZ3ogLXUgdWJ1bnR1OjEyMzQ1NiAtbyAvdG1wL2FzdHlsZS9hc3R5bGVfMi4wNF9saW51eC50YXIuZ3oK"
        `echo "${CMD_DLOAD}" | base64 -d --wrap=0`
        cd /tmp/astyle && tar xf astyle_2.04_linux.tar.gz && cd astyle/build/gcc && make && sudo make install
        cd $OLDFOLDER
        rm -rf /tmp/astyle
    fi
    cd $OLDFOLDER
    ASTYLE=`which astyle`
    local PATH="$1"
    if [ "$PATH" ] ; then
        PATH=$1
    else
        PATH="."
    fi
    $ASTYLE -A2 -c -C -U -Sw -p -f -x -H -j -k3 -n `$FINDCMD $PATH -name '*.cpp' -o -name '*.h' -o -name '*.c' `
}

function stylecheck()
{
    local OLDPWD=`pwd`
    rm temp.txt
    astyleformat $@
    local autocpplint="$(gettop)/sdk/tools/cpplint_forgaia/autoexec_cpplint.py"
    local autofixpy="$(gettop)/sdk/tools/cppfix_forgaia/cppfix_forgaia.py"
    python $autocpplint $@
    python $autofixpy "$(gettop)/sdk/tools/cpplint_forgaia/temp.txt"
    python $autocpplint $@
    cp "$(gettop)/sdk/tools/cpplint_forgaia/temp.txt" $OLDPWD
    echo "Coding Style Checking success, Please view the result in ./temp.txt"
}



http://astyle.sourceforge.net

http://blog.csdn.net/armygeneral/article/details/6346515


find ./ -name a2dp.c -exec astyle --style=ansi {} \; 分号不能缺少



1. 格式化从根开始的整个项目

astyle -A1 -R ./*.c ./*.h ./*.cpp-bt4 -Sw -fpxHU -Jk3 -n

astyle -A2 -R ./*.java -bt4 -Sw -fpxHU -Jk3 -n


$ astyle -A2 -c -C -U -p -xd -H `find . -name '*.cpp' -o -name '*.h'`$ astyle -A2 -c -C -U -p -xd -H `find . -name '*.cpp' -o -name '*.h'`

参数                      意义

Predefined Style Options

-A1         代表  --style = ansi  or   allman模式

 

Tab and Bracket options

-b         代表  语句块之前加上空格

-t4         代表  each tab as 4 spaces

 

Indentation options

-S         代表  switch开关语句,以便于使case x: 对自动对齐

-w        代表  用“/”对齐多行参数

 

Padding options

-f          代表  不相关的代码之间加空行

-p         代表  双目运算符前后加上空格,如:+  -  *  /

-x      代表  删除相关语句之是的空行. 如:if, else语句间的空行

-H           代表  在if, for, while关键字与括号()之间加上空格

-U         代表  移除括号两边不必要的空格

 

 

Formatting options

-J           代表  为单行语句加上{}

-k3        代表  指针及引用(*, &)符号,挨着变量名

 

Other option

-n         代表  不生成备份。默认生成 .orig文件

-R        代表  目录递归



 2. 格式化当前目录下的.c文件

astyle -A1 -R ./test/*.c  -bt4 -Sw -fpxHU -Jk3 -n

参数意义从略,看上面.

-R 后面添加所修改工程的路径,及文件名,就可以执行只格式化该目录下的.c文件。路径中的是用“/” or  “/”都可以录找到路径。

 

建议:在进行工程项目部分模块开发时,如果为了使这部分项目模块更规范,可以使用此选项。对于频繁修改少量多个文件。可以使用此选项,以实现批处理文件的功能。

 

3. 格式化单个文件

astyle.exe -A1 -bt4 -Sw -fpxHU -Jk3 -n

参数从略,看上面

只进行当前修改文件的格式化处理。 只针对单个文件起作用。这样很容易使所写文件更加的规范。在编写代码过程中,可以省去很多调整代码结构的时间,是最常用的选项。

 

建议:在平时编写代码时,尽量使用此选项。只对单个文件进行处理,不会影响其它文件。

SVN进行提交时,影响最小。











Bracket Style Options: ----------------------     --style=allman  OR  --style=ansi  OR  --style=bsd         OR  --style=break  OR  -A1     Allman style formatting/indenting.     Broken brackets.     --style=java  OR  --style=attach  OR  -A2     Java style formatting/indenting.     Attached brackets.     --style=kr  OR  --style=k&r  OR  --style=k/r  OR  -A3     Kernighan & Ritchie style formatting/indenting.     Linux brackets.     --style=stroustrup  OR  -A4     Stroustrup style formatting/indenting.     Stroustrup brackets.     --style=whitesmith  OR  -A5     Whitesmith style formatting/indenting.     Broken, indented brackets.     Indented class blocks and switch blocks.     --style=banner  OR  -A6     Banner style formatting/indenting.     Attached, indented brackets.     Indented class blocks and switch blocks.     --style=gnu  OR  -A7     GNU style formatting/indenting.     Broken brackets, indented blocks.     --style=linux  OR  -A8     Linux style formatting/indenting.     Linux brackets, minimum conditional indent is one-half indent.     --style=horstmann  OR  -A9     Horstmann style formatting/indenting.     Run-in brackets, indented switches.     --style=1tbs  OR  --style=otbs  OR  -A10     One True Brace Style formatting/indenting.     Linux brackets, add brackets to all conditionals.     --style=pico  OR  -A11     Pico style formatting/indenting.     Run-in opening brackets and attached closing brackets.     Uses keep one line blocks and keep one line statements.     --style=lisp  OR  -A12     Lisp style formatting/indenting.     Attached opening brackets and attached closing brackets.     Uses keep one line statements.
复制代码

0 0
原创粉丝点击