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

Robert Klemme <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAM9pMnOTyuQihA50j8YwKkFXCSZ_t3BHAShxiu8mp5oNCodaMQ@mail.gmail.com>
On Wed, Apr 3, 2019 at 1:55 AM Ryan Davis <[email protected]> wrote:

> > 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.

Some cases of malformed arguments are handled by declaring the
argument type (i.e. you can specify that something must be an
integer). It might even be possible to provide a custom regular
expression or a list of allowed values.

Kind regards

robert

-- 
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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.