1

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?

2 Answers 2

4

There isn't any set-in-stone link between them. The question you link to appears to be a program that can do one of several different things, with command-line arguments switching between them. These things happen to be implemented in the program using methods.

It is implied by the question that they have used something like argparse to write the glue between these; but the use of methods is just an implementation detail of the particular program.

Sign up to request clarification or add additional context in comments.

Comments

1

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:

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:

    #!/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

4 Comments

One of the problems wit your example is that it lacks any way to give value to arguments such as --max-val 5 or --treads=5
yes thats true indeed, but I didn't need specific arguments for my "commands" because the god class itself knew what to do.^^
wow, I never thought of doing something like that! Interesting.
I just added a cleaner way of this method I used. Now my "god class" is more like a "commander" that calls some functions^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.