Astyle:代碼格式化工具簡明指南
2010-12-22 12:58:11| 分類: 開發工具 | 标簽: |字号大中小 訂閱
astyle是一個我自己常用的開放源碼工具。它可以友善的将程式代碼格式化成自己想要的樣式而不必人工修改。本來嘛,作為高等生物應該優先去做一些智慧的事情,而不是把時間消耗在機器可以完美完成的事情上。
想要立刻開始?請先去首頁http://sourceforge.net/projects/astyle下載下傳最新版本。可以選擇二進制版本,也可以下載下傳源碼自行編譯。總之得到可執行檔案後請将astyle放在Path(C:\Program Files\Microsoft Visual Studio 8\Common7\IDE)中,這樣會友善很多。
astyle是一個指令行工具,指令文法很簡單:
astyle [options] < original > Beautified
astyle [options] Foo.cpp Bar.cpp [...]
例如:
astyle --style=ansi foo.cpp
上面的指令将美化foo.cpp檔案,更改其風格為ANSI,并将原始檔案備份到foo.cpp.orgin。是以,你可以安全的使用該軟體而不必擔心會将代碼改得無法回頭。
具體的來說,astyle包含了以下幾種預定義風格,隻需在參數中簡單指定即可使用:
--style=ansi:ANSI 風格格式和縮進
namespace foospace { int Foo() { if (isBar) { bar(); return 1; } else return 0; } } |
--style=kr :Kernighan&Ritchie 風格格式和縮進
namespace foospace { int Foo() { if (isBar) { bar(); return 1; } else return 0; } } |
--style=linux :Linux 風格格式和縮進
namespace foospace { int Foo() { if (isBar) { bar(); return 1; } else return 0; } } |
--style=gnu :GNU 風格格式和縮進
namespace foospace { int Foo() { if (isBar) { bar(); return 1; } else return 0; } } |
--style=java :Java 風格格式和縮進
class foospace { int Foo() { if (isBar) { bar(); return 1; } else return 0; } } |
從這裡開始介紹astyle的進階應用!這裡要介紹的是兩種應用情形,一是在Visual Studio中整合,二是批量處理。
先看如何在Visual Studio中整合。看圖說話!
第一步:點選“工具”菜單
第二步:點選“外部工具”
第三步:配置并儲存
在對話框中點選“添加”,如圖填入各項。其中參數填寫 --style=ansi $(ItemFileName)$(ItemExt)
可以勾選“使用輸出視窗”,這樣将不會顯示黑色的指令視窗。相關資訊都會顯示在Visual Studio中。
經過上面設定之後,隻需點選該菜單項就可以将目前文檔格式化成ansi風格。如果你想要其它風格,可以自行設定參數。
值得注意的是在低版本的Visual Studio中,預設設定運作外部程式不會儲存目前文檔。這樣的話如果在未儲存的情況下運作該指令,未儲存部分将會丢失。這個可以通過設定一個選項來解決。Visual Studio 6.0中:Options -> Editor -> Save Options -> Save before running tools 将該項勾選即可。我已經驗證,在Visual Studio 2005中不用擔心這類問題,可以放心使用。但是作為一個好習慣,我仍然建議你随時儲存你的工作,尤其是做這種大幅度改動之前,甚至應該對源代碼進行Check in操作。不知道Check in是什麼?沒關系,過幾天我還會寫一篇關于代碼控制的文章,應該可以解決你的疑惑。
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) 單個檔案--更改縮進2個空格
astyle --style=ansi --indent=spaces=2 Form1.cs
預設縮進一個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. 其他比較有用的開關:
(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替換為空格。
下面再介紹第二項獨門絕技:批量格式化!
有時候你會有很多檔案需要格式化成統一風格,難道一個個點選菜單?不!那樣太累了。
在Windows中,我們可以用指令行來解決問題。這裡用到一個超級指令 for
我來寫個範例,大家就知道該怎麼處理了。
for /R %f in (*.cpp;*.c;*.h) do astyle --style=ansi "%f"
該指令在目前目錄中尋找檔案名比對模式 *.cpp;*.c;*.h 的所有檔案(不同模式可用英文逗号隔開),并且對每個檔案%f執行操作:
astyle --style=ansi "%f"
好了,本教程可以結束了。希望對你有所幫助。
下面是标準的程式文檔,如果你想了解更多用法,可以一讀;如果你隻是像我一樣日常使用該工具,就可以不看了。
Artistic Style 1.15.3 (http://www.bigfoot.com/~davidsont/astyle)
(created by Tal Davidson, [email protected])
Modified edition by Qiongzhu Wan, 2004.09
Usage : astyle [options] < original > Beautified
astyle [options] Foo.cpp Bar.cpp [...]
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# 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 Styling options:
--------------------
--style=ansi
ANSI style formatting/indenting.
--style=kr
Kernighan&Ritchie style formatting/indenting.
--style=gnu
GNU style formatting/indenting.
--style=java
Java mode, with standard java style formatting/indenting.
--style=linux
Linux mode (i.e. 8 spaces per indent, break definition-block
brackets but attach command-block brackets.
Indentation options:
--------------------
-c or --mode=c
Indent a C, C++ or C# source file (default)
-j or --mode=java
Indent a Java(TM) source file
-s or -s# or --indent=spaces=#
Indent using # spaces per indent. Not specifying #
will result in a default of 4 spacec per indent.
-t or -t# or --indent=tab=#
Indent using tab characters, assuming that each
tab is # spaces long. Not specifying # will result
in a default assumption of 4 spaces per tab.
-T# or --force-indent=tab=# Indent using tab characters, assuming tha
t each
tab is # spaces long. Force tabs to be used in areas
Astyle would prefer to use spaces.
-C or --indent-classes
Indent 'class' blocks, so that the inner 'public:',
'protected:' and 'private: headers are indented in
relation to the class block.
-S or --indent-switches
Indent 'switch' blocks, so that the inner 'case XXX:'
headers are indented in relation to the switch block.
-K or --indent-cases
Indent 'case XXX:' lines, so that they are flush with
their bodies..
-N or --indent-namespaces
Indent the contents of namespace blocks.
-B or --indent-brackets
Add extra indentation to '{' and '}' block brackets.
-G or --indent-blocks
Add extra indentation entire blocks (including brackets).
-L or --indent-labels
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).
-m# or --min-conditional-indent=#
Indent a minimal # spaces in a continuous conditional
belonging to a conditional header.
-M# or --max-instatement-indent=#
Indent a maximal # spaces in a continuous statement,
relatively to the previous line.
-E or --fill-empty-lines
Fill empty lines with the white space of their
previous lines.
--indent-preprocessor
Indent multi-line #define statements
Formatting options:
-------------------
-b or --brackets=break
Break brackets from pre-block code (i.e. ANSI C/C++ style).
-a or --brackets=attach
Attach brackets to pre-block code (i.e. Java/K&R style).
-l or --brackets=linux
Break definition-block brackets and attach command-block
brackets.
--brackets=break-closing-headers
Break brackets before closing headers (e.g. 'else', 'catch', ..).
Should be appended to --brackets=attach or --brackets=linux.
-o or --one-line=keep-statements
Don't break lines containing multiple statements into
multiple single-statement lines.
-O or --one-line=keep-blocks
Don't break blocks residing completely on one line
-p or --pad=oper
Insert space paddings around operators only.
--pad=paren
Insert space paddings around parenthesies only.
-P or --pad=all
Insert space paddings around operators AND parenthesies.
--convert-tabs
Convert tabs to spaces.
--break-blocks
Insert empty lines around unrelated blocks, labels, classes, ...
--break-blocks=all
Like --break-blocks, except also insert empty lines
around closing headers (e.g. 'else', 'catch', ...).
--break-elseifs
Break 'else if()' statements into two different lines.
Other options:
-------------
--suffix=####
Append the suffix #### instead of '.orig' to original filename.
-X or --errors-to-standard-output
Print errors and help information to standard-output rather than
to standard-error.
-v or --version
Print version number
-h or -? or --help
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
HOMEPATH environment variable ( i.e. %HOMEPATH%\.astylerc ).
If a default options file is found, the options in this file
will be parsed BEFORE the command-line options.
Options within the default option file may be written without
the preliminary '-' or '--'.
版權聲明:本文為CSDN部落客「weixin_33973609」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/weixin_33973609/article/details/91909873