mgrangeiro has asked for the wisdom of the Perl Monks concerning the following question:

Hello there! I need to write a script to give options a,b,c to the user in a command line. Is it possible to do that? I started out by using a menu option by reading form a file and using the split function to delimit and execute commands, but that's not exactly what my boss wants. Any thoughts or helpful resources out there? Let me know. Thank you.
  • Comment on User input from a command line by giving options a,b,c

Replies are listed 'Best First'.
Re: User input from a command line by giving options a,b,c
by roboticus (Chancellor) on Apr 06, 2010 at 17:16 UTC

    mgrangeiro:

    Is it possible to do that?

    Yes.

    I started out by ... but that's not exactly what my boss wants.

    So perhaps you might describe what your boss wants, rather than having us try to guess?

    Perhaps you might try Getopts::Long?

      I thought I was clear enough that I wanted to write a script with menu options a,b,c, etc. and get the user input from a command line, but obviously I was not. I'll tell you what I did. I displayed a menu with 5 options in vim and prompted for user input. My boss said I should consider using command line so we could run the script from anywhere. Let me know if there is any other info I might be missing. Thanks for your patience. I'm a newbie with perl.
Re: User input from a command line by giving options a,b,c
by DStaal (Chaplain) on Apr 06, 2010 at 17:52 UTC

    I think you don't really mean 'in a command line', but more 'through a text menu', if I understand you correctly. (At least, not through the normal understanding of 'command line', whether it is accurate or not.)

    Take a look at Term::UI, Term::Menu, and IO::Prompt. I think those do what you are looking for.

      As a text menu through a command line. Is it possible? Thanks

        Why do you think DStaal offered the observation and provided those links?

        Do we do it for you? That's a different question.

        You mean have the script display the menu before it is run? Not really.

Re: User input from a command line by giving options a,b,c
by toolic (Bishop) on Apr 06, 2010 at 17:16 UTC
    Getopt::Long is a standard way of parsing command line options in Perl.
Re: User input from a command line by giving options a,b,c
by cdarke (Prior) on Apr 06, 2010 at 19:28 UTC
    It is really quite easy, and you would get the most from this forum if you tried to figure it out for yourself by reading the links. However, I will give you a start using Getopt::Std, which has less features than Getopt::Long but might be enough. It can be used in a couple of different ways, in this example we get the options set on the command-line into a hash:
    use warnings; use strict; use Getopt::Std; my %options; getopts('abc', \%options); while (my ($key, $value) = each %options) { print "Option: $key, value: $value\n" } print "Remaining arguments: @ARGV\n";
    Here is a selection of command-line calls and results:
    C:\>gash.pl -ac Option: c, value: 1 Option: a, value: 1 Remaining arguments: C:\>gash.pl -ab fred jim Option: a, value: 1 Option: b, value: 1 Remaining arguments: fred jim C:\>gash.pl -c -a fred jim Option: c, value: 1 Option: a, value: 1 Remaining arguments: fred jim