from : http://blog.csdn.net/gw_cs/article/details/7927735
一、AStyle下载路径
Astyle为开源项目,支持C/C++和java的代码格式化
Home Page: http://astyle.sourceforge.net/
Project Page: http://sourceforge.net/projects/astyle/
下载最新Windows版本的AStyle,解压到你指定位置,笔者解压到D:\program files下的。
二、Source Insight中安装Astyle工具
如:增加Astyle命令,格式化C/C++文件
1. 打开source insight中的一个工程,选择Options--Custom Commands命令
2. 点击add,添加新的命令Astyle。
1)ansi C 格式化当前文件的命令为
其中,Run中输入astyle的命令参数:"D:\program files\AStyle\bin\AStyle.exe" --style=ansi-s4 -S -N -L -m0 -M40 --convert-tabs --suffix=.pre %f
2)ansi C 格式化某个目录下所有文件的命令为:
"D:\program files\AStyle\bin\AStyle.exe" Z:\LocalProject\DEZHOU3716/*.c Z:\LocalProject\DEZHOU3716/*.h --style=ansi -s4 -S -N -L -m0 -M40 --recursive --convert-tabs --suffix=.pre %f
3. 添加menu菜单,点击“menu”,选择menu--View,然后INSERT。这样就可以在菜单栏View上多一个命令选项。
4. 添加快捷键,点击"Assigh New Key",输入快捷键:Ctrl+Alt+C。以后格式化C/C++文件,就可以直接使用快捷键了。
同理,新建java格式化命令,Astyle参数为
C:\AStyle\bin\AStyle.exe --style=java -s4 -S -N -L -m0 -M40 --suffix=none --convert-tabs %f
三、其他常用的参数:
-C |
类中public,pretected,private关键字,一个tab的缩进 |
-S |
switch中case关键字,一个tab的缩进 |
-K |
switch中case关键字,无缩进 |
-N |
被namespace包含的block,一个tab的缩进 |
-w |
格式化多行的宏定义 |
-c |
将tab转化为对应个数的空格 |
--mode=c |
格式化的是C/C++的源文件或者头文件(缺省值) |
--mode=java |
格式化的是JAVA的源文件 |
--suffix=#### |
将原始文件保存为“####”后缀,而不是“orig” |
--suffix=none |
不保存原始文件 |
--exclude=#### |
优化时不包含“####”文件或目录 |
-Z |
修改后保持文件的修改时间不变 |
-X |
将错误信息输出到标准输出设备(stdout),而不是标准错误设备(stderr) |
-Q |
只显示格式化前后发生变化的文件 |
-q |
不输出任何信息 |
-z1 |
使用windows版本的回车符(CRLF) |
-z2 |
使用linux版本的回车符(LF) |
--help |
显示帮助信息 |
-v |
显示版本信息 |
基于V2.02版本,astyle主要支持的参数有:
Style-格式配置:
最常用的就是ansi或或kr格式,实际上,kr,stroustrup和linux这三种格式是非常接近的了,试了好几个文件,只有非常微小的区别,可以忽略不计。
stype |
--style=allman |
--style=java |
--style=kr |
--style=stroustrup |
--style=whitesmith |
--style=banner |
代码风格 |
int Foo() |
int Foo() { |
int Foo() |
int Foo() |
int Foo() |
int Foo() { |
stype |
--style=gnu |
--style=linux |
--style=horstmann |
--style=1tbs |
--style=pico |
--style=lisp |
代码风格 |
int Foo() |
int Foo() |
int Foo() |
int Foo() |
int Foo() |
int Foo() { |
参考文档:
1. http://www.cnblogs.com/zhaoshixin/archive/2011/12/02/2272076.html
2. http://astyle.sourceforge.net/astyle.html
本人注:
根据以上写bat:AstyleCmd.bat,内容如下,将该bat当前目录下所有c/h 文件全部格式化。
@for /R %%a in (*.c;*.h;*.cpp) do @AStyle.exe --style=ansi -U -p -K -S -c -n "%%a"
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
Astyle.exe --help
Artistic Style 1.22
Maintained by: Jim Pattee
Original Author: Tal Davidson
Usage : astyle [options] Source1.cpp Source2.cpp [...]
astyle [options] < Original > Beautified
When indenting a specific file, the resulting indented file RETAINS the
original file-name. The original pre-indented file is renamed, with a
suffix of ".orig" added to the original filename.
By default, astyle is set up to indent C/C++/C#/Java files, with 4 spaces
per indent, a maximal indentation of 40 spaces inside continuous statements,
and NO formatting.
Option's Format:
----------------
Long options (starting with '--') must be written one at a time.
Short options (starting with '-') may be appended together.
Thus, -bps4 is the same as -b -p -s4.
Predefined Style Options:
-------------------------
--style=ansi
ANSI style formatting/indenting.
--style=gnu
GNU style formatting/indenting.
--style=kr
Kernighan&Ritchie style formatting/indenting.
--style=linux
Linux mode (8 spaces per indent, break definition-block
brackets but attach command-block brackets).
--style=java
Java mode, with standard java style formatting/indenting.
Tab and Bracket Options:
------------------------
default indent option
If no indentation option is set,
the default option of 4 spaces will be used.
--indent=spaces=# OR -s#
Indent using # spaces per indent. Not specifying #
will result in a default of 4 spaces per indent.
--indent=tab OR --indent=tab=# OR -t OR -t#
Indent using tab characters, assuming that each
tab is # spaces long. Not specifying # will result
in a default assumption of 4 spaces per tab.
--force-indent=tab=# OR -T#
Indent using tab characters, assuming that each
tab is # spaces long. Force tabs to be used in areas
Astyle would prefer to use spaces.
default brackets option
If no brackets option is set,
the brackets will not be changed.
--brackets=break OR -b
Break brackets from pre-block code (i.e. ANSI C/C++ style).
--brackets=attach OR -a
Attach brackets to pre-block code (i.e. Java/K&R style).
--brackets=linux OR -l
Break definition-block brackets and attach command-block
brackets.
--brackets=break-closing OR -y
Break brackets before closing headers (e.g. 'else', 'catch', ...).
Should be appended to --brackets=attach or --brackets=linux.
Indentation options:
--------------------
--indent-classes OR -C
Indent 'class' blocks, so that the inner 'public:',
'protected:' and 'private: headers are indented in
relation to the class block.
--indent-switches OR -S
Indent 'switch' blocks, so that the inner 'case XXX:'
headers are indented in relation to the switch block.
--indent-cases OR -K
Indent case blocks from the 'case XXX:' headers.
Case statements not enclosed in blocks are NOT indented.
--indent-brackets OR -B
Add extra indentation to '{' and '}' block brackets.
--indent-blocks OR -G
Add extra indentation entire blocks (including brackets).
--indent-namespaces OR -N
Indent the contents of namespace blocks.
--indent-labels OR -L
Indent labels so that they appear one indent less than
the current indentation level, rather than being
flushed completely to the left (which is the default).
--indent-preprocessor OR -w
Indent multi-line #define statements.
--max-instatement-indent=# OR -M#
Indent a maximal # spaces in a continuous statement,
relative to the previous line.
--min-conditional-indent=# OR -m#
Indent a minimal # spaces in a continuous conditional
belonging to a conditional header.
Formatting options:
-------------------
--break-blocks OR -f
Insert empty lines around unrelated blocks, labels, classes, ...
--break-blocks=all OR -F
Like --break-blocks, except also insert empty lines
around closing headers (e.g. 'else', 'catch', ...).
--break-elseifs OR -e
Break 'else if()' statements into two different lines.
--pad=oper OR -p
Insert space paddings around operators.
--pad=paren OR -P
Insert space padding around parenthesis on both the outside
and the inside.
--pad=paren-out OR -d
Insert space padding around parenthesis on the outside only.
--pad=paren-in OR -D
Insert space padding around parenthesis on the inside only.
--unpad=paren OR -U
Remove unnecessary space padding around parenthesis. This
can be used in combination with the 'pad' options above.
--one-line=keep-statements OR -o
Don't break lines containing multiple statements into
multiple single-statement lines.
--one-line=keep-blocks OR -O
Don't break blocks residing completely on one line.
--convert-tabs OR -c
Convert tabs to spaces.
--fill-empty-lines OR -E
Fill empty lines with the white space of their
previous lines.
--mode=c
Indent a C or C++ source file (this is the default).
--mode=java
Indent a Java source file.
--mode=cs
Indent a C# source file.
Other options:
--------------
--suffix=####
Append the suffix #### instead of '.orig' to original filename.
--suffix=none OR -n
Do not retain a backup of the original file.
--options=####
Specify an options file #### to read and use.
--options=none
Disable the default options file.
Only the command-line parameters will be used.
--recursive OR -r OR -R
Process subdirectories recursively.
--exclude=####
Specify a file or directory #### to be excluded from processing.
--preserve-date OR -Z
The date and time modified will not be changed in the formatted file.
--verbose OR -v
Verbose mode. Extra informational messages will be displayed.
--errors-to-stdout OR -X
Print errors and help information to standard-output rather than
to standard-error.
--version OR -V
Print version number.
--help OR -h OR -?
Print this help message.
Default options file:
---------------------
Artistic Style looks for a default options file in the
following order:
1. The contents of the ARTISTIC_STYLE_OPTIONS environment
variable if it exists.
2. The file called .astylerc in the directory pointed to by the
HOME environment variable ( i.e. $HOME/.astylerc ).
3. The file called astylerc in the directory pointed to by the
USERPROFILE environment variable ( i.e. %USERPROFILE%\astylerc ).
If a default options file is found, the options in this file
will be parsed BEFORE the command-line options.
Long options within the default option file may be written without
the preliminary '--'.