为群众增色。

beautysh的Python项目详细描述


美女Build Status

这个程序承担着美化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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java关闭域实例   java如何获得JTable列的头组件的最小首选宽度,以便其大小不能小于该值?   使用时使用java Jsoup。孩子们()。跳出边界性感觉   类静态初始化块内的java类用法   java ParseCrashReporting*。在其Android SDK的1.11.0中找不到jar   javahsqldb组织。springframework。刀。复杂选择上的TransientDataAccessResourceException   java EJB 3.1@SessionScoped Bean holding登录信息注销问题   java如何跟踪重复警报何时关闭并更新设置值?   具有可变参数类型的java接口方法   使用openstack4j计算查询的java连接超时   java这是一种双向的一对一关系吗?   java如何重置EventStream中的最后一个累积值?   java如何在超级抽象类中使用@Bean?   使用多部分上传java图像   java列出了已解析的SQL SELECT语句的所有表/列   java使用内部类实现单元测试类的正确方法   使用AWS API网关的java ContentType飞行前处理与自己的Rest服务器   调用dispose()后重新创建JinternalFrame对象   字符编码通过java编写一个以UTF8编码的文本文件,其中包含BOM表。尼奥