擅长:python、mysql、java
<p>是的,现在是2015年,在<code>setuptools</code>和<code>distutils</code>中添加命令和选项的文档仍然大部分丢失。</p>
<p>几个小时后,我找到了以下代码,用于向<code>setup.py</code>命令的<code>install</code>添加自定义选项:</p>
<pre><code>from setuptools.command.install import install
class InstallCommand(install):
user_options = install.user_options + [
('custom_option=', None, 'Path to something')
]
def initialize_options(self):
install.initialize_options(self)
self.custom_option = None
def finalize_options(self):
#print('The custom option for install is ', self.custom_option)
install.finalize_options(self)
def run(self):
global my_custom_option
my_custom_option = self.custom_option
install.run(self) # OR: install.do_egg_install(self)
</code></pre>
<p>值得一提的是,install.run()检查它是否被称为“本机”或已被修补:</p>
<pre><code>if not self._called_from_setup(inspect.currentframe()):
orig.install.run(self)
else:
self.do_egg_install()
</code></pre>
<p>此时,您可以使用<code>setup</code>注册命令:</p>
<pre><code>setup(
cmdclass={
'install': InstallCommand,
},
:
</code></pre>