python简易工具(一)——pycmd_command

pycmd_command是一个Python类,用于创建和管理命令行指令。它检查命令是否存在sys.argv中,解析指定数量的参数,并调用提供的函数来执行命令。如果参数错误或函数调用出错,它会抛出相应的异常。此外,还有command2类作为command的复制品,以及commands类用于管理多个命令字典。
摘要由CSDN通过智能技术生成

pycmd_command是一个简单的创建命令行指令的程序

以下为代码:

pycmd_command.py

class command(object):
    def __init__(self,command_name,command_args_num:int) -> None:
        if command_name=='' or type(command_name)!=str:
            raise SyntaxError('invalid command name')
        command_name='-'+command_name
        self.command_name=command_name
        self.command_args=command_args_num
    def command_exist(self):
        a=0
        import sys
        for i in sys.argv :
            if i == self.command_name:
                self.exist=(True,a)
                return True,a
            a+=1
        self.exist=(False,-1)
        return False,-1
    def delete_command(self):
        del self.command_name,self.command_args
    def run_command(self,func):
        import sys
        if self.command_exist()[0]:
            FROM=self.exist[1]+1
            TO=self.exist[1]+self.command_args+1
            try:
                args=tuple(sys.argv[FROM:TO])
            except IndexError as err:
                print('an error was found:')
                print(err)
                print('end of error output') 
                args=()
            try:
                func(args)
            except TypeError:
                raise SyntaxError('invalid function or args')
class command2(object):
    def __init__(self,command_name,command_args_num:int) -> None:
        if command_name=='' or type(command_name)!=str:
            raise SyntaxError('invalid command name')
        self.command_name=command_name
        self.command_args=command_args_num
    def command_exist(self):
        a=0
        import sys
        for i in sys.argv :
            if i == self.command_name:
                self.exist=(True,a)
                return True,a
            a+=1
        self.exist=(False,-1)
        return False,-1
    def delete_command(self):
        del self.command_name,self.command_args
    def run_command(self,func):
        import sys
        if self.command_exist()[0]:
            FROM=self.exist[1]+1
            TO=self.exist[1]+self.command_args+1
            try:
                args=tuple(sys.argv[FROM:TO])
            except IndexError as err:
                print('an error was found:')
                print(err)
                print('end of error output') 
                args=()
            try:
                func(args)
            except :
                raise SyntaxError('invalid function or args')
class commands(object):
    def __init__(self,commands_dict:dict[str:int]) -> None:

        self.commands_dict=commands_dict
        obj_list=[]
        for key,value in commands_dict.items():
            b=command2(key,value)
            obj_list.append(b)
        self.command_obj=obj_list
    def command_exist(self):
        a=[]
        for key,value in self.commands_dict.items() :
            b=command2(key,value)
            b=b.command_exist()
            a.append(b)
        return a
   
    def delete_command(self):
        del self.commands_dict
    def run_command(self,func):
        ii=0
        obj=self.command_obj()
        for i in func:
            obj[ii].run_command(i)
            obj[ii].delete_command()
            ii+=1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值