Astyle格式化代码-最好的选项

这里的源代码本来是一片,后来变成了一大片,经过几代人的手之后,成了原始森林,各种风格,各种旁逸斜出.不敢直视.一个switch语句几千万行,一眼望不到边.今天突然有种冲动,想整一下.然后就找到了Astyle(Artistic Style) 
查看了官方文档  http://astyle.sourceforge.net/astyle.html

配置了最佳的教科书格式
astyle *.c  *.cpp *.h --recursive --style=bsd --convert-tabs --indent=spaces=4 --attach-closing-while --indent-switches --indent-namespaces --indent-continuation=4 --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --pad-oper  --unpad-paren --delete-empty-lines --align-pointer=name --align-reference=name --break-elseifs --add-braces --pad-comma --add-one-line-braces 
简化形式

astyle *.cpp *.h -r -A1 -c -s4 -xV -S -N -xt4 -xW -w -xw -Y -p -U -xe -k3 -W3 -e -j -xg -J


节省竖行/部分代码块中花排号开关不另起一行的格式
astyle  *.cpp *.h --recursive --style=linux --convert-tabs --indent=spaces=4 --attach-closing-while --indent-switches --indent-namespaces --indent-continuation=4 --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --pad-oper  --unpad-paren --delete-empty-lines --align-pointer=name --align-reference=name --break-elseifs --add-braces --pad-comma --add-one-line-braces 
astyle *.cpp *.h -r -A8 -c -s4 -xV -S -N -xt4 -xW -w -xw -Y -p -U -xe -k3 -W3 -e -j -xg -J

--indent-switches 缩进case标签
 

switch (foo)
{
case 1:
    a += 1;
    break;

case 2:
{
    a += 2;
    break;
}
}

becomes:

switch (foo)
{
    case 1:
        a += 1;
        break;

    case 2:
    {
        a += 2;
        break;
    }
}

--indent=spaces=8  缩进8个空格

void Foo() 
{
........bar();
}

 

--indent-namespaces 缩进命名空间块

namespace foospace
{
class Foo
{
    public:
        Foo();
        virtual ~Foo();
};
}

becomes:

namespace foospace
{
    class Foo
    {
        public:
            Foo();
            virtual ~Foo();
    };
}

--indent-continuation=4 等号=或(结尾后续本语句符号插入空格,默认为1,可取1~4

isLongVariable =
    foo1 ||
    foo2;

isLongFunction(
    bar1,
    bar2);

becomes (with indent-continuation=3):

isLongVariable =
            foo1 ||
            foo2;

isLongFunction(
            bar1,
            bar2);


--style=bsd 大括号独占一行,上下对齐

 

 

int Foo(bool isBar)
{
    if (isBar)
    {
        bar();
        return 1;
    }
    else
        return 0;
}

 

 

--attach-closing-while (while紧贴)

do
{
    bar();
    ++x;
}
while x == 1;

becomes:

do
{
    bar();
    ++x;
} while x == 1;

 

--indent-preproc-block 缩进#开头的处理语句

#ifdef _WIN32
#include <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif

becomes:

#ifdef _WIN32
    #include <windows.h>
    #ifndef NO_EXPORT
        #define EXPORT
    #endif
#endif

--indent-preproc-cond  预处理语句也缩进
 

        isFoo = true;
#ifdef UNICODE
        text = wideBuff;
#else
        text = buff;
#endif

becomes:

        isFoo = true;
        #ifdef UNICODE
        text = wideBuff;
        #else
        text = buff;
        #endif

--indent-col1-comments 注释也缩进

void Foo()\n"
{
// comment
    if (isFoo)
        bar();
}

becomes:

void Foo()\n"
{
    // comment
    if (isFoo)
        bar();
}

 

--pad-oper 操作符间插入空格
 

if (foo==2)
    a=bar((b-c)*a,d--);

becomes:

if (foo == 2)
    a = bar((b - c) * a, d--);

 

--pad-comma 逗号间插入空格(--pad-oper中已有此效果)
 

if (isFoo(a,b))
    bar(a,b);

becomes:

if (isFoo(a, b))
    bar(a, b);

 

--pad-paren-in 括号里内插入空格
 

if (isFoo((a+2), b))
    bar(a, b);

becomes:

if ( isFoo( ( a+2 ), b ) )
    bar( a, b );

--unpad-paren 紧凑括号内外

if ( isFoo( ( a+2 ), b ) )
    bar ( a, b );

becomes (with no padding option requested):

if(isFoo((a+2), b))
    bar(a, b);

 

--delete-empty-lines 清除函数间的空行
 

void Foo()
{

    foo1 = 1;

    foo2 = 2;

}

becomes:

void Foo()
{
    foo1 = 1;
    foo2 = 2;
}

  指针符号紧贴哪

char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=type):

char* foo1;
char& foo2;
string^ s1;
char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=middle):

char * foo1;
char & foo2;
string ^ s1;
char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=name):

char *foo1;
char &foo2;
string ^s1;

//引用符号紧贴哪
char &foo1;

becomes (with --align-reference=type):

char& foo1;
char& foo2;

becomes (with --align-reference=middle):

char & foo2;
char& foo3;

becomes (with --align-reference=name):

char &foo3;

 

char &foo1;

becomes (with --align-reference=type):

char& foo1;
char& foo2;

becomes (with --align-reference=middle):

char & foo2;
char& foo3;

becomes (with --align-reference=name):

char &foo3;

 

--attach-return-type-decl 返回类型紧贴符号名
 

void
Foo(bool isFoo);

becomes:

void Foo(bool isFoo);

--add-braces 在'if', 'for', 'while'等句块中只有一行也加入大括号

if (isFoo)
    isFoo = false;

becomes:

if (isFoo) {
    isFoo = false;
}

 

--convert-tabs 将TAB符转化成空格,由转化参数指定,引号内的不转化
 

--recursive 遍历目录,文件名要指定为带通配符(*)的名字,含有空格的文件名要加引号
 

    

  • 20
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值