关于代码格式化工具astyle

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值