How do you specify a required switch (not argument) with Ruby OptionParser?

前端 未结 8 2081
时光取名叫无心
时光取名叫无心 2020-12-12 23:58

I\'m writing a script and I want to require a --host switch with value, but if the --host switch isn\'t specified, I want the option parsing to fai

8条回答
  •  执念已碎
    2020-12-13 00:36

    If host is required, then surely it isn't an option, it's an argument.

    With that in mind, here's a way to solve your problem. You can interrogate the ARGV array to see if a host has been specified, and, if it hasn't been, then call abort("You must specify a host!"), or similar, to make your program quit with an error status.

提交回复
热议问题