用Python写命令吧

简介: 用Python写命令吧

commandflow

使用Python生成命令行

🍕 安装

pip install commandflow

🎉 快速开始

from commandflow import Command
class MyCommand(Command):
    exe = 'fastp'
    def input(self, filename):
        self.set_action('f', 'file', value=filename)
if __name__ == '__main__':
    command = MyCommand()
    command.input('test.file')
    print(command.command)  # fastp --file test.file

✨文档

设计动机

  1. 在进行生信分析时,往往会使用很多分析软件,当串联一个完整的分析流程时,需要编写大量的shell脚本,这很不优雅。
  2. 我不喜欢写shell。

层次结构

fd35c5bcb3f4382b2ceb76cc9a92fba7_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png

from commandflow import Command
class MyCommand(Command):
    exe = 'bwa'  # 指定可执行程序

或者也可以通过set_exe()来指定可执行程序

from commandflow import Command
class MyCommand(Command):
    exe = 'bwa'  # 指定可执行程序
c = MyCommand()
c.set_exe('bwa')

添加参数

是有set_action()来添加参数到最终的命令行中

from commandflow import Command
class MyCommand(Command):
    exe = 'bwa'
    def input(self, filename):
        self.set_action(
            'f',
            'file',
            value=filename
        )
c = MyCommand()
c.input('test.docx')

set_action()可以接收多个参数

- short_arg_name: 短参数名  -f
- long_arg_name: 长参数名  --file
- value:参数值
- help:对该条参数的解释说明
- positional:使用是一个位置参数,默认放到最后
- sep:当传递的value是一个list的时候,所指定的连接符,默认为空格
- stdout:命令是否需要指定标准输出

value可以为intfloatstrlist类型的值,当value是一个list类型的值时,会通过sep连接起来,如当sep为空格时,[1, 2, 3]会被拼接为1 2 3,这对某些接收多个参数值的参数非常有用。

获取最终的命令行

from commandflow import Command
class MyCommand(Command):
    exe = 'bwa'
    def input(self, filename):
        self.set_action(
            'f',
            'file',
            value=filename
        )
c = MyCommand()
c.input('test.docx')
print(c.command)  # bwa --file test.docx

当相同的参数值被多次设置参数时,后面设置的会将前面设置的覆盖

c = MyCommand()
c.input('test.docx')
c.input('apple.docx')
print(c.command)  # bwa --file apple.docx

生成多个命令行

上面的方式只能生成一条命令,但实际应用中可能会涉及到需要生成多条命令,彼此之间只是参数值不同

使用record()记录需要生成的命令,并通过records来获取所有记录的命令

c = MyCommand()
for file in ['1.docx', '2.docx', '3.docx']:
    c.input(file)
    c.record()
print(c.records)  # ['bwa --file 1.docx', 'bwa --file 2.docx', 'bwa --file 3.docx']

将所有命令重置(删除所有参数)

c = MyCommand()
c.clear()

更改参数前面的前缀

有些软件中,可能参数名前面不是---,比如某些参数名并不是双短线跟着长参数名,而是短短线跟着长参数名,如-file test.docx。可以在继承的类中通过添加long_dash来声明长参数名的前缀,short_dash来声明短参数名的前缀。

from commandflow import Command
class MyCommand(Command):
    long_dash = '-'
    short_dash = '-'
    exe = 'bwa'
相关文章
|
6天前
|
机器学习/深度学习 Shell 开发工具
Python使用管道执行git命令报错|4-7
Python使用管道执行git命令报错|4-7
|
20天前
|
Unix Shell Linux
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
9 1
|
4天前
|
Python Windows
Python:执行py命令,提示: Can‘t find a default Python.
Python:执行py命令,提示: Can‘t find a default Python.
|
8天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
12 0
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
2月前
|
前端开发 计算机视觉
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
117 2
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
Azure 容器实例(Azure Container Instances,简称 ACI)是一个无服务器容器解决方案,允许用户在 Azure 云环境中运行 Docker 容器,而无需设置虚拟机、集群或编排器。 ACI 适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道快速部署、数据处理和生成作业。
|
2月前
|
Linux Shell 数据库
python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装、基本命令、视图与网站
|
3月前
|
Linux iOS开发 MacOS
python的virtualenv虚拟环境常见问题和命令
`venv`是Python的内置模块,用于创建隔离的虚拟环境。创建虚拟环境如`python3 -m venv myenv`,激活环境在Windows上是`./venv/Scripts/activate`,在Unix-like系统是`source myenv/bin/activate`。退出环境用`deactivate`。`pip list`查看已安装包,`pip install`安装包,`pip freeze > requirements.txt`保存依赖。PyCharm中红色`venv`表示项目使用了虚拟环境。
69 2
 python的virtualenv虚拟环境常见问题和命令
|
3月前
|
Python
python常用命令
python常用命令
32 1
下一篇
无影云桌面