options parsing: required and conflict

Discussion in 'Ruby' started by Kirill Shutemov, May 11, 2005.

  1. 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?
    Kirill Shutemov, May 11, 2005
    #1
    1. Advertising

  2. Kirill Shutemov wrote:
    > 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
    Robert Klemme, May 11, 2005
    #2
    1. Advertising

  3. Is that true, OptionParse has no support of it?
    Kirill Shutemov, May 11, 2005
    #3
  4. Kirill Shutemov

    Glenn Parker Guest

    Kirill Shutemov wrote:
    > 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.

    --
    Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/>
    Glenn Parker, May 11, 2005
    #4
  5. Kirill Shutemov

    Guest

    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.

    --
    Nobu Nakada
    , May 11, 2005
    #5
  6. ------=_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--
    Kirill Shutemov, May 23, 2005
    #6
  7. Kirill Shutemov

    Guest

    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

    --
    Nobu Nakada
    , May 24, 2005
    #7
  8. Kirill Shutemov

    Eric Hodel Guest

    On 23 May 2005, at 02:10, Kirill Shutemov wrote:

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

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
    Eric Hodel, May 24, 2005
    #8
  9. ------=_Part_15414_14121679.1116928452051
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 5/24/05, Eric Hodel <> wrote:
    >=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--
    Kirill Shutemov, May 24, 2005
    #9
  10. 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.

    --
    Nobu Nakada
    nobuyoshi nakada, May 25, 2005
    #10
  11. ------=_Part_19699_28028025.1117013576790
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 5/25/05, nobuyoshi nakada <> wrote:
    >=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--
    Kirill Shutemov, May 25, 2005
    #11
  12. On 25/05/05, Kirill Shutemov <> wrote:
    > On 5/25/05, nobuyoshi nakada <> wrote:
    > >
    > > 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/
    Brian Schröder, May 25, 2005
    #12
  13. Brian Schröder, May 25, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,021
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    496
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,058
  4. charles cashion

    css conflict (or html conflict)

    charles cashion, Feb 18, 2009, in forum: HTML
    Replies:
    2
    Views:
    776
    charles cashion
    Feb 18, 2009
  5. Sijo Kg
    Replies:
    1
    Views:
    90
    Justin Collins
    Jan 7, 2009
Loading...

Share This Page