Another newbie getopt thread. ^_^

Discussion in 'Python' started by googleboy, Jul 17, 2004.

  1. googleboy

    googleboy Guest

    Hiya!

    I discovered getopt tonight after getting halfway through writing huge
    amounts of looping statements to try to ensure that text entered as
    parameters to my python app are within the bounds of what is allowed.

    It all worked great until I realised that I don't know how to force
    people to enter digits only as parameters where appropriate.

    I tried doing an if statement to qa the warning value, but it didn't
    seem to like what I was doing:



    try:
    opts, args = getopt.getopt(sys.argv[1:], "w:c:t:Vh",
    ["warning=", "critical=", "timeout=", "version"])
    except getopt.GetoptError:
    print usage
    sys.exit(2)
    for opt, arg in opts:
    if opt == "-h":
    print intro + preamble + usage + options + bugs +
    query
    sys.exit()
    elif opt in ("-V", "--version"):
    print "Plugin\nVersion number 0.2\n(c)Googleboy"
    sys.exit()
    elif opt in ("-w", "--warning"):
    if arg in string.whitespace != 0:
    print "Warning and critical values must be
    between 0 and 100\n"
    sys.exit()
    else:
    warning = int(arg)
    print "warning is set to ", warning
    elif opt in ("-c", "--critical"):
    critical = int(arg)
    print "critical is set to ", critical
    elif opt in ("-t", "--timeout"):
    timeout = int(arg)



    What I was trying begins to confuse me too... If I make that If
    statement work, will warning be defined as a variable of type integer
    for me to look at later in the script? I didn't use the library's
    example of a main function because I thought that would be a problem.

    In other news, despite reading the
    http://docs.python.org/lib/module-getopt.html site, I get an error
    whenever I try to make the -h variable have a --help option. It
    points an error to the : on the line where I use identical syntax to
    all the other iterations and says Syntax Error: invalid token.

    Any assistance on running a check and being able to make a graceful
    call to sys.exit("You need to make the value of WARNING and CRITICAL
    between 0 and 100.") or something that will pring out a moderately
    okay error message, or the : syntax error would make me be very
    happy.

    Thanks for your attention,

    Googleboy.


    (PS sorry if this isn't too clear - it is after 8am here and I been up
    all night staring at this. Like I said, I am a n00b. And I haven't
    done any real programming prior to this, so I don't really know what
    the heck to do now)
    googleboy, Jul 17, 2004
    #1
    1. Advertising

  2. googleboy

    Chris Cioffi Guest

    Re: Another newbie getopt thread. ^_^

    On 17 Jul 2004 15:06:25 -0700, googleboy <> wrote:
    > Hiya!
    >
    > I discovered getopt tonight after getting halfway through writing huge
    > amounts of looping statements to try to ensure that text entered as
    > parameters to my python app are within the bounds of what is allowed.

    [snip]

    Look at the optparse module.
    http://www.python.org/doc/2.3.4/lib/module-optparse.html

    It allows you to define the parameter type, making it far easier to do
    any followup validation. As a bonues, it provides automatic support
    for stuff like 'program --help'.

    Chris
    --
    Still searching for an even prime > 2!
    Chris Cioffi, Jul 17, 2004
    #2
    1. Advertising

  3. googleboy

    googleboy Guest

    Re: Another newbie getopt thread. ^_^

    Chris Cioffi <> wrote in message news:<>...
    > On 17 Jul 2004 15:06:25 -0700, googleboy <> wrote:
    > > Hiya!
    > >
    > > I discovered getopt tonight after getting halfway through writing huge
    > > amounts of looping statements to try to ensure that text entered as
    > > parameters to my python app are within the bounds of what is allowed.

    > [snip]
    >
    > Look at the optparse module.
    > http://www.python.org/doc/2.3.4/lib/module-optparse.html
    >
    > It allows you to define the parameter type, making it far easier to do
    > any followup validation. As a bonues, it provides automatic support
    > for stuff like 'program --help'.



    Thanks for the tip. I am using 2.2 here, but a bit of research
    pointed me off to optik.sourceforge.net and it is all beginning to
    really come together now.

    There is one smallish issue I am having with getopt just at the
    moment, and that is with this bonus feature of automatic support for
    -h, --help. I have defined a whole bunch of things (like bugs, my
    email address, a preamble explaining the app's function) which I want
    to print to the screen when someone requests help, not just an
    obvious usuge and options definition. I think it is probably
    fortunate that I have done it as 5 smaller strings instead of one huge
    one, just in case there was a time when I wanted to use only a
    subsection (like when someone puts in a bad value, I will print a
    small error referring to the incorrect option, and the syntax example
    only.)

    The docs and examples don't make it clear how to do this, and when I
    defined a -h, --help option it gave me a conflict error:

    optik.errors.OptionConflictError: option -h/--help: conflicting option
    string(s): -h, --help

    Is there anyway around this?

    Finally, a quick one... Is there a way for optparse to force an
    integer to be between 0 and 100? I am passing in percentage
    values.... It isn't hard to make a validation on the integers
    afterwards, but I thought it might be nice to have this as part of
    the optparse.

    Thanks for the help!

    googleboy.
    googleboy, Jul 18, 2004
    #3
  4. googleboy

    John Lenton Guest

    Re: Another newbie getopt thread. ^_^

    On 18 Jul 2004 00:53:16 -0700, googleboy <> wrote:
    > The docs and examples don't make it clear how to do this, and when I
    > defined a -h, --help option it gave me a conflict error:
    >
    > optik.errors.OptionConflictError: option -h/--help: conflicting option
    > string(s): -h, --help
    >
    > Is there anyway around this?


    OptionParser has a add_help_option that defaults to 1; also, a 'usage'
    parameter that you can override the message that appears _before_ the
    options.}

    And you can always subclass OptionParser...

    > Finally, a quick one... Is there a way for optparse to force an
    > integer to be between 0 and 100? I am passing in percentage
    > values.... It isn't hard to make a validation on the integers
    > afterwards, but I thought it might be nice to have this as part of
    > the optparse.


    doesn't make_option help you do this?

    --
    John Lenton () -- Random fortune:
    bash: fortune: command not found
    John Lenton, Jul 18, 2004
    #4
  5. Re: Another newbie getopt thread. ^_^

    On 18 Jul 2004, googleboy wrote:

    > There is one smallish issue I am having with getopt just at the
    > moment, and that is with this bonus feature of automatic support for
    > -h, --help. I have defined a whole bunch of things (like bugs, my
    > email address, a preamble explaining the app's function) which I want
    > to print to the screen when someone requests help, not just an
    > obvious usuge and options definition. I think it is probably
    > fortunate that I have done it as 5 smaller strings instead of one huge
    > one, just in case there was a time when I wanted to use only a
    > subsection (like when someone puts in a bad value, I will print a
    > small error referring to the incorrect option, and the syntax example
    > only.)


    Using % substitution should work for this:

    help = '%s %s %s\n%s\n%s' % (appname,version,build,author,usage)

    or, if these things are defined in a dictionary:

    help = '%(appname)s %(version)s %(build)s\n%(author)s\n%(usage)s' % helpinfo
    Christopher T King, Jul 19, 2004
    #5
  6. googleboy

    googleboy Guest

    Re: Another newbie getopt thread. ^_^

    Christopher T King <> wrote a very useful response
    which said:

    > On 18 Jul 2004, googleboy wrote:


    > Using % substitution should work for this:


    > help = '%s %s %s\n%s\n%s' % (appname,version,build,author,usage)



    > or, if these things are defined in a dictionary:


    > help = '%(appname)s %(version)s %(build)s\n%(author)s\n%(usage)s' % helpinfo



    Thanks for the tip! It came a few hours too late to help me. I
    actually ended up defining callbacks to execute a function whenever it
    detects a call to help ( -h or --help given as a parameter).


    The only issue I have with optparse now came up in final QA testing,
    and it isn't a huge problem. Just *might* be a big enough issue to
    keep teh guys I submit it to from using it... Optparse seems to
    ignore any parameters that haven't been defined by anything. I can
    put any sort of spurious characters I like after the command name
    interspersed amongst my real and valid switches, it performs exactly
    as though they were not there. Is there any way for optparse to
    automatically bail out in such cases? I haven't found anything to
    help me in the docs, nor the help list archive.

    Thanks again!!!

    ^_^

    googleboy
    googleboy, Jul 22, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sayoyo
    Replies:
    3
    Views:
    1,090
    sayoyo
    Feb 16, 2004
  2. Nafai
    Replies:
    2
    Views:
    378
  3. d
    Replies:
    0
    Views:
    1,120
  4. Mustafa Cayci

    Newbie -> NameError: getopt

    Mustafa Cayci, Apr 4, 2011, in forum: Python
    Replies:
    1
    Views:
    533
    R. Tyler Croy
    Apr 4, 2011
  5. Replies:
    3
    Views:
    96
    Dave Angel
    Jan 8, 2014
Loading...

Share This Page