如何在setuptools上使用选项运行run_command?

qua*_*oke 7 python distutils setuptools

我的setup.py文件中 有两个自定义命令:create_tablesdrop_tables:

class create_tables(command):
    description = 'create DB tables'

    user_options = [
        ('database=', 'd', 'which database configuration use'),
        ('reset', 'r', 'reset all data previously'),
    ]

    def initialize_options(self):
        command.initialize_options(self)
        self.reset = False

    def run(self):
        if self.reset:
            self.run_command('drop_tables')
        else:
            command.run(self)
        from vk_relations import models
        models.create_tables()
        print 'Tables were created successfully'


class drop_tables(command):
    description = 'drop all created DB tables'

    user_options = [
        ('database=', 'd', 'which database configuration use'),
    ]

    def run(self):
        command.run(self)
        answer = raw_input('Are you sure you want to clear all VK Relations data? (y/n): ')
        if 'y' == answer:
            from vk_relations import models
            models.drop_tables()
            print 'Tables were dropped successfully'
        elif 'n' == answer:
            quit()
        else:
            sys.exit()
Run Code Online (Sandbox Code Playgroud)

命令$ setup.py create_tables -r -dmain应该运行命令drop_tables并在main数据库中创建新表,但run_command方法不允许为命令提供选项.如何databasedrop_tables内部create_tables命令指定选项?

qua*_*oke 4

现在我已经使用了这个技巧:

cmd_obj = self.distribution.get_command_obj('drop_tables')
cmd_obj.database = self.database
self.run_command('drop_tables')
Run Code Online (Sandbox Code Playgroud)