HidyHo, folks...

More of an 'approach' question, than specific to Perl...

I've been building a LOT of programs in recent years that use command line switches for some inputs... but I'm now getting to a bit more of a complex situation... and I'm not sure about the 'best' (standard?) way to deal with it.

Firstly, I now have programs that may need to process many command line switches. The actual switches are simple enough (for example, "-v" for "verbose logging" or "-t 10" for "timeout time") ...but many of the switches are mutually exclusive... so how to process/validate these items!?

Initial thoughts include simply setting 'flag variables' for each switch... and I set the flag if I see it when dealing with the command line... and I later have a function that simply does all the ANDing and ORing etc to see what flags are set/clear.

Alternatively, I sometimes have set-up a bitfield to represent the state of each switch... and as I find a switch on the command line, I set that bit in a 'status value'... and I then have something that checks for 'magic numbers' (ooo, bad!) that represent the invalid combinations of bits... but maybe there's a better/simpler (or more standardized) way?

The other issue is that I have a lot of functionality in some of these programs... and to select the particular action I want to perform is getting a bit ugly/hairy to deal with and I could, conceivably, end-up dealing with too many command line switches for the "-f value" -style of parameter (which I would like to maintain -- I don't want to have to deal with the complexities of weirdo syntaxes like "-p input='a,b'-12'" ...!)

For now, I'm simply using a construct like "-a 1" to say "run action 1" and I explain the actions/numbers/functionality in the help output (which is getting to be more than a couple of paragraphs of explanation... Ugh).

As usual, I'm building these Perl programs under Win32 (maybe to run under Win64, too?) and Linux, using v5.16.x, v5.20.x and beyond.

Many thanks for any thoughts...


In reply to How do I process many (conflicting) command line parameters? by ozboomer

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.