getopts::long & option string argument

Discussion in 'Perl Misc' started by Florian Kaufmann, Sep 2, 2009.

  1. What can Getopt::Long, or any other 'standard' module offer me for
    this problem task:

    I have an option which expects a string argument. But a) only a finite
    set of strings are to be accepted b) unique abbreviations shall be
    accepted, e.g. if the set of valid strings is ("foo","bar","fly") then
    "fo" should stand for "foo".

    An example for such a use case is the --backup[=CONTROL] option of
    many coreutils tools as for example cp, ln, install. CONTROL can only
    be ("no","off","numbered"... and a few more).
     
    Florian Kaufmann, Sep 2, 2009
    #1
    1. Advertising

  2. Florian Kaufmann wrote:

    > What can Getopt::Long, or any other 'standard'

    module offer me for
    > this problem task:
    >
    > I have an option which expects a string

    argument. But a) only a finite
    > set of strings are to be accepted b) unique

    abbreviations shall be
    > accepted, e.g. if the set of valid strings is

    ("foo","bar","fly") then
    > "fo" should stand for "foo".
    >
    > An example for such a use case is the --

    backup[=CONTROL] option of
    > many coreutils tools as for example cp, ln,

    install. CONTROL can only
    > be ("no","off","numbered"... and a few more).

    What you want to do is exactly what Getopt is
    for.
     
    James Spahlinger, Sep 2, 2009
    #2
    1. Advertising


  3. > What you want to do is exactly what Getopt is
    > for.


    So which part of "perldoc Getopt::Long" tells me how I accomplish
    point a) and b) from my question? I already used Getopt::Long before,
    so I know the basics. Reading the documentation, the 'only' limitation
    upon the argument of an option I can make, is to tell wheter it has to
    be a string, an integer, or a float. I cant say, it has to be a string
    of a certain finite set. See section "options with values".
     
    Florian Kaufmann, Sep 2, 2009
    #3
  4. [Quoting in full]
    James Spahlinger <> wrote:
    >Florian Kaufmann wrote:
    >
    >> What can Getopt::Long, or any other 'standard'

    >module offer me for
    >> this problem task:
    >>
    >> I have an option which expects a string

    >argument. But a) only a finite
    >> set of strings are to be accepted b) unique

    >abbreviations shall be
    >> accepted, e.g. if the set of valid strings is

    >("foo","bar","fly") then
    >> "fo" should stand for "foo".
    >>
    >> An example for such a use case is the --

    >backup[=CONTROL] option of
    >> many coreutils tools as for example cp, ln,

    >install. CONTROL can only
    >> be ("no","off","numbered"... and a few more).

    >What you want to do is exactly what Getopt is
    >for.


    What on earth did you do with the quoted text. You _REALLY_ should check
    your quoting style. It is absolutely impossible to tell what you were
    quoting and what you were saying.

    jue
     
    Jürgen Exner, Sep 2, 2009
    #4
  5. Florian Kaufmann

    Uri Guttman Guest

    >>>>> "TJM" == Tad J McClellan <> writes:

    TJM> Jürgen Exner <> wrote:
    >> [Quoting in full]
    >> James Spahlinger <> wrote:
    >>> Florian Kaufmann wrote:
    >>>
    >>>> What can Getopt::Long, or any other 'standard'
    >>> module offer me for
    >>>> this problem task:
    >>>>
    >>>> I have an option which expects a string
    >>> argument. But a) only a finite
    >>>> set of strings are to be accepted b) unique
    >>> abbreviations shall be
    >>>> accepted, e.g. if the set of valid strings is
    >>> ("foo","bar","fly") then
    >>>> "fo" should stand for "foo".
    >>>>
    >>>> An example for such a use case is the --
    >>> backup[=CONTROL] option of
    >>>> many coreutils tools as for example cp, ln,
    >>> install. CONTROL can only
    >>>> be ("no","off","numbered"... and a few more).
    >>> What you want to do is exactly what Getopt is
    >>> for.

    >>
    >> What on earth did you do with the quoted text. You _REALLY_ should check
    >> your quoting style. It is absolutely impossible to tell what you were
    >> quoting and what you were saying.



    TJM> But it is a pretty good bellweather of the quality of the comment
    TJM> offered...

    beyond that, you CAN do what the OP wants with getopt::long using a
    callback sub which is a closure. it would contain the hash of valid
    values as well as a ref to the variable to set. see the docs for how to
    use that. also Getopt::Euclid has code constraints that allow this very
    thing as well and it does much more including pod and usage generation
    all in one.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Sep 2, 2009
    #5
    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. Hans van Vooren

    Usage of --help and --version in getopts()

    Hans van Vooren, May 27, 2004, in forum: Perl
    Replies:
    1
    Views:
    570
    Jim Gibson
    May 27, 2004
  2. lnatz

    getopts::std

    lnatz, Jul 27, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    125
    Uri Guttman
    Jul 28, 2006
  3. vabby
    Replies:
    3
    Views:
    114
    vabby
    Jan 23, 2007
  4. vabby
    Replies:
    0
    Views:
    81
    vabby
    Jan 23, 2007
  5. is this a bug in getopts

    , Feb 7, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    131
    Brian McCauley
    Feb 7, 2007
Loading...

Share This Page