Astyle 给代码调整格式

来源:互联网 发布:网络直播策划方案脚本 编辑:程序博客网 时间:2024/05/29 18:16

1. 代码格式化,往往是程序员头疼的事情,不同公司代码格式不一样,风格也不一样。提供几种方法吧。

    1) astyle

astyle -A1 -z2 -xC100 -xL -j -O -o -s4 -K -m0 -M80 -w -c -H -U -p -Z -n -k3 *.c  短指令

-A1   

--style=allman / --style=bsd / --style=break / -A1

Allman style uses broken brackets.

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

--lineend=windows / -z1
--lineend=linux   / -z2
--lineend=macold  / -z3 

Force use of the specified line end style. Valid options are windows (CRLF), linux (LF), and macold (CR). MacOld style is the format for Mac OS 9 and earlier. OS X uses the Linux style. If one of these options is not used the line ends will be determined automatically from the input file.

--attach-classes / -xc
Attach brackets to a class statement. This is done regardless of the bracket style being used.

the bracket is always attached to a class statement:

class FooClass {...};

 

--attach-inlines / -xl
Attach brackets to class and struct inline function definitions. This is not done for run-in type brackets (Horstmann and Pico styles). This option is effective for C++ files only.

all brackets are always attached to class and struct inline function definitions:

class FooClass{    void Foo() {    ...    }};

 

--add-brackets / -j 
Add brackets to unbracketed one line conditional statements (e.g. 'if', 'for', 'while'...). The statement must be on a single line. The brackets will be added according to the currently requested predefined style or bracket type. If no style or bracket type is requested the brackets will be attached. If --add-one-line-brackets is also used the result will be one line brackets.

if (isFoo)    isFoo = false;

becomes:

if (isFoo) {    isFoo = false;}

--keep-one-line-blocks / -O 
Don't break one-line blocks.

if (isFoo){ isFoo = false; cout << isFoo << endl; }

remains unchanged.

--keep-one-line-statements / -o 
Don't break complex statements and multiple statements residing on a single line.

if (isFoo){    isFoo = false; cout << isFoo << endl;}

remains unchanged.

if (isFoo) DoBar();

remains unchanged.

default indent
If no indentation option is set, the default option of 4 spaces will be used (e.g. -s4 --indent=spaces=4 ).

with default values:

void Foo() {....if (isBar1............&& isBar2)    // indent of this line can be changed with min-conditional-indent........bar();}

 

--indent-cases / -K
Indent 'case X:' blocks from the 'case X:' headers. Case statements not enclosed in blocks are NOT indented.

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;        }}

--min-conditional-indent=# / -m#
Set the minimal indent that is added when a header is built of multiple lines. This indent helps to easily separate the header from the command statements that follow. The value for # indicates a number of indents and is a minimum value. The indent may be greater to align with the data on the previous line.
The valid values are:
0 - no minimal indent. The lines will be aligned with the paren on the preceding line.
1 - indent at least one additional indent.
2 - indent at least two additional indents.
3 - indent at least one-half an additional indent. This is intended for large indents (e.g. 8).
The default value is 2, two additional indents.

// default setting makes this non-bracketed code clearif (a < b        || c > d)    foo++;// but creates an exaggerated indent in this bracketed codeif (a < b        || c > d){    foo++;}

becomes (when setting --min-conditional-indent=0):

// setting makes this non-bracketed code less clearif (a < b    || c > d)    foo++;// but makes this bracketed code clearerif (a < b    || c > d){    foo++;}

--max-instatement-indent=# / -M#
Set the  maximum of # spaces to indent a continuation line. The # indicates a number of columns and must not be greater than 120. If no # is set, the default value of 40 will be used. A maximum of less than two indent lengths will be ignored. This option will prevent continuation lines from extending too far to the right. Setting a larger value will allow the code to be extended further to the right.

fooArray[] = { red,         green,         blue };fooFunction(barArg1,         barArg2,         barArg3);

becomes (with larger value):

fooArray[] = { red,               green,               blue };fooFunction(barArg1,            barArg2,            barArg3);

--indent-preproc-define / -w
Indent multi-line preprocessor definitions ending with a backslash. Should be used with --convert-tabs for proper results. Does a pretty good job, but cannot perform miracles in obfuscated preprocessor definitions. Without this option the preprocessor statements remain unchanged.

#define Is_Bar(arg,a,b) \(Is_Foo((arg), (a)) \|| Is_Foo((arg), (b)))

becomes:

#define Is_Bar(arg,a,b) \    (Is_Foo((arg), (a)) \     || Is_Foo((arg), (b)))

 

--convert-tabs / -c
Converts tabs into spaces in the non-indentation part of the line. The number of spaces inserted will maintain the spacing of the tab. The current setting for spaces per tab is used. It may not produce the expected results if convert-tabs is used when changing spaces per tab. Tabs are not replaced in quotes.


--pad-header / -H 
Insert space padding between a header (e.g. 'if', 'for', 'while'...) and the following paren. Any end of line comments will remain in the original column, if possible. This can be used with unpad-paren to remove unwanted spaces.

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

becomes:

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

--unpad-paren / -U 
Remove extra space padding around parenthesis on the inside and outside. Any end of line comments will remain in the original column, if possible. This option can be used in combination with the paren padding options pad‑parenpad‑paren‑outpad‑paren‑in, and pad‑header above. Only padding that has not been requested by other options will be removed.

For example, if a source has parens padded on both the inside and outside, and you want inside only. You need to use unpad-paren to remove the outside padding, and pad‑paren‑in to retain the inside padding. Using only pad‑paren‑in would not remove the outside padding.

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

becomes (with no padding option requested):

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

--pad-oper / -p 
Insert space padding around operators. Any end of line comments will remain in the original column, if possible. Note that there is no option to unpad. Once padded, they stay padded.

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

becomes:

if (foo == 2)    a = bar((b - c) * a, d--);
--preserve-date / -Z
Preserve the original file's date and time modified. The time modified will be changed a few micro seconds to force the changed files to compile. This option is not effective if redirection is used to rename the input file.


--suffix=none / -n
Do not retain a backup of the original file. The original file is purged after it is formatted.


--align-pointer=type   / -k1
--align-pointer=middle / -k2
--align-pointer=name   / -k3 

Attach a pointer or reference operator (*, &, or ^) to either the variable type (left) or variable name (right), or place it between the type and name (middle). The spacing between the type and name will be preserved, if possible. This option is for C/C++, C++/CLI, and C# files. To format references separately use the following align-reference option.

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;

AStyle.exe *.c  --indent-switches --indent-cases --indent-preproc-define --convert-tabs --pad-header --pad-oper --preserve-date --suffix=none --align-pointer=name 

-s4 --indent=spaces=4


--indent-switches --indent-cases --indent-preproc-define --convert-tabs--pad-header --pad-oper \

--preserve-date --suffix=none --align-pointer=name \

--quiet


--indent-switches:缩进switch

--indent-cases:缩进case

--indent-preproc-define:缩进预处理标记(#if/#else/#endif等)

--convert-tabs:转换Tab为4个空格

--pad-header:在if/while/switch后添加空格

--pad-oper:操作符前后添加空格

--preserve-date:不修改文件的时间戳

--suffix=none:不备份原始文件(依赖SVN比较文件差异)

--align-pointer=name:指针符号*紧跟在名字前面,例如char *str;

--quiet:安静模式,不显示处理过程信息


      http://astyle.sourceforge.net/astyle.html#_General_Information

                         

0 0
原创粉丝点击