To the OP: Although below I'll show a way to put multiple commands on one command line, I actually don't necessarily recommend this, nor would I recommend building a monolithic "one script to do it all", instead follow stevieb's and Athanasius's advice to modularize. One element of the Unix philosophy is to make each tool "Do One Thing and Do It Well". If your scripts need to share common functions, then do that through a module, rather than shoving all the code into one script.

You can not get command line options twice within the same program.

That statement is just a little too absolute for my tastes, it's totally possible to get too clever with @ARGV :-)

use warnings; use strict; use Data::Dump; use Getopt::Std; use Getopt::Long; # fake command line local @ARGV = qw/ one -abc -- two -d -e -- three -abcdef -- four --foo --bar --bar --bar -- five --foo --quz baz /; while (@ARGV) { my $cmd = shift @ARGV; getopts('abcdef', \my %o); dd $cmd, \%o; last if $o{f}; } while (@ARGV) { my $cmd = shift @ARGV; GetOptions(\my %o, 'foo!', 'bar+', 'quz!'); dd $cmd, \%o; last if $o{quz}; } dd @ARGV; __END__ ("one", { a => 1, b => 1, c => 1 }) ("two", { d => 1, e => 1 }) ("three", { a => 1, b => 1, c => 1, d => 1, e => 1, f => 1 }) ("four", { bar => 3, foo => 1 }) ("five", { foo => 1, quz => 1 }) "baz"

Also, it's possible to read options from strings, such as the lines of STDIN. Getopt::Long has GetOptionsFromString, and with Getopt::Std it's possible with a little trick.


In reply to Re^2: Wrap multiple programs by haukex
in thread Wrap multiple programs by jnarayan81

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.