astyle是一个代码格式化工具,为C,C++,JAVA等语言提供代码格式化功能。

官方描述为:Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages.

相关页面:官方地址文档地址下载页面(当前最新版本位3.1)。

一、编译安装

安装可参考官方安装文档,提供了多种方式编译安装,这里使用cmake

# 解压
tar -zxvf astyle_3.1_linux.tar.gz
cd astyle
# 创建编译文件夹
mkdir  as-gcc-exe
cd  as-gcc-exe
cmake  ..
# 编译和安装
make
make install

默认情况下,可执行文件被安装在/usr/bin,文档放在/usr/share/doc/astyle,安装完成后直接就可使用astyle命令。

官方并没有提供make uninstall卸载的功能,如需卸载需要在编译目录下执行xargs rm < install_manifest.txt

二、astyle用法

2.1 基本用法

详细用法可参考官方文档地址,astyle最简单的用法就是通过--style选项指定要格式化的风格,然后就会对代码进行格式化。

$ astyle test.cpp --style=linux
Formatted  /data/code/c/0-test/test.cpp
$ ls
test.cpp  test.cpp.orig # 生成一个.orig作为源代码备份

常见的style选项有:ansi kr linux gnu java等,更多可以查看官方文档Brace Style Options

如需指定输入输出可通过设置--stdin--stdout选项:

$ astyle --stdin=test.cpp --stdout=linux.cpp --style=linux 

2.2 生成各种风格样例

准备一个源文件test.cpp,内容如下:

#include<iostream>
using namespace std;

void f(int i) {
    if (i == 1) {
        cout << "helloworld" << endl;
    } else {
        cout << "HELLOWORLD" << endl;
    }
}

int main() {
    cout << "HelloWorld" << endl;
    return 0;
}

编写一个shell脚本分别输出不同格式:

$ #! /bin/bash

sts=("bsd" "allman" "ansi" "kr" "linux" "gnu" "java")

for i in ${sts[@]}
do
    astyle --stdin=test.cpp --stdout=$i.cpp --style=$i 
done

运行脚本将会生成不同格式的代码文件:

$ ls -al
total 44
drwxrwxr-x 2 ma ma 4096 May  4 17:20 .
drwxrwxr-x 9 ma ma 4096 May  4 17:20 ..
-rw-rw-r-- 1 ma ma  248 May  4 17:20 allman.cpp
-rw-rw-r-- 1 ma ma  248 May  4 17:20 ansi.cpp
-rw-rw-r-- 1 ma ma  248 May  4 17:20 bsd.cpp
-rw-rw-r-- 1 ma ma  272 May  4 17:20 gnu.cpp
-rw-rw-r-- 1 ma ma  236 May  4 17:20 java.cpp
-rw-rw-r-- 1 ma ma  236 May  4 17:20 kr.cpp
-rw-rw-r-- 1 ma ma  236 May  4 17:20 linux.cpp
-rwxrwxr-x 1 ma ma  152 May  4 17:19 run.sh
-rw-rw-r-- 1 ma ma  236 May  4 17:13 test.cpp

三、不同风格的特点

3.1 linux风格

特点:函数的括号上下对齐,条件控制的括号没有对齐。

#include<iostream>
using namespace std;

void f(int i)
{
    if (i == 1) {
        cout << "helloworld" << endl;
    } else {
        cout << "HELLOWORLD" << endl;
    }
}

int main()
{
    cout << "HelloWorld" << endl;
    return 0;
}

3.2 ansi风格

特点:所有的括号都一一对齐。

#include<iostream>
using namespace std;

void f(int i)
{
    if (i == 1)
    {
        cout << "helloworld" << endl;
    }
    else
    {
        cout << "HELLOWORLD" << endl;
    }
}

int main()
{
    cout << "HelloWorld" << endl;
    return 0;
}

3.3 gnu风格

特点:括号一一对齐,条件控制语句的括号会增加一个缩进。

void f(int i)
{
    if (i == 1)
        {
            cout << "helloworld" << endl;
        }
    else
        {
            cout << "HELLOWORLD" << endl;
        }
}

int main()
{
    cout << "HelloWorld" << endl;
    return 0;
}

3.4 bsd风格

特点:括号一一对齐。

#include<iostream>
using namespace std;

void f(int i)
{
    if (i == 1)
    {
        cout << "helloworld" << endl;
    }
    else
    {
        cout << "HELLOWORLD" << endl;
    }
}

int main()
{
    cout << "HelloWorld" << endl;
    return 0;
}

3.5 java风格

特点:所有的括号都没有对齐,直接写在同一行的最后。

#include<iostream>
using namespace std;

void f(int i) {
    if (i == 1) {
        cout << "helloworld" << endl;
    } else {
        cout << "HELLOWORLD" << endl;
    }
}

int main() {
    cout << "HelloWorld" << endl;
    return 0;
}
最后修改:2018 年 05 月 04 日
如果觉得我的文章对你有用,请随意赞赏