Re: optparse question: how to warn of missing positional arguments

Ryan Davis <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>

> On Apr 2, 2019, at 07:17, Robert Klemme <[email protected]> wrote:
> 
> On Fri, Mar 22, 2019 at 8:49 PM Artie Ziff <[email protected]> wrote:
> 
>> My goal is to force a display of the banner (usage string) when a positional parameter is not present; in the sense of ARGV[1] == nil
> 
> I do not think it is possible to ever get into that situation except
> for messing with ARGV in the ruby program. If you do not do that there
> will never be Nil in ARGV. Or are you talking about ARGV being too
> short? In that case, there are two situations
> 
> 1. a mandatory arg is missing for an option, then OptionParser will
> flag this anyway.
> 2. a mandatory non option argument is missing, then you just check for
> empty ARGV (or generally the length of ARGV) and flag this as error
> outside of OptionParser processing.

I totally overlooked that nil… Yeah. there won’t be a nil, but there might be an empty string. If that’s what you’re checking for, then you can raise InvalidArgument in the handling block (or write a converter? I’ve never needed/bothered to do that). See parse_arg in optparse.rb.

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.