文章目录
- AStyle 介绍
- Ubuntu 下安装
- Windows 下安装
AStyle 介绍
AStyle,全名 Artistic Style,是一款源代码格式化工具。它可以自动格式化 C
,C++
,C#
,和Java
源代码。使用它您可以轻松地对代码进行格式化,以满足您需要的代码风格。它提供了大量的选项,可以非常细致地控制代码的格式化方式。并且,它是一个开放源码的工具,所以您可以自由地使用和修改它。
AStyle的主要功能包括:
- 缩进风格的控制,包括K&R风格,Allman风格,GNU风格,和自定义风格。
- 缩进宽度的控制,可以设置为使用空格或Tab,也可以设置Tab的宽度。
- 代码块大括号的位置控制。
- 在运算符两侧添加或删除空格。
- 控制if,for,while等语句的括号是否需要在新的一行开始。
- 一键格式化整个源文件或选定的代码块。
以下是使用AStyle的一个例子:代码格式化前:
int main(){ printf("Hello, World!"); return 0; }
使用AStyle格式化后(K&R风格,4个空格缩进):
int main()
{
printf("Hello, World!");
return 0;
}
命令行示例:
astyle --style=kr --indent=spaces=4 -f myfile.c
这条命令会将 myfile.c
中的代码格式化为K&R
风格,并且使用4
个空格进行缩进。
Ubuntu 下安装
- 使用 apt-get 命令安装
sudo apt-get update -y
sudo apt-get install -y astyle
- 源码编译安装
git clone https://github.com/steinwurf/astyle.git
cd astyle
python waf configure --cxx_mkspec=cxx_default
python waf build
编译好之后将 bulid/cxx_default/astyle 放到 bin 目录下即可使用了:
(*^~^*) ~/astyle/build/cxx_default> cp astyle ~/.local/bin/
(*^~^*) ~/astyle/build/cxx_default> astyle -h
Artistic Style 2.06 beta
Maintained by: Jim Pattee
Original Author: Tal Davidson
Usage:
------
astyle [OPTIONS] File1 File2 File3 [...]
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 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.
Options:
--------
This program follows the usual GNU command line syntax.
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.
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 '--'.
Disable Formatting:
----------------------
Disable Block
Blocks of code can be disabled with the comment tags *INDENT-OFF*
and *INDENT-ON*. It must be contained in a one-line comment.
Disable Line
Padding of operators can be disabled on a single line using the
comment tag *NOPAD*. It must be contained in a line-end comment.
Bracket Style Options:
----------------------
default bracket style
If no bracket style is requested, the opening brackets will not be
changed and closing brackets will be broken from the preceding line.
--style=allman OR --style=bsd OR --style=break OR -A1
Allman style formatting/indenting.
Broken brackets.
--style=java OR --style=attach OR -A2
Java style formatting/indenting.
Attached brackets.
--style=kr OR --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
VTK style formatting/indenting.
Broken, indented brackets, except for opening brackets.
--style=banner OR -A6
Banner style formatting/indenting.
Attached, indented brackets.
--style=gnu OR -A7
GNU style formatting/indenting.
Broken brackets, indented blocks.
--style=linux OR --style=knf OR -A8
Linux style formatting/indenting.
Linux brackets, minimum conditional indent is one-half indent.
参考:https://iq.opengenus.org/install-astyle-in-ubuntu/
Windows 下安装
见:https://sourceforge.net/projects/astyle/