• 【嵌入式开发 Linux 常用命令系列 8 --代码格式修改工具 astyle】


    AStyle 介绍

    在这里插入图片描述

    AStyle,全名 Artistic Style,是一款源代码格式化工具。它可以自动格式化 CC++C#,和Java源代码。使用它您可以轻松地对代码进行格式化,以满足您需要的代码风格。它提供了大量的选项,可以非常细致地控制代码的格式化方式。并且,它是一个开放源码的工具,所以您可以自由地使用和修改它。

    AStyle的主要功能包括:

    • 缩进风格的控制,包括K&R风格,Allman风格,GNU风格,和自定义风格。
    • 缩进宽度的控制,可以设置为使用空格或Tab,也可以设置Tab的宽度。
    • 代码块大括号的位置控制。
    • 在运算符两侧添加或删除空格。
    • 控制if,for,while等语句的括号是否需要在新的一行开始。
    • 一键格式化整个源文件或选定的代码块。

    以下是使用AStyle的一个例子:代码格式化前:

    int main(){ printf("Hello, World!"); return 0; }
    
    • 1

    使用AStyle格式化后(K&R风格,4个空格缩进):

    int main()
    {
        printf("Hello, World!");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    命令行示例:

    astyle --style=kr --indent=spaces=4 -f myfile.c
    
    • 1

    这条命令会将 myfile.c 中的代码格式化为K&R风格,并且使用4个空格进行缩进。

    将当前目录及其子目录下的 C 文件转换成 linux 风格:

    find . -iname "*.c" | xargs -I {} astyle --style=kr --indent=spaces=4 -f {} \;
    
    • 1

    -I {} 表示使用 find 的输出当做 astyle的输入

    或者:

    find . -iname "*.c" -exec astyle --style=kr --indent=spaces=8 -f {} \;
    
    • 1

    Ubuntu 下安装

    1. 使用 apt-get 命令安装
    sudo apt-get update -y
    sudo apt-get install -y astyle
    
    • 1
    • 2
    1. 源码编译安装
    git clone https://github.com/steinwurf/astyle.git
    cd astyle
    python waf configure --cxx_mkspec=cxx_default
    python waf build
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述


    编译好之后将 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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    参考:https://iq.opengenus.org/install-astyle-in-ubuntu/

    Windows 下安装

    见:https://sourceforge.net/projects/astyle/

  • 相关阅读:
    SpringBoot-自定义转换器
    正则表达式replaceAll()方法具有什么功能呢?
    金仓数据库 KingbaseES 客户端编程接口指南 - PHP PDO (4. KingbaseES PDO的函数说明)
    Java的动态代理Proxy.newProxyInstance
    Linux fdformat命令教程:如何进行软盘的低级格式化(附案例详解和注意事项)
    多组学+机器学习+膀胱癌+分型+建模
    9.2 Windows钩子
    C++基础算法总结
    java基于springboot+vue的编程教学在线考试系统 elementui
    【C++】设计模式之——建造者
  • 原文地址:https://blog.csdn.net/sinat_32960911/article/details/134051718