转自<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;}
然后在终端下输入一下指令:
- 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 '--'.
相关推荐
Astyle 格式化代码 Astyle 是一种格式化代码工具,能够对代码进行格式化、美化和调整,以提高代码的可读性和维护性。Astyle 支持多种编程语言,包括 C、C++、Java、Python 等,并提供了多种格式化风格供用户选择。 ...
8. **版本控制友好**:Astyle格式化后的代码改动会被视为文本编辑,不会导致大量的不可比较的二进制差异,这对于使用版本控制系统(如Git、SVN)的团队来说是非常友好的。 9. **错误检测**:Astyle在格式化过程中还...
3. 格式化代码:运行AStyle命令,并指定要格式化的源代码文件或整个目录。AStyle会创建一个备份文件,然后对原始文件进行格式化,以避免意外覆盖未保存的更改。 4. 检查结果:格式化完成后,你应该检查新格式化的...
1、Qt中格式化代码工具有好几种,目前使用的最多最好用的就是Astyle 2、Windows平台下:可以直接下载带有Astyle.exe文件的压缩包,放到指定的目录即可 3、单击上面一行的“工具——选项——Beautifier”进入面板,在...
它能够帮助程序员自动整理和格式化代码,使代码风格统一,提高代码可读性和团队协作效率。对于使用Visual Studio 2010的开发者来说,AStyle是一个不可或缺的辅助工具。 **1. 安装与配置AStyle** 首先,你需要下载...
此程序源码已经丢失,BUG在于如果程序所处路径包含中文,就会死掉。...本儿偶然使用AStyle来格式化代码,但是命令行不方便,就用delphi随便写了个个GUI外壳,这个外壳也能保存你的多种设置,方便选择使用。
它能够自动格式化代码,包括缩进、空格、括号对齐以及遵循不同的编码风格,如K&R、Allman、GNU等。 2. **代码格式化**: 代码格式化是将混乱或不一致的代码转换为整洁、统一格式的过程,有助于提高代码的可读性和...
其中,AStyle(Artistic Style)是一款流行的开源代码格式化工具,它能够自动对C、C++、C++/CLI、Objective-C和Java等语言的源代码进行格式化。AStyle插件可以集成到Keil中,使得在Keil环境下直接对代码进行美化变得...
9. **命令行与GUI界面**:除了作为插件集成到Source Insight中,AStyle还提供命令行工具,方便在脚本或构建系统中自动化代码格式化。 10. **自定义配置**:用户可以通过配置文件定制AStyle的行为,以满足特定的格式...
**AStyle代码格式化插件详解** AStyle(Artistic Style)是一款强大的源代码格式化工具,专为C、C++、C++/CLI、Objective-C、C#和Java等编程语言设计。它旨在帮助程序员统一代码风格,提高代码可读性和维护性,尤其...
大名鼎鼎的Astyle格式化代码插件,用来对C/C++、java等代码进行格式化,用在sourceinsight中最好不过了。附件里面描述了基于最新的Source Insight 4.0代码格式化设置,供大家参考。
3. **自动化构建**:通过在构建脚本中集成AStyle,可以在构建过程中自动格式化代码,保持代码库的一致性。 4. **学习与教育**:对于初学者,AStyle是一个很好的工具,可以帮助他们理解和遵循良好的编码规范。 总的...
3. **处理注释**:AStyle在格式化代码的同时,会保持原有注释的位置和格式,避免格式化过程中破坏注释的完整性。 4. **处理嵌套结构**:对于复杂的嵌套结构,如循环和条件语句,AStyle能确保它们的缩进正确,使代码...
1. **AStyle.exe**:这是Astyle工具的可执行文件,用户可以通过运行这个程序来格式化代码。只需将待格式化的源代码文件拖放到AStyle.exe上,或者在命令行中指定文件路径,Astyle就会根据预设的规则对代码进行美化。...
**Astyle代码格式化工具详解** Astyle,全称Artistic Style,是一款强大的源代码格式化、美化工具,尤其在C、C++以及Objective-C等编程语言中广泛应用。这款开源工具旨在提供统一的代码风格,使得代码更具可读性和...
**AStyle:代码格式化与自动对齐工具** AStyle,全称为Artistic Style,是一款开源的源代码格式化、美化工具,适用于C、C++、C++/CLI、Objective-C、C#以及Fortran等编程语言。它旨在提供一种标准化的代码格式,...
使用Astyle格式化代码可以提高代码的可读性和维护性。例如,如果单行语句没有添加花括号,Astyle可以在格式化后添加花括号。格式化风格可以在配置文件中调整。 Astyle的优点 ------------- Astyle具有以下优点: ...
本文将深入探讨代码格式化工具,特别是`AStyle`这款广泛应用的代码美化工具。 首先,我们需要理解什么是代码格式化。代码格式化是指将混乱或不规范的源代码转换为遵循特定编程规范的整洁格式的过程。这包括缩进、...
提供的"AStyle_2.04_windows(格式化代码).zip"文件包含了适用于Windows操作系统的Astyle版本。解压后,你可以根据文档"Astyle使用方法.docx"了解如何配置和使用Astyle。 在Source Insight中,你可以创建一个...