[Tutor] Making a better main() : Critique request

John Fouhy john at fouhy.net
Mon Aug 21 03:04:36 CEST 2006


On 21/08/06, Tony Cappellini <cappy2112 at gmail.com> wrote:
> 1. check the number of arguments as well as checking that each argument was
> valid.
> 4. If the argument validation failed, a Usage() function would be called to
> show all possible arguments
> 5. Provide a "help" option  to display all the possible arguments.

Hi,

I haven't read your whole post, I'm afraid, but I did see that you
were using getopt.

If you use optparse instead, it will take care of all this for you.

Basically, you create a parser, and tell it what arguments you are
expecting, including:
 - short name
 - long name
 - help text
 - argument type (int / string / float / bool)
 - default value

If you give your script the -h or --help option, optparse will
generate usage text (which you can customise).  Likewise, if you give
it an unknown argument, or an argument of the wrong type.

I've only just started playing with it myself, but it looks pretty nifty :-)

-- 
John.


More information about the Tutor mailing list