options parsing: required and conflict

K

Kirill Shutemov

Can I define options dependencies using OptionParser?=20
For example: if user defines option --option-a than he has to define
option --option-b or --option-c and mustn't define --option-d and
--option-e.
Is it possible?
 
R

Robert Klemme

Kirill said:
Can I define options dependencies using OptionParser?
For example: if user defines option --option-a than he has to define
option --option-b or --option-c and mustn't define --option-d and
--option-e.
Is it possible?

I would check these constraints after all options have been parsed. Nobu
might have a more elegant approach at hand, but I think, you can check
them only after you saw all options because order might differ.

Kind regards

robert
 
G

Glenn Parker

Kirill said:
Is that true, OptionParse has no support of it?

I don't think OptionParse has any way to express "dependencies" between
options, and that's a good thing. Given the generality of the problem,
there is no way it could do a better job than you would using straight
ruby code.
 
N

nobu.nokada

Hi,

At Wed, 11 May 2005 20:47:27 +0900,
Glenn Parker wrote in [ruby-talk:142164]:
I don't think OptionParse has any way to express "dependencies" between
options, and that's a good thing. Given the generality of the problem,
there is no way it could do a better job than you would using straight
ruby code.

I'd had similar idea in the early days, but abandoned it soon.
As those relations could come complicated easily, it didn't
feel worth to design/learn a such "mini-language" again. We
have already the powerful enough language.
 
K

Kirill Shutemov

------=_Part_11727_22358488.1116839447506
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
I'd had similar idea in the early days, but abandoned it soon.
As those relations could come complicated easily, it didn't
feel worth to design/learn a such "mini-language" again. We
have already the powerful enough language.

What about add to OptionsParser method 'usage':

opts =3D OptionParser.new{|opts|
<skip/>
opts.usage("--operation1 [--optional_option] --required_option ARG=20
[--optional_option2 ARG]")
opts.usage("--operation2 [--optional_option] --required_option2=20
[--optional_option3 ARG]")
<and_so_on/>
}

I tried to do it by myself, but optparse.rb is too difficult for me :(

------=_Part_11727_22358488.1116839447506--
 
N

nobu.nokada

Hi,

At Mon, 23 May 2005 18:10:50 +0900,
Kirill Shutemov wrote in [ruby-talk:143398]:
What about add to OptionsParser method 'usage':

opts = OptionParser.new{|opts|
<skip/>
opts.usage("--operation1 [--optional_option] --required_option ARG
[--optional_option2 ARG]")
opts.usage("--operation2 [--optional_option] --required_option2
[--optional_option3 ARG]")
<and_so_on/>
}

Adding mere messages?

$ ruby -roptparse -e 'ARGV.options{|opt|opt.separator("--foo");opt.parse!}' -- --help
Usage: -e [options]
--foo
 
E

Eric Hodel

What about add to OptionsParser method 'usage':

opts = OptionParser.new{|opts|
<skip/>
opts.usage("--operation1 [--optional_option] --required_option ARG
[--optional_option2 ARG]")
opts.usage("--operation2 [--optional_option] --required_option2
[--optional_option3 ARG]")
<and_so_on/>
}

I tried to do it by myself, but optparse.rb is too difficult for me :(

You mean opts.banner and opts.separator?
 
K

Kirill Shutemov

------=_Part_15414_14121679.1116928452051
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

=20
=20
You mean opts.banner and opts.separator?
=20
No, I mean that such metod can be used for build and usage message and=20
options dependences together.

------=_Part_15414_14121679.1116928452051--
 
N

nobuyoshi nakada

Hi,

At Tue, 24 May 2005 18:54:14 +0900,
Kirill Shutemov wrote in [ruby-talk:143518]:
No, I mean that such metod can be used for build and usage message and
options dependences together.

What I disposed is "mini-language" like it.
 
K

Kirill Shutemov

------=_Part_19699_28028025.1117013576790
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

=20
Hi,
=20
Tue, 24 May 2005 18:54:14 +0900,
Kirill Shutemov wrote in [ruby-talk:143518]:
No, I mean that such metod can be used for build and usage message and
options dependences together.
=20
What I disposed is "mini-language" like it.
=20
Why?

------=_Part_19699_28028025.1117013576790--
 
B

Brian Schröder

Hi,

Tue, 24 May 2005 18:54:14 +0900,
Kirill Shutemov wrote in [ruby-talk:143518]:
No, I mean that such metod can be used for build and usage message an= d
options dependences together.

What I disposed is "mini-language" like it.
Why?
=20
=20

I think because the dependency can be arbitrarily complex, so any
language would have to be extended until it is turing complete. So it
is easier to use ruby in the first place, because no new syntax is
introduced. If you want, you can easily write your own mini-language
for your special application.

Simply create an object descendent from OptionParser that contains
your settings as accessors. Include a check routine and call this
after parsing

I made a small example. Maybe something like this can go into the
OptionParser documentation. I think the option parser example at the
moment is great but a bit overwhelming.

best regards,

Brian

#!/usr/bin/ruby

require 'optparse'

class MyOptions < OptionParser
attr_accessor :eek:1, :eek:2
def initialize(args =3D ARGV)
super(args)
=20
self.on("--o1", "Option 1") do self.o1 =3D true end
self.on("--o2", "Option 2") do self.o2 =3D true end

self.parse!(args)=20
self.check =20
end
=20
def check
raise "Don't specify o1 and o2 at the same time" if self.o1 and self.o2
raise "Specify at least on out of o1 or o2" unless self.o1 or self.o2
end
end

options =3D MyOptions.new

puts "o1 specified" if options.o1
puts "o2 specified" if options.o2

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top