Python命令行参数与方法有何关系? [英] How are Python command line arguments related to methods?

查看:79
本文介绍了Python命令行参数与方法有何关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都在类太大且难以添加新功能的情况下完全不受该问题的限制,该问题以某种方式将命令行选项连接到方法,但是我找不到有关此问题的文档。不是 optparse argparse sys.argv -这个问题暗示方法和命令行选项之间存在某种直接关系。我缺少什么?

Everyone at Class too big and hard to add new features is completely unphased by the question, which somehow connects command line options to methods, but I can find no documentation for this. It's not optparse, or argparse, or sys.argv - the question implies some kind of direct relationship between methods and command line options. What am I missing?

推荐答案

我只是使用这样的类,这似乎不是一个好主意,因为它一旦有很多命令,就很难维护。

I simply use the class like this, what seems not to be a very good idea, because it is very hard to maintain once u got many commands.

class myprogram(object):
    def __init__(self)
        self.prepare()
    def prepare(self):
        # some initializations
        self.prepareCommands()
    def prepareCommands(self):
        self.initCommand("--updateDatabase", self.updateDatabase)
        self.initCommand("--getImages", self.getImages)
        # and so on
    def initCommand(self, cmd, func):
        options = sys.argv
        for option in options:
            if option.find(cmd)!=-1:
                return func()
    # my commands
    def updateDatabase(self):
        #...
    def getImages(self):
        #...
if __name__ == "__main__":
    p = myprogram()

EDIT1:
这是我刚刚实现的一种更简洁的方法:

Here a cleaner way I just implemented:

myprogram.py:

myprogram.py:

from config import * # has settings
from commands import *

from logsys import log
import filesys

class myprogram(object):
    def __init__(self):
        log(_class=self.__name__, _func='__init__', _level=0)
        log(_class=self.__name__, _func='__init__',  text="DEBUG LEVEL %s" % settings["debug"], _level=0)
        self.settings = settings
        self.cmds = commands
    def prepare(self):
        log(_class=self.__name__, _func='prepare', _level=1)
        self.dirs = {}
        for key in settings["dir"].keys():
            self.dirs[key] = settings["dir"][key]
            filesys.checkDir(self.dirs[key])

    def initCommands(self):
        log(_class=self.__name__, _func='initCommands', _level=1)
        options = sys.argv
        for option in options:
            for cmd in self.cmds.keys():
                if option.find(cmd) != -1:
                    return self.cmds[cmd]()


if __name__ == '__main__':    
    p = myprogram()
    p.prepare()
    p.initCommands()

commands.py:

commands.py:

    #!/usr/bin/env python
# -*- coding: utf-8 -*-



commands = {}
#csv
import csvsys
commands["--getCSV"] = csvsys.getCSV
#commands["--getCSVSplitted"] = csvsys.getCSVSplitted



# update & insert
import database
commands["--insertProductSpecification"] = database.insertProductSpecification


# download
import download
commands["--downloadProductSites"] = download.downloadProductSites
commands["--downloadImages"] = download.downloadImages

# parse
import parse
commands["--parseProductSites"] = parse.parseProductSites

EDIT2:我现在更新了您的问题链接到您的问题一个更完整的示例类太大而很难添加新功能

I have now updated my question you linked to your question with a more complete example Class too big and hard to add new features

这篇关于Python命令行参数与方法有何关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆