Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Parsing command line options without knowing what they are

by DrWhy (Chaplain)
on Nov 25, 2010 at 19:41 UTC ( [id://873716]=note: print w/replies, xml ) Need Help??


in reply to Re: Parsing command line options without knowing what they are
in thread Parsing command line options without knowing what they are

Thanks, I may use some of the ideas in here for my own parser.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on Re^2: Parsing command line options without knowing what they are

Replies are listed 'Best First'.
Re^3: Parsing command line options without knowing what they are
by BrowserUk (Patriarch) on Nov 25, 2010 at 19:48 UTC

    YW. I later looked inside some of the existing getopts modules to see what other rules they implement, and boy are they ever complicated. Reverse engineering the rules from them would be a nightmare.

    Here's a slightly cleaner refactoring of the above:

    sub getOpts { my %opts; my @toDelete; for( my $i=0; $i < @ARGV; ++$i ) { $_ = $ARGV[ $i ]; if( m[^-(.)(.*)$] ) { my( $first, $rest ) = ( $1, $2 ); push @toDelete, $i; next unless defined $first; if( $first ne '-' ) { $opts{ $_ } = 1 for $first, split'',$rest; next; } last if $rest eq ''; if( $ARGV[ $i+1 ] !~ m[^-] ) { $opts{ $rest } = $ARGV[ ++$i ]; push @toDelete, $i; } if( $rest =~ m[^no(.+)$] ) { $opts{ $1 } = 0; } else { $opts{ $rest } = 1; } } } splice @ARGV, $_, 1 for reverse @toDelete; return %opts; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Nice. Especially nice since I've been doing alot of refactoring like this the last few weeks on code written by much less experienced Perl programmers. I'm curious about this line, though:
      next unless defined $first;
      I don't think it's ever possible for $first to be undefined. If the regex in your if matches, the first capture will always have to be one character long, right? Never undefined?

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        I don't think it's ever possible for $first to be undefined

        You're right of course++

        My only excuse is that it is an artifact of the iterative development process I went through as I tried to handle each different case. I think the test made sense at some point; but then again, maybe not :)

        During the refactoring, I just attempted to remove some levels of nesting by moving things around, without breaking anything. I didn't actually consider why each condition was there at that point. It just highlights the value of peer review :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://873716]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found