ruby - 在ruby中使用每个参数选项的多个输入来解析命令行参数
问题描述
我在 ruby 中使用 Slop 来解析输入参数:
slop_opts = Slop.parse(ARGV.map(&:strip)) do |o|
o.string '--test1', 'explain test1'
o.string '--test2', 'explain test2'
o.on '--help' do
puts o
exit
end
end
slop_opts.to_hash
我需要它test2
可以包括几个选项:例如
ruby this_script.rb --test1 one_arg --test2 first_arg second_arg
我的一个限制是我需要 first_arg 和 second_arg 是 2 个不同的输入,所以我不能仅仅通过拆分,
(或类似的)输入字符串(如first_arg,second_arg
.
谢谢您的帮助!
解决方案
做--test2
一个数组参数。将分隔符设置nil
为禁用拆分输入。
slop_opts = Slop.parse(ARGV.map(&:strip)) do |o|
o.string '--test1', 'explain test1'
o.array '--test2', 'explain test2', delimiter: nil
o.on '--help' do
puts o
exit
end
end
然后每个输入都有自己的--test2
.
ruby this_script.rb --test1 one_arg --test2 first_arg --test2 second_arg
推荐阅读
- python-3.x - 如果代码中没有 Tk() 声明怎么办
- python - PYOMO 优化以满足供给 < 需求
- python - 为什么 tkinter 不抛出/引发异常?
- python - Python Zipline 离线测试
- c# - OAuth 资源服务器,验证令牌并返回端点授权范围
- javascript - 无法在mounted() 中调用函数
- ios - Nativescript+Angular 上的 ListView 只占了竖屏的一半。使用 iPhone 模拟器
- python - 安装 Tornado 4.5.3 后如何恢复我的 Python 3.6?
- python - 如何使用 peewee 设置或选择 postgres 的 current_settings?
- c++ - 我的 object::collides(object * o) 函数总是返回 true,但什么也不做