Astyle使用方法

下载地址: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






  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
包含源代码,需要的可以自行下载。 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径 VC6++ 设置方法 菜单->工具->定制->工具菜单内容->新建菜单,参数如下 命令行:astyle.exe 变量: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(FileName)$(FileExt) 初始目录: $(FileDir) VC2008 外部工具里设置,还可以添加快捷键 命令:astyle.exe 参数: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(ItemFileName)$(ItemExt) 初始目录: $(ItemDir) CodeBlocks 设置差不多,很多绿色版的已经设置好了 AStyle_2.02_windows.zip bin 目录里有官方编译好的 Artistic Style 2.01 Maintained by: Jim Pattee Original Author: Tal Davidson Usage : astyle [options] Source1.cpp Source2.cpp [...] astyle [options] 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. Wildcards (* and ?) may be used in the filename. A 'recursive' option can process directories recursively. By default, astyle is set up to indent C/C++/C#/Java files, with four spaces per indent, a maximal indentation of 40 spaces inside continuous statements, a minimum indentation of eight spaces inside conditional statements, and NO formatting options.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值