为群众增色。
beautysh的Python项目详细描述
美女
这个程序承担着美化bash脚本的艰巨任务。 (是的)。处理bash脚本并不简单,它们不像c或java 程序-它们有很多不明确的语法,而且(不寒而栗)您可以使用 关键字作为变量。几年前,在测试第一个版本时 程序,我遇到了这个示例:
done=0;while(($done <=10));doechodone=$done;done=$((done+1));done
同一个名字,但有三个不同的意思(叹息)。bash解释器可以解决 这种反常,但我决定不尝试重新创建bash解释器 美化剧本。这意味着这条Python会有一些边界情况 程序将无法处理。但是在大型linux系统的测试中 bash脚本,其无错误得分为~99%。
安装
如果您已经设置了pip
,则可以执行
pip install beautysh
或者克隆repo并安装:
git clone https://github.com/bemeurer/beautysh
cd beautysh
python setup.py install
用法
您可以从命令行调用beautysh,如
beautysh file1.sh file2.sh file3.sh
在这种情况下,它会美化每个文件。
可用标志为:
--indent-size INDENT_SIZE, -i INDENT_SIZE
Sets the number of spaces to be used in indentation.
--backup, -b Beautysh will create a backup file in the same path as
the original.
--check, -c Beautysh will just check the files without doing any
in-place beautify.
--tab, -t Sets indentation to tabs instead of spaces.
--force-function-style FORCE_FUNCTION_STYLE, -s FORCE_FUNCTION_STYLE
Force a specific Bash function formatting. See below
for more info.
--version, -v Prints the version and exits.
--help, -h Print this help message.
Bash function styles that can be specified via --force-function-style are:
fnpar: function keyword, open/closed parentheses, e.g. function foo()
fnonly: function keyword, no open/closed parentheses, e.g. function foo
paronly: no function keyword, open/closed parentheses, e.g. foo()
用于重新格式化stdin并在stdout上打印的示例:
beautysh - < infile.sh > outfile.sh
您也可以将Beautysh作为模块调用:
#!/usr/bin/env python# -*- coding: utf-8 -*- from beautysh import Beautysh [ ... ] result,error = Beautysh().beautify_string(source)
如前所述,beautysh可以在调用时美化大量bash脚本 通过各种方式,包括bash脚本:
#!/bin/shfor path in `find /path -name '*.sh'`do beautysh $pathdone
以及更明显的例子:
$ beautysh *.sh
CAUTION: Because Beautysh overwrites all the files submitted to it, this could have disastrous consequences if the files include some of the increasingly common Bash scripts that have appended binary content (a regime where Beautysh has undefined behaviour ). So please — back up your files, and don't treat Beautysh as a harmless utility. Even if that is true most of the time.
Beautysh在这里处理bash文档时非常小心(可能有一些 它不处理边境案件)。其基本思想是创始者知道 他在这里想要的格式医生,一个美容师不应该试图猜测 他。所以Beautysh尽其所能传递这里的文档内容 不变:
iftruethenecho"Before here-doc"# Insert 2 lines in file, then save.#--------Begin here document-----------# vi $TARGETFILE<<x23LimitStringx23iThis is line 1 of the example file.This is line 2 of the example file.^[ZZx23LimitStringx23#----------End here document-----------#echo"After here-doc"fi
特殊注释@formatter:off
和@formatter:on
可用于禁用语句块周围的格式设置。
# @formatter:offcommand\ --option1 \ --option2 \ --option3 \# @formatter:on
这是从eclipse特性中得到的灵感。
最初由Paul Lutus