http://www.perlmonks.org?node_id=893505


in reply to Re^2: Dynamic option
in thread Dynamic option

It is hard to give an answer without knowing your validation logic needs, whether or not -nc/-nd can themselves have values and so on. I use Perl 5.8.8 and 5.10.0. Given the features available to me, I'd do one of the following:

(a) No validation needed: I'd just define the whole lot (-nd, -na, -nc) as options. That way you can easily test for existence like you are doing in your sample code. Also you can easily assign values. Just because something is in the option list doesn't mean that the user has to enter it.

(b) Validation needed. There are a couple possibilities here. I'm still guessing at what you want though because I'm not sure of the meaning of the intial -input [-nd -na]. Are those defaults? Also I don't know which sub-sub-options (nd/na) can be applied to more than one sub-option(data/modem/app). Nor do I know if na/nd can have values of their own.

If I were to (a) want to set defaults for data/modem/app (b) distinguish between -nd for data vs. -nd for modem, I might do something like this:

But I might do something entirely different if -na, -nc, -nd had values of their own. It just isn't possible to answer your question without knowing more. Command line options are data. Anything more complicated than a simple list needs to be understood and analyzed like a mini-data model. Structure of any data depends on its functional dependencies (how many values can an option have, what are its constraints, what are the interdependencies among options, and so on). The possibilities are quite broad which is why you aren't likely to find some out of the box solution except for the most simple cases.

When you've written up your more detailed explanation of your validation logic, I'd recommend, you update your original post with a what you wrote above plus the new validation information. I'd recommend you reply to your own original post with a comment rather than this one. (Anonymous monks can't edit their posts). I think that would be better than a reply to this comment. That way people can see your needs without having to burrow down into replies of replies. You are liable to get more on target answers that way.

In the future, also please consider posting questions as a named user. You can edit your posts that way. In general, the ideal solution when an initial post is unclear is to update the initial post, but that option isn't available to you as an anonymous poster.

Update - changed recommendation - anonymous monks can't update posts.

Replies are listed 'Best First'.
Re^4: Dynamic option
by Anonymous Monk on Mar 16, 2011 at 06:45 UTC
    To explain again ,the ones in "[]" are optional sub-options(they may or may not present always) to the values of "input"option, data,modem,apps are values to option "input",they can also change.
    EXAMPLE:- findfiles -input [-nd -na ] data [-nc -nd] modem apps -des "finding fi +les" -r 1000 Basically ,pseudco code of what I want to achieve is below.Really appr +eciate if someone can give me the perlversion of the code. for each "input option value" { call function A if not exits nd call function B if not exits na call function C if not exists nc }

      So? If the value is optional, tell Getopt::Long it's optional.

      --input --input=nd --input=na --input=nd,na
      As the author of Getopt::Long always says, options are optional, or they wouldn't be options. So the "they may or may not be present" isn't anything special. In fact, it's the bread and butter of Getopt::Long - it only deals with things that may or may not be present.

      If you make the call syntax like:

      findfiles --input data modem apps --nd --na --nc --des "..." -r 1000
      Something like:
      GetOptions 'input=s{0,}' => \my @input, 'na' => \my $na, 'nd' => \my $nd, 'nc' => \my $nc, 'des=s' => \my $des, 'r=i' => \my $r;
      Should do the trick.

        That's not what the OP wants. He wants to be able to specify 'nd' for -input (or not), and again for data (or not).

        Your solution only permits specifying it once (although it could be changed to counted), and it's not associated with "input" or "data".