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"

好了,本教程可以结束了。希望对你有所帮助。

下面是标准的程序文档,如果你想了解更多用法,可以一读;如果你只是像我一样日常使用该工具,就可以不看了。

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 theoriginal file-name. The original pre-indented file is renamed, with asuffix of ".orig" added to the original filename.

By default, astyle is set up to indent C/C++/C# files, with 4 spaces perindent, 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 that 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 '--'.

qt 使用插件astyle_astyle使用基础教程相关推荐

  1. Qt Creator下载和安装(详细教程)以及如何发布可执行程序

    简介 Qt是跨平台的图形开发库,目前由Digia全资子公司 Qt Company 独立运营,官方网址:  http://www.qt.io/  也可以访问Qt项目域名:http://qt-projec ...

  2. python3廖雪峰云-python3基础教程廖雪峰云_Python GUI库大汇总

    Python GUI库大汇总 所有程序都是基于命令行的,这序可能只有一些"专的计算机人士才会使用.例如前面编写的五等程序,恐怕只有程序员自己才愿意玩这么"糟糕"的游戏,很 ...

  3. Qt Creator下载和安装(详细教程)

    简介 Qt是跨平台的图形开发库,目前由Digia全资子公司 Qt Company 独立运营,官方网址: Qt | Cross-platform software development for emb ...

  4. 在 Ubuntu Linux 上从源代码构建 Qt 6.2.2的简短教程

    Qt 6.2.0 是 Qt 6 系列的第3个版本,功能与 Qt 5 相当,因此有望成为第一个被新 Qt 项目或从 Qt 5 移植的项目广泛采用的版本.在这篇文章中我提供有关在 Ubuntu Linux ...

  5. OpenVAS漏洞扫描基础教程之连接OpenVAS服务

    OpenVAS漏洞扫描基础教程之连接OpenVAS服务 连接OpenVAS服务 当用户将OpenVAS工具安装并配置完后,用户即可使用不同的客户端连接该服务器.然后,对目标主机实施漏洞扫描.在本教程中 ...

  6. Spring Boot 2.x基础教程:使用国产数据库连接池Druid

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | http://blog.di ...

  7. Spring Boot 2.x基础教程:Swagger静态API文档的生成

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/spring-boot-learni ...

  8. Spring Cloud Alibaba基础教程:Sentinel Dashboard同步Apollo存储规则

    点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 在之前的两篇教程中我们分别介绍了如何将Sentinel的限流规则存储到Nacos和Apollo中.同时 ...

  9. Web前端-JavaScript基础教程上

    Web前端-JavaScript基础教程 将放入菜单栏中,便于阅读! JavaScript是web前端开发的编程语言,大多数网站都使用到了JavaScript,所以我们要进行学习,JavaScript ...

最新文章

  1. python魔术方法call_php魔术方法__call
  2. 深入浅出HTTPS基本原理
  3. 天气webservice服务
  4. 伪指令 .align 的含义
  5. Persistent Memory编程简介
  6. 组播IP地址到底是谁的IP?
  7. C语言上证指数运行源码,个股对比上证指数公式源码
  8. PHP gd库 验证码
  9. conv2d的输入_pytorch1.0中torch.nn.Conv2d用法详解
  10. matlab机械臂工作空间代码_ROS中机械臂笛卡尔空间规划姿态求解无效-Moveit!
  11. 鼠标右键添加新建类型
  12. OpenGL基础28:模型
  13. python机器学习案例系列教程——优化,寻找使成本函数最小的最优解
  14. VS2017内使用Coin3d第三方库的配置方法(含库与案例)
  15. SIFT与SURF算法
  16. Python打开系统资源管理器并选中文件
  17. Centos7.2+Coturn+SignalMaster 搭建WebRTC进行H5直播
  18. ES2015 class
  19. 什么叫ocpm、ocpc、ocpa?三种转化出价方式
  20. golang操作mongo

热门文章

  1. java输入输出及文件_(java基础)Java输入输出流及文件相关
  2. 理一理字节对齐的那些事
  3. Spring Cloud 一:注册中心
  4. 经典的 div + css 鼠标 hover 下拉菜单
  5. js判断设备、浏览器类型
  6. 161227、js显示对象所有属性和方法的函数
  7. Linux软件间的依赖关系(转)
  8. 【深度好文】ICLR 2022 | cosFormer:重新思考注意力机制中的Softmax
  9. 丰厚奖学金博士招生 | 澳大利亚OPTIMA 招募博士,多光谱时间序列数据的时空目标检测/分割方向...
  10. 中科院副研究员高林:面向可视媒体分析与合成的深度几何学习方法分享