1:概述
2:下载
3:安装
2022/2/5 更新:章节 3.3:使用,后续会更新别的使用方法和可能用各种IDE来使用Uncrustify。
在下方的Where to get里面可以找到SourceForge的链接点进去下载就好了。同时还有GitHub项目的地址也可以点进去看看。
搞好配置文件了以后放在你喜欢的地方。接下来我们看看怎么使用。如果你使用的是qt或者atom他们都有加载Uncrustify的方法,这里不管他们,我们从命令行起步(将就在vscode里玩了,方便看目录树和文件)。
在命令行中输入uncrustify
,会看到他的手册页(你应该会看到如下图,如果出现uncrustify
不是内部命令等多半是环境变量问题):
下面直接贴出手册页内容:
PS G:\cProject\LearnOpenGL> uncrustify Usage: uncrustify.exe [options] [files ...] If no input files are specified, the input is read from stdin If reading from stdin, you should specify the language using -l or specify a filename using --assume for automatic language detection. If -F is used or files are specified on the command line, the output filename is 'prefix/filename' + suffix When reading from stdin or doing a single file via the '-f' option, the output is dumped to stdout, unless redirected with -o FILE. Errors are always dumped to stderr The '-f' and '-o' options may not be used with '-F' or '--replace'. The '--prefix' and '--suffix' options may not be used with '--replace'. Basic Options: -c CFG : Use the config file CFG, or defaults if CFG is set to '-'. -f FILE : Process the single file FILE (output to stdout, use with -o). -o FILE : Redirect stdout to FILE. -F FILE : Read files to process from FILE, one filename per line (- is stdin). --check : Do not output the new text, instead verify that nothing changes when the file(s) are processed. The status of every file is printed to stderr. The exit code is EXIT_SUCCESS if there were no changes, EXIT_FAILURE otherwise. files : Files to process (can be combined with -F). --suffix SFX : Append SFX to the output filename. The default is '.uncrustify' --prefix PFX : Prepend PFX to the output filename path. --replace : Replace source files (creates a backup). --no-backup : Do not create backup and md5 files. Useful if files are under source control. --if-changed : Write to stdout (or create output FILE) only if a change was detected. -l : Language override: C, CPP, D, CS, JAVA, PAWN, OC, OC+, VALA. -t : Load a file with types (usually not needed). -q : Quiet mode - no output on stderr (-L will override). --frag : Code fragment, assume the first line is indented correctly. --assume FN : Uses the filename FN for automatic language detection if reading from stdin unless -l is specified. Config/Help Options: -h -? --help --usage : Print this message and exit. --version : Print the version and exit. --count-options : Print the number of available options and exit. --show-config : Print out option documentation and exit. --update-config : Output a new config file. Use with -o FILE. --update-config-with-doc : Output a new config file. Use with -o FILE. --universalindent : Output a config file for Universal Indent GUI. --detect : Detects the config from a source file. Use with '-f FILE'. Detection is fairly limited. --set <option>=<value> : Sets a new value to a config option. Debug Options: -p FILE : Dump debug info into FILE, or to stdout if FILE is set to '-'. Must be used in combination with '-f FILE' -ds FILE : Dump parsing info at various moments of the formatting process. --dump-steps FILE This creates a series of files named 'FILE_nnn.log', each corresponding to a formatting step in uncrustify. The file 'FILE_000.log' lists the formatting options in use. Must be used in combination with '-f FILE' -L SEV : Set the log severity (see log_levels.h; note 'A' = 'all') -s : Show the log severity in the logs. --decode : Decode remaining args (chunk flags) and exit. --tracking_space FILE : Prepare tracking informations for debugging. Cannot be used with the -o option' Usage Examples cat foo.d | uncrustify -q -c my.cfg -l d uncrustify -c my.cfg -f foo.d uncrustify -c my.cfg -f foo.d -L0-2,20-23,51 uncrustify -c my.cfg -f foo.d -o foo.d uncrustify -c my.cfg -f foo.d -o foo.d -ds dump uncrustify -c my.cfg foo.d uncrustify -c my.cfg --replace foo.d uncrustify -c my.cfg --no-backup foo.d uncrustify -c my.cfg --prefix=out -F files.txt Note: Use comments containing ' *INDENT-OFF*' and ' *INDENT-ON*' to disable processing of parts of the source file (these can be overridden with enable_processing_cmt and disable_processing_cmt). There are currently 787 options and minimal documentation. Try UniversalIndentGUI and good luck.
我们需要用上面搞好的配置文件去对一个源文件进行处理并且得到一个格式化后的输出。举个例子如下图是我的新玩具glad的一段程序,包含opengl上下文等等一些内容,很明显采用的是大括号上下对齐,if
和for
等后面只有语句没有括号等风格,当然我最早学c的时候就喜欢这种风格,但因为后面洗心革面了被迫学习了一下讨厌的java,决定使用新的风格(待会看格式化后的风格)。
接着我们调出terminal然后输入uncrustify -c 配置目录 -f 源文件 -o 输出文件
命令得到的新文件如下图左侧的temp.cpp(我的命令是uncrustify -c -f .\src\main.cpp -o .\src\temp.cpp
因为我有默认配置所以-c后面没有目录参数):
可以观察到,右边新文件已经按照之前一下午调好的配置文件(***.cgf)文件格式化了,是不是很棒棒?