Artistic Style详解

    Astyle 全称为 Artistic Style,官方网站是 http://astyle.sourceforge.net

    Astyle 是一个命令行程序,简单的使用方法是直接使用Astyle加上你要格式化的程序的源文件,Astyle支持不同的代码风格,也有不同的参数来设置自己想要的效果,astyle是一个命令行工具。

关于学习最好的方法还是官方文档:http://astyle.sourceforge.net/astyle.html

    命令语法:

astyle [options] < original > Beautified
astyle [options] Foo.cpp Bar.cpp  [...]

可以用通配符指定要处理的文件,用 -r 递归处理子目录

   举例:

  • 下面的命令可以一次性格式化某个目录(包含子目录)下所有的源文件和头文件
for /R %f in (*.cpp;*.c;*.h) do astyle --style=ansi "%f"
  • 下面的命令将格式化foo.c文件,更改其风格为ANSI,并将原始文件备份到foo.c.orgin 
astyle --style=ansi foo.c

 

1. 通用命令

查询版本:astyle -V

帮助信息:astyle --help

 

2. 预定义风格

Astyle包含了以下几种预定义风格,只需在参数中简单指定即可使用:ansi, kr, linux, gnu, java

  • --style=ansi  ANSI 风格格式和缩进
namespace foospace
{
 int Foo()
 {
  if (isBar)
  {
   bar();
   return 1;
  }
  else
   return 0;
 }
}
  • --style=kr  Kernighan&Ritchie 风格格式和缩进
namespace foospace {
 int Foo() {
  if (isBar) {
   bar();
   return 1;
  } else
   return 0;
 }
}
  • --style=linux    Linux 风格格式和缩进
namespace foospace
{
 int Foo()
 {
  if (isBar) {
   bar();
   return 1;
  } else
   return 0;
 }
}
  • --style=gnu   GNU 风格格式和缩进
namespace foospace
{
 int Foo()
 {
  if (isBar)
  {
   bar();
   return 1;
  }
  else
   return 0;
 }
}
  • --style=java :Java 风格格式和缩进
class foospace {
 int Foo() {
  if (isBar) {
   bar();
   return 1;
  } else
   return 0;
 }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章