astyle使用基础教程

astyle使用基础教程

转自:html

http://babybandf.blog.163.com/blog/static/61993532010112205811797/java


astyle是一个我本身经常使用的开放源码工具。它能够方便的将程序代码格式化成本身想要的样式而没必要人工修改。原本嘛,做为高等生物应该优先去作一些智慧的事情,而不是把时间消耗在机器能够完美完成的事情上。linux

想要马上开始?请先去主页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  [...]
app

例如:less

          astyle --style=ansi foo.cppide

上面的命令将美化foo.cpp文件,更改其风格为ANSI,并将原始文件备份到foo.cpp.orgin。因此,你能够安全的使用该软件而没必要担忧会将代码改得没法回头。工具

具体的来讲,astyle包含了如下几种预约义风格,只需在参数中简单指定便可使用:post

  --style=ansi:ANSI 风格格式和缩进this

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, 
davidsont@bigfoot.com)

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 '--'.

相关文章
相关标签/搜索