astyle基本功能介绍

时间:2023-03-09 07:11:16
astyle基本功能介绍

astyle是一个命令行工具,语法格式如下

astyle [options] < original >

例如:

astyle --style=ansi foo.cpp

上面的命令将美化foo.cpp文件,更改其风格为ANSI,并将原始文件备份到foo.cpp.orgin。

具体的来说,astyle包含了以下几种预定义风格,只需在参数中简单指定即可使用:

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

Astyle具有很多其他options可以用来选择确定自己所需要的美化风格,每个选项的具体用法可以参考Astyle的官方使用说明文档。

批量格式化:

Astyle支持通配符操作,例如:

Astyle --style=ansi *.c *.h 则会将当前目录下的所有.c .h文件格式化为ansi风格

另外如果需要对当前目录以及所有子目录下的源文件进行批量格式化,可以使用如下的命令

for /R %f in (*.cpp;*.c;*.h) do astyle --style=ansi "%f"

该命令在当前目录中寻找文件名匹配模式 *.cpp;*.c;*.h 的所有文件(不同模式可用英文逗号隔开),并且对每个文件%f执行操作:

另外补充说明一下,Astyle的选项支持两种语法格式,一种是两横杠,例如astyle --style=linux *.c,另外一种是单横杠,例如Astyle -A8 *.c;这两种语法格式的作用是完全等同的。