下載地址:http://sourceforge.net/projects/astyle/
在CSDN的下載地址爲:http://download.csdn.net/detail/jackyard/6945053
下載下來後,在bin文件夾下找到astyle.exe,把它放到你的path路徑的隨便一個文件夾下就可以了,因爲這個工具是在cmd下通過命令來調用的
。
1.常用功能(1) 單個文件--缺省美化
astyle --style=ansi Form1.cs
處理前的代碼:
private void Form1_Load(object sender, EventArgs e)
{
int s;
for (int i=0;i<10;i++){
for (int j=0;j<10; j++){
s = s+j+i;}
}
}
處理後:
private void Form1_Load(object sender, EventArgs e)
{
int s;
for (int i=0;i<10;i++)
{
for (int j=0;j<10; j++)
{
s = s+j+i;
}
}
}
(2) 單個文件--更改縮進3個空格
astyle --style=ansi --indent=spaces=3 Form1.c 一般用這個就可以了
缺省縮進一個TAB,也可以顯式說明使用Tab,如下:
astyle --style=ansi --indent=tab Form1.cs
3) 處理多個文件--有限個
astyle --style=ansi Form1.cs Form2.cs
(4) 批量處理多個文件--無限個
for /R .\ %f in (*.cs) do astyle --style=ansi "%f"
說明:/R表明遍歷一個目錄樹,後面緊跟的路徑是根,缺省爲當前目錄。
本例中,根爲.\表示當前目錄,命令等價於:
for /R %f in (*.cs) do astyle --style=ansi "%f"
作用是從(目錄樹根)當前目錄開始,查找所有java文件,包含子目錄中的文件;然後交給astyle處理。
當然,目錄樹根也可以使用絕對路徑,下面的命令查找C盤所有的java文件並處理。
for /R c:\ %f in (*.cs) do astyle --style=ansi "%f
2. 其他比較有用的開關:(均在--style之前寫)
(1) -f
在兩行不相關的代碼之間插入空行,如import和public class之間、public class和成員之間等;
(2) -p
在操作符兩邊插入空格,如=、+、-等。
如:int a=10*60;
處理後變成int a = 10 * 60;
(3) -P
在括號兩邊插入空格。另,-d只在括號外面插入空格,-D只在裏面插入。
如:MessageBox.Show ("aaa");
處理後變成MessageBox.Show ( "aaa" );
(4) -U
移除括號兩邊不必要的空格。
如:MessageBox.Show ( "aaa" );
處理後變成MessageBox.Show ("aaa");
(5) -V
將Tab替換爲空格。
增加到sourceinsigt中:
Windows平臺下也有好多人都喜歡用SourceInsight編輯C/C++程序,但是SourceInsight沒有提供對代碼格式化的功能,如果將Artistic Style集成到SourceInsight中,那就可以爲它擴展出代碼格式化的功能了。假定AStyle.exe的目錄是“C:\ArtisticStyle\”,在該目錄下有一個“c.opt”文件是用來保存配置的文件。下面簡要地介紹下Artistic Style集成到SourceInsight中的方法。
1. 打開你的SourceInsight, 選擇菜單“Options-->Custom Commands-->Add”, 輸入Artistic Style(可以隨便輸入一個名字)。
2. Run中輸入: C:\ArtisticStyle\Astyle.exe --options=c.opt %f
3. Dir留空,將Iconic Window, Capture Output, Parse Links in OutPut, File,then Line 四項前打上勾。
4. 然後點對話框中右側的按鈕“Menu”, Menu--->Menu-->View--><end of menu>, 右側Insert, OK.
5. 此時在SourceInsight中的View菜單下多了個Style的子菜單選項,可以用它來對單個C/C++文件進行格式化。
here is a link for help:http://www.cnblogs.com/xuxm2007/archive/2013/04/06/3002390.html