我有一个需要将版本作为参数的Ruby程序。此版本是指程序将与之交互的资源的版本,而不是程序本身的版本。我找到了Trollop,并对它的简洁感到满意。但是,Trollop似乎会自动创建一个--version标志来打印出该程序的版本。我希望我的版本标志以字符串作为参数。
Google搜索在Trollup项目的History.txt中显示以下内容:
== 1.3 / 2007-01-31
* Wrap at (screen width - 1) instead of screen width.
* User can override --help and --version.
* Bugfix in handling of -v and -h.
* More tests to confirm the above.
因此,从历史来看,我应该能够覆盖--version。 但是,当我创建--version选项并将其传入时,程序会立即退出。
opts = Trollop::options do
opt : version, "The version to use. ie. 1.0.1", :type => String, :required => true
end
上面的代码正确地要求传入version参数。但是,当我传入它时,程序会立即退出。我认为它是印刷版(显然是零)并退出。
如何覆盖版本以在Trollop中获取参数?
答案 0 :(得分:2)
将您的选项定义为:version
以外的其他选项,然后将:long
和:short
表单配置为version
和v
。
opts = Trollop::options do
opt :use_version, "The version to use. ie. 1.0.1",
:long => 'version', :short => 'v', :type => String, :required => false
end
当您将选项定义为:version
时,默认的:version
定义会被覆盖,但当Trollop看到已提供:version
选项时仍然throws a VersionNeeded
exception。然后Trollop打印版本并立即退出。
同样的想法也适用于:help
。