in reply to Getopt::Long

I don't quite understand, is your shell also written in perl? And it executes other perl scripts inside of that?

If that's the case, you can tell your perl script that v = view, a = add, etc, etc. (Actual details of code left up to you).

I could be wrong, but this doesn't sound like a Getopt::Long problem at all, since you actually want to run a different command based on character entry, not do different things with the same program based upon the parameters passed to it.

If the shell is smart enough, (or your OS), why not just use aliases?

HTH,
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Re: Getopt::Long
by hotshot (Prior) on Dec 16, 2002 at 08:15 UTC
    first, my shell is also written in perl, and it's executing functions depends on the command the user enters, the function behaves according to the options to the command. Like Getopt::Long doesn't build aliases for the options or telling the script that p = percentage, a = active ... I don't won't to do that either. I just wondered whether I can combine somehow the module to help me with that, since it does what I want with the options (never said that it's a problem with the module).

    UPDATE:
    this is an example to what I want:
    # suppose I have the above commands (view, add, edit, set ,show) # since no command except 'add' starts with the letter 'a', I want tha +t both will work myshell> add -name test -value 40 # first myshell> a -name test -value 40 # second # in the GetOptions() function, I only have for the 'add' command, the + followings: if ($command eq 'add') { if (GetOptions(\%hash, 'name=s', 'value=i')) { &doSomething(); } } elsif ($command eq 'edit') { ... } ...
    Currently I identify that 'add' was called and the first if is processed with it's options. $command is NOT an option.
    I hope that now it's clearer. Hotshot
      But how are you looking for input in your interactive shell? Expect? (Which would be the logical choice).

      IMHO, Getopt::Long is not made for what you're trying to do. Make aliases for whatever your shell is expecting. Use a hash as a lookup table or something.
      # Not tested, or even sure if it makes sense. %commands = ( 'add' => '&add', 'view' => '&view', 'delete' => '&delete', 'a' => '\$commands{add}', 'v' => '\$commands{view}', 'd' => '\$commands{delete}', );
      Cheers,
      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
      Well then, the "cheap" way to do it is:
      if ($command eq 'add' || $command eq 'a') { if (GetOptions(\%hash, 'name=s', 'value=i')) { &doSomething(); } } elsif ($command eq 'edit' || $command eq 'e') { ... }
      Of course, I think a better way to do it would be something like the following. (Note: My understanding of perl's refrences lacks something and this should be taken with a grain of salt).
      # not tested %commands = ( 'add' => '&add', 'a' => '\$command{add}', 'view' => '&view', 'v' => '\$command{view}', ); while (<STDIN>) { # or whatever $command{$_}; # or is it '\$command{$_}' ? ... } sub add { GetOptions(\%hash, 'name=s', 'value=i'); # your code here } sub view { GetOptions( ... ); # your code here }
      Cheers,
      ibanix

      $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
        nice approach although not generic since each time I'll add a new command to my shell, I'll have to update %commands carfully - check first letters of each command. quite a mess if I have lots of commands, and anyhow, my list of commands is generated according to the menu I'm in (my shell is quite big and menus oriented). the available commands list is generated on each TAB key pressing (for tab completion), so I'll have to think maybe how to generelize your suggestion. I think it's suits smaller and less complicated systems then I got here, but thanks for the effort.

        Hotshot