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

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


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

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:
    635
    Jim Gibson
    May 27, 2004
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    971
    Eric Sosman
    Jul 8, 2003
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,576
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    696
    santosh
    Jul 24, 2007
  5. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    1,241
    Peter Nilsson
    Jan 15, 2008
  6. veryhotsausage
    Replies:
    1
    Views:
    2,353
    veryhotsausage
    Jul 4, 2008
  7. Oliver Graeser
    Replies:
    10
    Views:
    819
    Oliver Graeser
    Sep 26, 2008
  8. Perl Learner
    Replies:
    1
    Views:
    316
    Jim Keenan
    Jun 13, 2005
Loading...