Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1404
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2017-03-21 23:53
文章分类
文章存档

2017年(2)

我的朋友
最近访客

分类: LINUX

2017-03-21 23:54:46

原文地址:astyle格式化代码 作者:txgc_wm

           astyle是一个开源工具,它可以方便的将代码格式化成自己想要的样式而不必人工修改。可以到网站()上下载源码自己编译,也可以使用指令(apt-get install astyle)安装。


下面介绍一下astyle的简单使用。例如有以下的源码:
  1. #include <stdio.h>
  2. int main()
  3. {int i;printf("Just a test!\n");for(i=0;i<10;++i)printf("%d\n",i);}return 0;}
然后在终端下输入一下指令:
  1. astyle test1.c
效果如下:
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int i;
  5.     printf("Just a test!\n");
  6.     for(i=0; i<10; ++i) {
  7.         printf("%d\n",i);
  8.     }
  9.     return 0;
  10. }

当然也可以加上一些选项,例如“astyle --style=bsd test1.c”,“ astyle --style=gnu test1.c”等等。


vim中的命令模式下,可以使用下面的某一种方式来格式化代码。

  1. %!astyle (simple case - astyle default mode is C/C++)
或者
  1. %!astyle --mode=c --style=ansi -s2 (ansi C++ style, use two spaces per indent level)

或者

  1. 1,40!astyle --mode=c --style=ansi (ansi C++ style, filter only lines 1-40)

为方便使用,可以把它写成一个脚本,代码如下:

  1. #! /bin/bash

  2. for f in $(find . -name '*.c' -or -name '*.cpp' -type f)
  3. do
  4.     astyle $f
  5. done
在格式化完代码后,会生成一个后缀为orig的文件,将脚本更改如下:
  1. #! /bin/bash

  2. for f in $(find . -name '*.c' -or -name '*.cpp' -or -name '*.h' -type f)
  3. do
  4.      astyle $f
  5. done

  6. # after formate the code,we need to rm '*.orig' files
  7. for f in $(find . -name '*.orig' -type f)
  8. do
  9.      rm $f
  10. done


astyle其使用说明如下:

点击(此处)折叠或打开

  1. Usage : astyle [options] Source1.cpp Source2.cpp [...]
  2. astyle [options] < Original > Beautified

  3. When indenting a specific file, the resulting indented file RETAINS the
  4. original file-name. The original pre-indented file is renamed, with a
  5. suffix of ".orig" added to the original filename.

  6. Wildcards (* and ?) may be used in the filename.
  7. A 'recursive' option can process directories recursively.

  8. By default, astyle is set up to indent C/C++/C#/Java files, with four
  9. spaces per indent, a maximal indentation of 40 spaces inside continuous
  10. statements, a minimum indentation of eight spaces inside conditional
  11. statements, and NO formatting options.

  12. Option's Format:
  13. ----------------
  14. Long options (starting with '--') must be written one at a time.
  15. Short options (starting with '-') may be appended together.
  16. Thus, -bps4 is the same as -b -p -s4.

  17. Predefined Style Options:
  18. -------------------------
  19. --style=allman OR --style=ansi OR --style=bsd OR -A1
  20. Allman style formatting/indenting.
  21. Broken brackets.

  22. --style=java OR -A2
  23. Java style formatting/indenting.
  24. Attached brackets.

  25. --style=k&r OR --style=k/r OR -A3
  26. Kernighan & Ritchie style formatting/indenting.
  27. Linux brackets.

  28. --style=stroustrup OR -A4
  29. Stroustrup style formatting/indenting.
  30. Stroustrup brackets.

  31. --style=whitesmith OR -A5
  32. Whitesmith style formatting/indenting.
  33. Broken, indented brackets.
  34. Indented class blocks and switch blocks.

  35. --style=banner OR -A6
  36. Banner style formatting/indenting.
  37. Attached, indented brackets.
  38. Indented class blocks and switch blocks.

  39. --style=gnu OR -A7
  40. GNU style formatting/indenting.
  41. Broken brackets, indented blocks, indent is 2 spaces.

  42. --style=linux OR -A8
  43. GNU style formatting/indenting.
  44. Linux brackets, indent is 8 spaces.

  45. --style=horstmann OR -A9
  46. Horstmann style formatting/indenting.
  47. Horstmann brackets, indented switches, indent is 3 spaces.

  48. --style=1tbs OR --style=otbs OR -A10
  49. One True Brace Style formatting/indenting.
  50. Linux brackets, add brackets to all conditionals.

  51. Tab and Bracket Options:
  52. ------------------------
  53. default indent option
  54. If no indentation option is set,
  55. the default option of 4 spaces will be used.

  56. --indent=spaces=# OR -s#
  57. Indent using # spaces per indent. Not specifying #
  58. will result in a default of 4 spaces per indent.

  59. --indent=tab OR --indent=tab=# OR -t OR -t#
  60. Indent using tab characters, assuming that each
  61. tab is # spaces long. Not specifying # will result
  62. in a default assumption of 4 spaces per tab.

  63. --indent=force-tab=# OR -T#
  64. Indent using tab characters, assuming that each
  65. tab is # spaces long. Force tabs to be used in areas
  66. Astyle would prefer to use spaces.

  67. default brackets option
  68. If no brackets option is set,
  69. the brackets will not be changed.

  70. --brackets=break OR -b
  71. Break brackets from pre-block code (i.e. ANSI C/C++ style).

  72. --brackets=attach OR -a
  73. Attach brackets to pre-block code (i.e. Java/K&R style).

  74. --brackets=linux OR -l
  75. Break definition-block brackets and attach command-block
  76. brackets.

  77. --brackets=stroustrup OR -u
  78. Attach all brackets except function definition brackets.

  79. --brackets=horstmann OR -g
  80. Break brackets from pre-block code, but allow following
  81. run-in statements on the same line as an opening bracket.

  82. Indentation options:
  83. --------------------
  84. --indent-classes OR -C
  85. Indent 'class' blocks, so that the inner 'public:',
  86. 'protected:' and 'private: headers are indented in
  87. relation to the class block.

  88. --indent-switches OR -S
  89. Indent 'switch' blocks, so that the inner 'case XXX:'
  90. headers are indented in relation to the switch block.

  91. --indent-cases OR -K
  92. Indent case blocks from the 'case XXX:' headers.
  93. Case statements not enclosed in blocks are NOT indented.

  94. --indent-brackets OR -B
  95. Add extra indentation to '{' and '}' block brackets.

  96. --indent-blocks OR -G
  97. Add extra indentation entire blocks (including brackets).

  98. --indent-namespaces OR -N
  99. Indent the contents of namespace blocks.

  100. --indent-labels OR -L
  101. Indent labels so that they appear one indent less than
  102. the current indentation level, rather than being
  103. flushed completely to the left (which is the default).

  104. --indent-preprocessor OR -w
  105. Indent multi-line #define statements.

  106. --indent-col1-comments OR -Y
  107. Indent line comments that start in column one.

  108. --max-instatement-indent=# OR -M#
  109. Indent a maximal # spaces in a continuous statement,
  110. relative to the previous line.

  111. --min-conditional-indent=# OR -m#
  112. Indent a minimal # spaces in a continuous conditional
  113. belonging to a conditional header.

  114. Padding options:
  115. --------------------
  116. --break-blocks OR -f
  117. Insert empty lines around unrelated blocks, labels, classes, ...

  118. --break-blocks=all OR -F
  119. Like --break-blocks, except also insert empty lines
  120. around closing headers (e.g. 'else', 'catch', ...).

  121. --pad-oper OR -p
  122. Insert space paddings around operators.

  123. --pad-paren OR -P
  124. Insert space padding around parenthesis on both the outside
  125. and the inside.

  126. --pad-paren-out OR -d
  127. Insert space padding around parenthesis on the outside only.

  128. --pad-paren-in OR -D
  129. Insert space padding around parenthesis on the inside only.

  130. --pad-header OR -H
  131. Insert space padding after paren headers (e.g. 'if', 'for'...).

  132. --unpad-paren OR -U
  133. Remove unnecessary space padding around parenthesis. This
  134. can be used in combination with the 'pad' options above.

  135. --delete-empty-lines OR -x
  136. Delete empty lines within a function or method.
  137. It will NOT delete lines added by the break-blocks options.

  138. --fill-empty-lines OR -E
  139. Fill empty lines with the white space of their
  140. previous lines.

  141. Formatting options:
  142. -------------------
  143. --break-closing-brackets OR -y
  144. Break brackets before closing headers (e.g. 'else', 'catch', ...).
  145. Use with --brackets=attach, --brackets=linux,
  146. or --brackets=stroustrup.

  147. --break-elseifs OR -e
  148. Break 'else if()' statements into two different lines.

  149. --add-brackets OR -j
  150. Add brackets to unbracketed one line conditional statements.

  151. --add-one-line-brackets OR -J
  152. Add one line brackets to unbracketed one line conditional
  153. statements.

  154. --keep-one-line-blocks OR -O
  155. Don't break blocks residing completely on one line.

  156. --keep-one-line-statements OR -o
  157. Don't break lines containing multiple statements into
  158. multiple single-statement lines.

  159. --convert-tabs OR -c
  160. Convert tabs to the appropriate number of spaces.

  161. --align-pointer=type OR -k1
  162. --align-pointer=middle OR -k2
  163. --align-pointer=name OR -k3
  164. Attach a pointer or reference operator (* or &) to either
  165. the operator type (left), middle, or operator name (right).

  166. --mode=c
  167. Indent a C or C++ source file (this is the default).

  168. --mode=java
  169. Indent a Java source file.

  170. --mode=cs
  171. Indent a C# source file.

  172. Other options:
  173. --------------
  174. --suffix=####
  175. Append the suffix #### instead of '.orig' to original filename.

  176. --suffix=none OR -n
  177. Do not retain a backup of the original file.

  178. --options=####
  179. Specify an options file #### to read and use.

  180. --options=none
  181. Disable the default options file.
  182. Only the command-line parameters will be used.

  183. --recursive OR -r OR -R
  184. Process subdirectories recursively.

  185. --exclude=####
  186. Specify a file or directory #### to be excluded from processing.

  187. --errors-to-stdout OR -X
  188. Print errors and help information to standard-output rather than
  189. to standard-error.

  190. --preserve-date OR -Z
  191. The date and time modified will not be changed in the formatted file.

  192. --verbose OR -v
  193. Verbose mode. Extra informational messages will be displayed.

  194. --formatted OR -Q
  195. Formatted display mode. Display only the files that have been formatted.

  196. --quiet OR -q
  197. Quiet mode. Suppress all output except error messages.

  198. --lineend=windows OR -z1
  199. --lineend=linux OR -z2
  200. --lineend=macold OR -z3
  201. Force use of the specified line end style. Valid options
  202. are windows (CRLF), linux (LF), and macold (CR).

  203. --version OR -V
  204. Print version number.

  205. --help OR -h OR -?
  206. Print this help message.

  207. Default options file:
  208. ---------------------
  209. Artistic Style looks for a default options file in the
  210. following order:
  211. 1. The contents of the ARTISTIC_STYLE_OPTIONS environment
  212. variable if it exists.
  213. 2. The file called .astylerc in the directory pointed to by the
  214. HOME environment variable ( i.e. $HOME/.astylerc ).
  215. 3. The file called astylerc in the directory pointed to by the
  216. USERPROFILE environment variable ( i.e. %USERPROFILE%\astylerc ).
  217. If a default options file is found, the options in this file
  218. will be parsed BEFORE the command-line options.
  219. Long options within the default option file may be written without
  220. the preliminary '--'.
阅读(309) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Astyle编程语言格式化工具的中文说明

给主人留下些什么吧!~~