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

本文介绍了AStyle,一个用于自动格式化C、C++、C#和Java源代码的工具,支持多种风格,如K&R、Allman等。文章详细讲解了如何在Ubuntu和Windows上安装,以及其主要功能和命令行用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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; }

使用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个空格进行缩进。

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

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

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

或者:

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

Ubuntu 下安装

  1. 使用 apt-get 命令安装
sudo apt-get update -y
sudo apt-get install -y astyle
  1. 源码编译安装
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/

包含源代码,需要的可以自行下载。 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径 VC6++ 设置方法 菜单->工具->定制->工具菜单内容->新建菜单,参数如下 命令行:astyle.exe 变量: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(FileName)$(FileExt) 初始目录: $(FileDir) VC2008 外部工具里设置,还可以添加快捷键 命令:astyle.exe 参数: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(ItemFileName)$(ItemExt) 初始目录: $(ItemDir) CodeBlocks 设置差不多,很多绿色版的已经设置好了 AStyle_2.02_windows.zip bin 目录里有官方编译好的 Artistic Style 2.01 Maintained by: Jim Pattee Original Author: Tal Davidson Usage : astyle [options] Source1.cpp Source2.cpp [...] astyle [options] 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主公讲 ARM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值