astyle批量优化代码风格

#astyle & Google style

本文主要介绍使用astyle这个工具来批量优化我们的代码风格为Google style。


##astyle
###安装
Ubuntu安装: sudo apt-get install astyle

###使用
Usage: astyle [options] Source1.cpp Source2.cpp […]
    astyle [options] < Original > Beautified

例如:
    astyle --style=kr your.cpp your.h
这个命令优化了your.cpp和your.h文件,优化为kr风格。

kr风格
–style=kr

namespace My_Style
{
int Style()
{
    if (flag) {
        return 1;
    } else {
        return 0;
    }
}
}
**ansi风格** --style=ansi ``` namespace My_Style { int Style() { if (flag) { return 1; } else { return 0; } } } ``` **linux风格** --style=linux ``` namespace My_Style { int Style() { if (flag) { return 1; } else { return 0; } } } ``` **gnu风格** --style=gnu ``` namespace My_Style { int Style() { if (flag) { return 1; } else { return 0; } } } ``` **java风格** --style=java ``` namespace My_Style { int Style() { if (flag) { return 1; } else { return 0; } } } ``` ##Google style 目前实习的公司代码采用[Google style](http://zh-google-styleguide.readthedocs.io/en/latest/)。 使用工具cpplint可以进行Google style的检查,cpplint是一个python脚本,Google使用它作为自己的C++代码规范检查工具。 ###安装 sudo apt-get install python-pip sudo pip install cpplint ###使用 cpplint your.cpp ##使用astyle批量优化 ###参数 - `-p` 在操作符两边插入空格,如=、+、-等。 如:int a=10*60; 处理后:int a = 10 * 60; - `-a` 大括号前一个与上一行在同一行 - `-n` 不备份文件 - `-U` 移除括号两端多余空格 - `-s#` 默认行缩进为4个空格,可以将#替换为缩进量 - `-c` tab转换为空格 - `-S` 缩进switch中的case块,case和switch不在同一列 - `-q` 忽略所有错误

更过的参数使用可以通过 astyle -h 获取
###批量格式化
使用java风格的时候,类定义中的public private protected标签均不缩进,而Google style是需要有一个空格的。暂时没有找到可以配置的参数来实现,这里采用sed来实现。
有知道的大神,还请指点一下。

#!/bin/bash

#批量格式化
for f in $(find ./ -name '*.c' -or -name '*.cpp' \
    -or -name '*.h' -or -name '*.hpp' -type f)
do
    astyle --style=java -p -s4 -a -n -U -H -c -S $f > /dev/null 2>&1
    sed -i 's/public:/ public:/g' `grep public: -rl $f` > /dev/null 2>&1
    sed -i 's/private:/ private:/g' `grep private: -rl $f` > /dev/null 2>&1
    sed -i 's/protected:/ protected:/g' `grep protected: -rl $f` > /dev/null 2>&1
done

欢迎大家批评指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含源代码,需要的可以自行下载。 把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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值