文档章节

astyle格式化代码

mskk
 mskk
发布于 2017/05/04 21:20
字数 1581
阅读 41
收藏 0
点赞 0
评论 0

转自<http://blog.chinaunix.net/uid-25885064-id-3363736.html>

astyle是一个开源工具,它可以方便的将代码格式化成自己想要的样式而不必人工修改。可以到网站(http://astyle.sourceforge.net/)上下载源码自己编译,也可以使用指令(apt-get install astyle)安装。


下面介绍一下astyle的简单使用。例如有以下的源码:

#include <stdio.h>
    int main()
    {int i;printf("Just a test!\n");for(i=0;i<10;++i)printf("%d\n",i);}return 0;}

 然后在终端下输入一下指令:

  1. astyle test1.c

效果如下:

#include <stdio.h>
    int main()
    {
        int i;
        printf("Just a test!\n");
        for(i=0; i<10; ++i) {
            printf("%d\n",i);
        }
        return 0;
    }

 

当然也可以加上一些选项,例如“astyle --style=bsd test1.c”,“ astyle --style=gnu test1.c”等等。

 

 

vim中的命令模式下,可以使用下面的某一种方式来格式化代码。

%!astyle (simple case - astyle default mode is C/C++)

 或者

%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)

 

或者

%!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)
     

 

为方便使用,可以把它写成一个脚本,代码如下:

#! /bin/bash
    for f in $(find . -name '*.c' -or -name '*.cpp' -type f)
    do
        astyle $f
    done

 

在格式化完代码后,会生成一个后缀为orig的文件,将脚本更改如下:

#! /bin/bash
    for f in $(find . -name '*.c' -or -name '*.cpp' -or -name '*.h' -type f)
    do
         astyle $f
    done
    # after formate the code,we need to rm '*.orig' files
    for f in $(find . -name '*.orig' -type f)
    do
         rm $f
    done

 

    astyle其使用说明如下:

Usage : astyle [options] Source1.cpp Source2.cpp [...]
    astyle [options] < Original > 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.
    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 Style Options:
    -------------------------
    --style=allman OR --style=ansi OR --style=bsd OR -A1
    Allman style formatting/indenting.
    Broken brackets.
    --style=java OR -A2
    Java style formatting/indenting.
    Attached brackets.
    --style=k&r OR --style=k/r OR -A3
    Kernighan & Ritchie style formatting/indenting.
    Linux brackets.
    --style=stroustrup OR -A4
    Stroustrup style formatting/indenting.
    Stroustrup brackets.
    --style=whitesmith OR -A5
    Whitesmith style formatting/indenting.
    Broken, indented brackets.
    Indented class blocks and switch blocks.
    --style=banner OR -A6
    Banner style formatting/indenting.
    Attached, indented brackets.
    Indented class blocks and switch blocks.
    --style=gnu OR -A7
    GNU style formatting/indenting.
    Broken brackets, indented blocks, indent is 2 spaces.
    --style=linux OR -A8
    GNU style formatting/indenting.
    Linux brackets, indent is 8 spaces.
    --style=horstmann OR -A9
    Horstmann style formatting/indenting.
    Horstmann brackets, indented switches, indent is 3 spaces.
    --style=1tbs OR --style=otbs OR -A10
    One True Brace Style formatting/indenting.
    Linux brackets, add brackets to all conditionals.
    Tab and Bracket Options:
    ------------------------
    default indent option
    If no indentation option is set,
    the default option of 4 spaces will be used.
    --indent=spaces=# OR -s#
    Indent using # spaces per indent. Not specifying #
    will result in a default of 4 spaces per indent.
    --indent=tab OR --indent=tab=# OR -t OR -t#
    Indent using tab characters, assuming that each
    tab is # spaces long. Not specifying # will result
    in a default assumption of 4 spaces per tab.
    --indent=force-tab=# OR -T#
    Indent using tab characters, assuming that each
    tab is # spaces long. Force tabs to be used in areas
    Astyle would prefer to use spaces.
    default brackets option
    If no brackets option is set,
    the brackets will not be changed.
    --brackets=break OR -b
    Break brackets from pre-block code (i.e. ANSI C/C++ style).
    --brackets=attach OR -a
    Attach brackets to pre-block code (i.e. Java/K&R style).
    --brackets=linux OR -l
    Break definition-block brackets and attach command-block
    brackets.
    --brackets=stroustrup OR -u
    Attach all brackets except function definition brackets.
    --brackets=horstmann OR -g
    Break brackets from pre-block code, but allow following
    run-in statements on the same line as an opening bracket.
    Indentation options:
    --------------------
    --indent-classes OR -C
    Indent 'class' blocks, so that the inner 'public:',
    'protected:' and 'private: headers are indented in
    relation to the class block.
    --indent-switches OR -S
    Indent 'switch' blocks, so that the inner 'case XXX:'
    headers are indented in relation to the switch block.
    --indent-cases OR -K
    Indent case blocks from the 'case XXX:' headers.
    Case statements not enclosed in blocks are NOT indented.
    --indent-brackets OR -B
    Add extra indentation to '{' and '}' block brackets.
    --indent-blocks OR -G
    Add extra indentation entire blocks (including brackets).
    --indent-namespaces OR -N
    Indent the contents of namespace blocks.
    --indent-labels OR -L
    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).
    --indent-preprocessor OR -w
    Indent multi-line #define statements.
    --indent-col1-comments OR -Y
    Indent line comments that start in column one.
    --max-instatement-indent=# OR -M#
    Indent a maximal # spaces in a continuous statement,
    relative to the previous line.
    --min-conditional-indent=# OR -m#
    Indent a minimal # spaces in a continuous conditional
    belonging to a conditional header.
    Padding options:
    --------------------
    --break-blocks OR -f
    Insert empty lines around unrelated blocks, labels, classes, ...
    --break-blocks=all OR -F
    Like --break-blocks, except also insert empty lines
    around closing headers (e.g. 'else', 'catch', ...).
    --pad-oper OR -p
    Insert space paddings around operators.
    --pad-paren OR -P
    Insert space padding around parenthesis on both the outside
    and the inside.
    --pad-paren-out OR -d
    Insert space padding around parenthesis on the outside only.
    --pad-paren-in OR -D
    Insert space padding around parenthesis on the inside only.
    --pad-header OR -H
    Insert space padding after paren headers (e.g. 'if', 'for'...).
    --unpad-paren OR -U
    Remove unnecessary space padding around parenthesis. This
    can be used in combination with the 'pad' options above.
    --delete-empty-lines OR -x
    Delete empty lines within a function or method.
    It will NOT delete lines added by the break-blocks options.
    --fill-empty-lines OR -E
    Fill empty lines with the white space of their
    previous lines.
    Formatting options:
    -------------------
    --break-closing-brackets OR -y
    Break brackets before closing headers (e.g. 'else', 'catch', ...).
    Use with --brackets=attach, --brackets=linux,
    or --brackets=stroustrup.
    --break-elseifs OR -e
    Break 'else if()' statements into two different lines.
    --add-brackets OR -j
    Add brackets to unbracketed one line conditional statements.
    --add-one-line-brackets OR -J
    Add one line brackets to unbracketed one line conditional
    statements.
    --keep-one-line-blocks OR -O
    Don't break blocks residing completely on one line.
    --keep-one-line-statements OR -o
    Don't break lines containing multiple statements into
    multiple single-statement lines.
    --convert-tabs OR -c
    Convert tabs to the appropriate number of spaces.
    --align-pointer=type OR -k1
    --align-pointer=middle OR -k2
    --align-pointer=name OR -k3
    Attach a pointer or reference operator (* or &) to either
    the operator type (left), middle, or operator name (right).
    --mode=c
    Indent a C or C++ source file (this is the default).
    --mode=java
    Indent a Java source file.
    --mode=cs
    Indent a C# source file.
    Other options:
    --------------
    --suffix=####
    Append the suffix #### instead of '.orig' to original filename.
    --suffix=none OR -n
    Do not retain a backup of the original file.
    --options=####
    Specify an options file #### to read and use.
    --options=none
    Disable the default options file.
    Only the command-line parameters will be used.
    --recursive OR -r OR -R
    Process subdirectories recursively.
    --exclude=####
    Specify a file or directory #### to be excluded from processing.
    --errors-to-stdout OR -X
    Print errors and help information to standard-output rather than
    to standard-error.
    --preserve-date OR -Z
    The date and time modified will not be changed in the formatted file.
    --verbose OR -v
    Verbose mode. Extra informational messages will be displayed.
    --formatted OR -Q
    Formatted display mode. Display only the files that have been formatted.
    --quiet OR -q
    Quiet mode. Suppress all output except error messages.
    --lineend=windows OR -z1
    --lineend=linux OR -z2
    --lineend=macold OR -z3
    Force use of the specified line end style. Valid options
    are windows (CRLF), linux (LF), and macold (CR).
    --version OR -V
    Print version number.
    --help OR -h OR -?
    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
    USERPROFILE environment variable ( i.e. %USERPROFILE%\astylerc ).
    If a default options file is found, the options in this file
    will be parsed BEFORE the command-line options.
    Long options within the default option file may be written without
    the preliminary '--'.

 

本文转载自:http://gaylord.iteye.com/blog/2090616

共有 人打赏支持
mskk
粉丝 2
博文 148
码字总数 3099
作品 0
宝山
程序员
qiuchangjie/CodeFormatter

#CodeFormatter 插件作用 个人因为无法习惯各种代码风格,因此写了个插件格式化vim当前编辑的代码文件(当前转换的代码风格是个人习惯的风格,如不习惯可自行修改,参照astyle工具的参数说明...

qiuchangjie
2016/04/08
0
0
如何自动调整代码格式 - vim /Visual Studio/ Source Insight...

ArtisticStyle----很好用的C/C++样式格式化工具(支持VC6) 下载地址: http://srgb.googlecode.com/files/AStyle2.02windows.7z 把astyle.exe 复制到 C:WINDOWS 目录里,省的指定路径 VC6++ ...

AlphaJay
2012/06/21
0
0
在EditPlus中使用代码格式化插件

最近在用EditPlus学习java,本来是想学习java代码的书写格式,比如for关键字和其后的左小括号之间的空格问题,但是我发现这不是十分必要,因为EditPlus已经有了格式化代码插件。 1、下载Epi...

天蚕宝衣
2016/04/14
270
0
SourceFormatX 2.56 代码整理软件 求搞掂?

因公司现在的整个源代码很混乱,自己想把它整理好,以便日后的维护和修改。尝试了很多种代码格式化的软件(phpcb,astyle),只有这个才比较合适,但是不能保存,也不能格式化整个网站目录。...

wankaiming
2011/06/28
226
3
Qt Creator 代码格式美化插件--Qtc_AStyle

Qtc_AStyle是一个Qt Creator IDE的插件,使用了Artistic Style V2.04的静态库,IDE加载这个插件后,在选项中可以很直观地定义自己的代码风格,然后一键对整个项目所有的源文件进行代码格式美...

zlqzlq
2017/05/26
209
0
sublime Text 3 做c编辑器

基础设置 首先是常用插件安装: 然后如果点开中文名称的文件,标签上显示中文名称乱码的,需要如此设置 然后对中文支持的需要安装 ConvertToUTF8 然后是基础的配置,点开 Preferences -> Setting...

新年
2016/03/16
133
0
语法着色工具--Highlight

Highlight 用于将源码输出为语法高亮的格式文件: 支持输出为HTML, XHTML, RTF, ODT, TeX, LaTeX, SVG 和 BBCode格式 支持180种编程语言 包含80种着色风格 语法定义方式为正则表达式或文本列...

匿名
2008/09/19
11.6K
0
EditPlus 添加格式化java代码功能

下载AStyle:http://sourceforge.net/projects/astyle/ 在Editplus 配置用户工具: 实例: astyle --style=java d:srcTest.java 语法: astyle [options] SourceFile1 SourceFile2 SourceFil......

LiShixi
2012/06/05
0
0
VIM使用系列之一——配置VIM下C/C++编程环境

作者:gnuhpc from http://blog.csdn.net/gnuhpc 本文环境:ubuntu 9.10 1.基本配置 建立三个目录:mkdir ~/.vim/{plugin,doc,syntax} –p 建立配置文件:touch ~/.vimrc vimrc作为vim的主要...

brucema
2011/10/14
0
0
一键将VIM变换成强大的IDE]

打开终端,执行下面的命令就自动安装好了: wget https://raw.github.com/ma6174/vim/master/setup.sh -O ma6174vimsetup.sh && bash ma6174vimsetup.sh 等候片刻,不用输入什么,自己调回命...

cbliu
2016/01/12
94
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

java集合元素的默认大小

当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使...

竹叶青出于蓝
6分钟前
1
0
Java快速开发平台,JEECG 3.7.7闪电版本发布,增加多套主流UI代码生成器模板

JEECG 3.7.7 闪电版本发布,提供5套主流UI代码生成器模板 导读 ⊙平台性能优化,速度闪电般提升 ⊙提供5套新的主流UI代码生成器模板(Bootstrap表单+BootstrapTable列表\ ElementUI列表表单)...

Jeecg
9分钟前
0
0
export 和 module.export 的区别

在浏览器端 js 里面,为了解决各模块变量冲突等问题,往往借助于 js 的闭包把左右模块相关的代码都包装在一个匿名函数里。而 Nodejs 编写模块相当的自由,开发者只需要关注 require,exports,...

孟飞阳
11分钟前
0
0
技术教育的兴起

技术教育的兴起 作者: 阮一峰 1、 有一年,我在台湾环岛旅行。 花莲的海边,我遇到一对台湾青年夫妻,带着女儿在海滩上玩。我们聊了起来。 当时,我还在高校当老师。他们问我,是否觉得台湾...

吕伯文
11分钟前
0
0
Linux服务器下的HTTP抓包分析

说到抓包分析,最简单的办法莫过于在客户端直接安装一个Wireshark或者Fiddler了,但是有时候由于客户端开发人员(可能是第三方)知识欠缺或者其它一些原因,无法顺利的在客户端进行抓包分析,...

mylxsw
16分钟前
0
0
mybatis3-javaapi

sqlSessionFactoryBuilder->sqlSessionFactory->sqlSession<-rowbound<-resultHandler myBatis uses a Java enumeration wrapper for transaction isolation levels, called TransactionIsol......

writeademo
19分钟前
0
0
Java NIO:浅析I/O模型

也许很多朋友在学习NIO的时候都会感觉有点吃力,对里面的很多概念都感觉不是那么明朗。在进入Java NIO编程之前,我们今天先来讨论一些比较基础的知识:I/O模型。下面本文先从同步和异步的概念...

yzbty23
20分钟前
0
0
了解iOS消息推送一文就够:史上最全iOS Push技术详解

本文作者:陈裕发, 腾讯系统测试工程师,由腾讯WeTest整理发表。 1、引言 开发iOS系统中的Push推送,通常有以下3种情况: 1)在线Push:比如QQ、微信等IM界面处于前台时,聊天消息和指令都会...

JackJiang-
21分钟前
0
0
Mysql汉子转拼音

update t_app_city SET CITY_NAME_BEGIN = ELT(INTERVAL(CONV(HEX(LEFT(CONVERT(CITY_NAME USING gbk),1)),16,10), 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7, 0xBFA......

尘叙缘
23分钟前
0
0
大数据构建智慧城市“新引擎”,加速推进新旧动能转换

——“大数据与智慧城市”技术交流分享会——济南站召开 7月13日,“大数据携手智慧城市,助力山东新旧动能转换”技术交流分享会——济南站在山东信息通信技术研究院会议室成功举办,此次会议...

左手的倒影
25分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部