一些有用的link
Astyle下载官网,下面有个download,点进去就是了
AStyle 3.1全部命令行参数英文原生文档
AStyle 3.1 全部命令行参数中文说明。机翻(编程语言代码格式化美化工具)
Astyle使用方法,如何在source insight上使用
在Notepad++中使用Astyle
在keil中使用Astyle
命令基础使用
- 单个文件–更改缩进3个空格
astyle --style=ansi --indent=spaces=3 Form1.c 一般用这个就可以了 - 缺省缩进一个TAB,也可以显式说明使用Tab,如下:
astyle --style=ansi --indent=tab Form1.cs - 处理多个文件–有限个
astyle --style=ansi Form1.cs Form2.cs
win上批处理方法(.bat)
for /R .\ %f in (*.cpp;*.c;*.h) do "C:\gsp\mybin\AStyle.exe" -A4 -p -U -k3 -t4 -S -M120 -Y -y -xb -j -xf -xh -xW "%f"
说明:
/R:表明遍历一个目录树,后面紧跟的路径,缺省为当前目录。
.\:当前目录
%f:找到的文件名的通配符
(*.c):表示所有以.c结尾的文件
do: 执行后面的语句<astyle --style=ansi "%f">,其中的%f会被替换成找到的*.c的文件名
作用:
从当前目录开始,查找所有以.c结尾的文件,包含子目录中的文件;然后交给astyle处理。
linux上批量处理脚本(.sh)
dir=`pwd`
filelist=`find $dir -type f -name "*.c" -or -name "*.h"`
for file in $filelist
do
astyle --style=ansi --indent=spaces=4 $file
done
其他比较有用的设置:
- -f
在两行不相关的代码之间插入空行。
如import和public class之间、public class和成员之间等; - -V
将Tab替换为空格。
个人偏好设置:
–style=stroustrup / -A4
说明:
Stroustrup 样式使用 linux 大括号,其中右大括号与右大括号断开(例如 --break-close-headers)。左大括号仅从函数定义中断开。左大括号附加到其他所有内容,包括函数中的命名空间、类、数组、结构、枚举和语句。
- 函数大括号独占一行
- 函数内语句大括号起始不独占,结束独占。括号和该语句对齐(而不是向里面缩进一格)
int Foo(bool isBar)
{
if (isBar) {
bar();
return 1;
}
else
return 0;
}
–pad-oper / -p 操作符前后插入空格
if (foo==2)
a=bar((b-c)*a,d--);
// 操作符前后插入空格后
if (foo == 2)
a = bar((b - c) * a, d--);
有用但是不需要输入 --pad-comma / -xg 在逗号后插入空格,如果–pad-oper / -p开启,就不需要用了
if (isFoo(a,b))
bar(a,b);
becomes:
if (isFoo(a, b))
bar(a, b);
–unpad-paren / -U 删除括号内外额外空格。
// 原始的
if ( isFoo( ( a+2 ), b ) )
bar ( a, b );
// 删除括号内外额外空格
if(isFoo((a+2), b))
bar(a, b);
–align-pointer=name / -k3 将指针或引用运算符(*、&或^)附加到变量名(右)
// 原始的
char* foo1;
char & foo2;
string ^s1;
// 附加到变量名:注意指针*的变化
char *foo1;
char &foo2;
string ^s1;
–indent=tab=4 / -t4 缩进设置,使用tab缩进,tab等于4空格
缩进的其他用法:
--indent=spaces=# / -s#
// 缩进使用#空间每缩进(例如:--indent=spaces=3 / -s3)
// #必须在2到20之间。不指定#将导致每个缩进默认4个空格。
--indent=tab=# / -t#
缩进使用缩进的制表符和连续行对齐的空格。这可以确保代码正确显示,而不管查看器的选项卡大小如何。将每个缩进作为#空格(例如:--indent=tab=6 / -t6)#必须在2到20之间。如果未设置#,则将缩进视为4个空格
–indent-switches / -S 缩进“Switch”块
// 原始的
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
// 缩进后
switch (foo)
{
case 1:
a += 1;
break;
case 2:
{
a += 2;
break;
}
}
–max-continuation-indent=120 / -M120 设置最大缩进续行为120空格。最大值120
缩进续行,就是代码一行没写完,写在下一行的时候,前面的空格最多能有多少个。
因为有时候代码可能会很长,但是又想将参数都写在一块儿,所以需要能设置成最大值。
// 代码中前面空格最多能有多少个,
//注释中的120和130的0刚好是第120/130个字符
//test 120----------++++++++++----------++++++++++----------++++++++++----------++++++++++----------++++++++++-------120+++++++130
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
/*修改后的效果如下*/
//注释中的120和130的0刚好是第120/130个字符
//test 120----------++++++++++----------++++++++++----------++++++++++----------++++++++++----------++++++++++-------120+++++++130
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
–indent-col1-comments / -Y 注释与代码一起缩进。
void Foo()
{
// comment
if (isFoo)
bar();
}
becomes:
void Foo()
{
// comment
if (isFoo)
bar();
}
–break-closing-braces / -y 右页眉大括号始终与其他样式断开
当与 --style=java、–style=kr、–style=stroustrup、–style=linux 或 --style=1tbs 一起使用时,这会将结束标头(例如 ‘else’、‘catch’,…)从紧接在紧接的右大括号中分离出来。右页眉大括号始终与其他样式断开。
void Foo(bool isFoo) {
if (isFoo) {
bar();
} else {
anotherBar();
}
}
//becomes (a broken 'else'):
void Foo(bool isFoo) {
if (isFoo) {
bar();
}
else {
anotherBar();
}
}
–break-one-line-headers / -xb 将一行代码if (isFoo1) bar1();分成两行
void Foo(bool isFoo)
{
if (isFoo1) bar1();
if (isFoo2) { bar2(); }
}
becomes:
void Foo(bool isFoo)
{
if (isFoo1)
bar1();
if (isFoo2) {
bar2();
}
}
–add-braces / -j 将大括号添加到未带括号的一行条件语句
if (isFoo)
isFoo = false;
becomes:
if (isFoo) {
isFoo = false;
}
–attach-return-type / -xf 和下面的一起用,将返回类型附加到函数名称
用于函数定义 (-xf)
–attach-return-type-decl / -xh 将返回类型附加到函数名称
这两个选项分别用于函数定义 (-xf) 和函数声明或签名 (-xh)
void
Foo(bool isFoo);
//becomes:
void Foo(bool isFoo);
–indent-preproc-block / -xW #if一类的预处理缩进
在大括号级别零处缩进预处理器块,并立即缩进命名空间中。对缩进的内容有限制。方法、类、数组等中的块不会缩进。包含大括号或多行定义语句的块将不会缩进。如果没有此选项,预处理器块将不会缩进。
#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
个人偏好设置汇总参数
"C:\gsp\mybin\AStyle.exe" -A4 -p -U -k3 -t4 -S -M120 -Y -y -xb -j -xf -xh -xW
--pad-oper / -p 操作符前后插入空格
--unpad-paren / -U 删除括号内外额外空格。
--align-pointer=name / -k3 将指针或引用运算符(*、&或^)附加到变量名(右)
--indent=tab=4 / -t4 缩进设置,使用tab缩进,tab等于4空格
--indent-switches / -S 缩进“Switch”块
--max-continuation-indent=120 / -M120 设置最大缩进续行为120空格。最大值120
--indent-col1-comments / -Y 注释与代码一起缩进。
--break-closing-braces / -y 右页眉大括号始终与其他样式断开
--break-one-line-headers / -xb 将一行代码if (isFoo1) bar1();分成两行
--add-braces / -j 将大括号添加到未带括号的一行条件语句
--attach-return-type / -xf 和下面的一起用,将返回类型附加到函数定义
--attach-return-type-decl / -xh 将返回类型附加到函数声明
--indent-preproc-block / -xW #if一类的预处理缩进
功能测试代码,修改前
// 测试代码:
#if 1
#include<stdio.h>
#if 1
#define TEST
#endif
#include<string.h>
#endif
int
main(int, char*);
int
main( int argc,
char* argv[]){
char* a;
char * b;
char *c;
// commit
1=1;
if(1)return;
if( 1==argc ) return;
if( 1 ){
}else{1=1;}
//test 120----------++++++++++----------++++++++++----------++++++++++----------++++++++++----------++++++++++-------120+++++++130
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name=func(param1,
param1,param1,param1,param);
switch(argc)
{
case 1:
argc=(((3*5),(4*2)+7)+8);
break;
case 2:
break;
}}
被调整过后的代码如下,后面注释是实现了什么功能
// 整理后代码,后面注释是实现了什么功能,建议两个用BC对比看
#if 1
#include<stdio.h>
#if 1
#define TEST
#endif
#include<string.h>
#endif
int main (int, char *); // --attach-return-type-decl / -xh 将返回类型附加到函数声明
int main (int argc,
char *argv[]) // --attach-return-type / -xf 和下面的一起用,将返回类型附加到函数定义.
{ // -A4
char *a; // --align-pointer=name / -k3 将指针或引用运算符(*、&或^)附加到变量名(右)
char *b;
char *c;
// commit // --indent-col1-comments / -Y 注释与代码一起缩进。
1 = 1; // --pad-oper / -p 操作符前后插入空格;
if (1) { // --add-braces / -j 将大括号添加到未带括号的一行条件语句
return; // --break-one-line-headers / -xb 将一行代码if (isFoo1) bar1();分成两行
}
if (1 == argc) { // 上面三条和下面一条汇总的处理结果
return;
}
if (1) { // --unpad-paren / -U 删除括号内外额外空格
} // --break-closing-braces / -y 右页眉大括号始终与其他样式断开
else {
1 = 1;
}
//test 120----------++++++++++----------++++++++++----------++++++++++----------++++++++++----------++++++++++-------120+++++++130
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
I_am_a_very_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_param_name = func (param1,
param1, param1, param1, param);
switch (argc) { // --indent-switches / -S 缩进“Switch”块
case 1:
argc = ( ( (3 * 5), (4 * 2) + 7) + 8); // --pad-paren-out / -d 在括号外侧增加空格。对于多括号表达式方便看
break;
case 2:
break;
} // --break-closing-braces / -y 右页眉大括号始终与其他样式断开
}
我目前不用,但是后面可能会用到设置
–indent-labels / -L 向标签中添加额外的缩进
向标签中添加额外的缩进,使它们比当前的缩进小一个缩进,而不是被冲到左边(默认)。
// 原始的,注意error位置
void Foo() {
while (isFoo) {
if (isFoo)
goto error;
...
error:
...
}
}
// 缩进后,注意error位置
void Foo() {
while (isFoo) {
if (isFoo)
goto error;
...
error:
...
}
}
–pad-paren-out / -d 在括号外侧增加空格。对于多括号表达式方便看
if (isFoo((a+2), b))
bar(a, b);
becomes:
if (isFoo ( (a+2), b) )
bar (a, b);
// (a+2)的外侧是左侧,因为右边是逗号已经分割了
// (a+2), b)的外侧就是左右都要加空格,否则两个正括号、反括号就在一起了,很难分清层次
-n 不保留原始文件.orig
格式化文件时,新缩进的文件保留原始文件名。 创建原始文件的副本,并在原始文件名后附加 .orig。 (这可以通过选项 -n 或 --suffix=none 完全抑制)。 因此,在缩进 SourceFile.cpp 之后,缩进的文件将被命名为 SourceFile.cpp,而原始的预缩进的文件将被重命名为 SourceFile.cpp.orig。
如何在source insight上使用:
假定AStyle.exe的目录是“C:\ArtisticStyle\”,在该目录下有一个“c.opt”文件是用来保存配置的文件。下面简要地介绍下Artistic Style集成到SourceInsight中的方法。
- 打开Source Insight, 选择菜单“Options–>Custom Commands–>Add”, 输入Astyle(可以随便输入一个名字)。Source Insight 4.0 在“Tools–>Custom Commands–>Add”
- Run中输入: C:\ArtisticStyle\Astyle.exe --options=c.opt %f
- Dir留空,将Iconic Window, Capture Output, Parse Links in OutPut, File,then Line 四项前打上勾。
- 然后点对话框中右侧的按钮“Menu”, Menu—>Menu–>View–>, 右侧Insert, OK.
- 此时在SourceInsight中的View菜单下多了个Astyle的子菜单选项,可以用它来对单个C/C++文件进行格式化。
- 如果想创建快捷键,“Options–>Key Assignments”。在command里面输入你刚刚起的名字:Astyle就就能查找到对应的命令。选中该命令,然后点击"Assigh New Key",输入快捷键:XXX(自己定义)。以后格式化C/C++文件,就可以直接使用快捷键了。