perl - how to use getopt__long method_ - stack overflow.pdf

3
 11/11/ 2014 per l - How t o use Getopt:: Long method? - Stack Overfl ow ht tp://stackoverflow.com/questi ons/3148574/how-to-use-getopt l ong-met hod 1/ 3 Take the 2-minute tour  × Ether 34.8k 9 51 122 Tree 1,397 7 36 63 3 Answers How can I use Getopt::Long method if the input command execution is like this: $ testcmd -option check ARG1 ARG2 ARG3 or $ testcmd ARG1 ARG2 ARG3 perl  getopt-long edited Jun 30 '10 at 16:14 asked Jun 30 '10 at 11:04 2  That depends on what you wa nt to have done with those options and flags. The people who answer below are trying to make their best guess, but mind-reading is a difficult skil l to master. – Ether  Jun 30 '10 at 16:15   A quick e xample: #! /usr  /bin/perl use warnings; use strict; use Getopt::Long; sub usage { "Usage: $0 [--option=VALUE] ARG1 ARG2 ARG3\n"  } my $option = "default"; GetOptions("option=s" , \$option)  or die usage; die usage unless @ARGV == 3; print "$0: option=$option: @ARGV\n" ; Getopt::Long  is quite flexible in what it will accept: Stack Overflow is a question and answer site for professional and enthusiast programmer s. It's 100% free, no registration required. How to use Getopt::Long method? sign up log in tour help stack overflow careers  

Upload: gaurav-panwar

Post on 07-Oct-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 11/11/2014 perl - How to use Getopt::Long method? - Stack Overflow

    http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method 1/3

    Take the 2-minute tour

    Ether34.8k 9 51 122

    Tree1,397 7 36 63

    3 Answers

    How can I use Getopt::Long method if the input command execution is like this:

    $ testcmd -option check ARG1 ARG2 ARG3

    or

    $ testcmd ARG1 ARG2 ARG3

    perl getopt-long

    edited Jun 30 '10 at 16:14 asked Jun 30 '10 at 11:04

    2 That depends on what you want to have done with those options and flags. The people who answer below aretrying to make their best guess, but mind-reading is a difficult skill to master. Ether Jun 30 '10 at 16:15

    A quick example:

    #! /usr/bin/perl

    use warnings;use strict;

    use Getopt::Long;

    sub usage { "Usage: $0 [--option=VALUE] ARG1 ARG2 ARG3\n" }

    my $option = "default";

    GetOptions("option=s", \$option) or die usage;

    die usage unless @ARGV == 3;

    print "$0: option=$option: @ARGV\n";

    Getopt::Long is quite flexible in what it will accept:

    Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.

    How to use Getopt::Long method?

    sign up

    log in

    tour

    help

    stack overflow careers

    add a comment

    http://stackoverflow.com/http://stackoverflow.com/tourhttp://stackoverflow.com/users/40468/etherhttp://stackoverflow.com/users/352860/treehttp://stackoverflow.com/questions/tagged/perlhttp://stackoverflow.com/questions/tagged/getopt-longhttp://stackexchange.com/https://stackoverflow.com/users/signup?returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f3148574%2fhow-to-use-getoptlong-methodhttps://stackoverflow.com/users/login?returnurl=http%3a%2f%2fstackoverflow.com%2fquestions%2f3148574%2fhow-to-use-getoptlong-methodhttp://stackoverflow.com/tourhttp://careers.stackoverflow.com/?utm_source=stackoverflow.com&utm_medium=site-ui&utm_campaign=anon-topbar

  • 11/11/2014 perl - How to use Getopt::Long method? - Stack Overflow

    http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method 2/3

    Greg Bacon53.5k 11 111 175

    Zaid21.5k 6 44 99

    $ ./cmdUsage: ./cmd [--option=VALUE] ARG1 ARG2 ARG3

    $ ./cmd 1 2 3./cmd: option=default: 1 2 3

    $ ./cmd --option=foo 4 5 6./cmd: option=foo: 4 5 6

    $ ./cmd -option=bar 7 8 9./cmd: option=bar: 7 8 9

    $ ./cmd -option check a b c./cmd: option=check: a b c

    edited Jun 30 '10 at 11:38 answered Jun 30 '10 at 11:11

    You need to enable the pass_through option. Documentation quoted below:

    pass_through (default: disabled)

    Options that are unknown, ambiguous or supplied with an invalid option value are passed through in@ARGV instead of being flagged as errors. This makes it possible to write wrapper scripts thatprocess only part of the user supplied command line arguments, and pass the remaining options tosome other program.

    If require_order is enabled, options processing will terminate at the first unrecognized option, ornon-option, whichever comes first. However, if permute is enabled instead, results can becomeconfusing.

    DVK's posted an example of how to do this in another answer. I'd upvote his answer first if you find ituseful.

    edited Jun 30 '10 at 11:19 answered Jun 30 '10 at 11:12

    +1 for recognizing that values starting with "-" need to be passed through DVK Jun 30 '10 at 13:28

    add a comment

    add a comment

    http://stackoverflow.com/users/123109/greg-baconhttp://stackoverflow.com/users/133939/zaid

  • 11/11/2014 perl - How to use Getopt::Long method? - Stack Overflow

    http://stackoverflow.com/questions/3148574/how-to-use-getoptlong-method 3/3

    Rukmangathan1

    #!/usr/bin/perl

    use Getopt::Long;

    my $cla = GetOptions ( "one=s" => \$one, "two=s" => \$two, "three=s" => \$three, "help|h|?" => \$help,) or usage ();

    if ($help) { usage ();}

    my $first = $one;my $second = $two;my $third = $three;

    printf ("%-7s %-9s %-7s\n", $first, $second, $third);

    sub usage { print "\n$0\t" . "[ -one -two -three ]\n\n"; exit (0);}

    answered Nov 3 '13 at 2:52

    Maybe you should add some sort of explanation to your code so it can be more useful. Meryovi Nov 3 '13 at14:25

    Not the answer you're looking for? Browse other questions tagged perl getopt-long or

    ask your own question.

    add a comment

    http://stackoverflow.com/users/2949082/rukmangathanhttp://stackoverflow.com/questions/tagged/perlhttp://stackoverflow.com/questions/tagged/getopt-long