Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > optparse question

Reply
Thread Tools

optparse question

 
 
Pat
Guest
Posts: n/a
 
      01-27-2009
Up until today, I never needed to pass any arguments to a Python program.

I did all the requisite reading and found that I should use optparse
instead of getopt. I read the documentation and since the words
"simple" and "easy" often appeared in the examples and documentation, I
just knew that it would be a snap to implement.

Problem is that all I wanted to do was pass a one flag to the program
"-d", for to enable debug mode. Several hours later I gave up after
optparse complained about every variation I tried.

What does it take to pass single parameter to a program?
http://docs.python.org/library/optparse.html stated that programs always
have options. Is that so? What about "dir /s"?

getopt resolved my immediate need, but I would like to know how one
could use optparse to extract out the options from something like "dir
/s /b".

 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      01-27-2009
On Tue, Jan 27, 2009 at 11:02 AM, Pat <> wrote:
(...)

> What does it take to pass single parameter to a program?
> http://docs.python.org/library/optparse.html stated that programs always
> have options. Is that so? What about "dir /s"?


Sample code:

----------------------------------------
#!/usr/bin/env python

"""optexample

Example of using optparse
"""

import os
import sys
import os.path
import optparse

__version__ = "0.1"

USAGE = "%prog [options] <arg>"
VERSION = "%prog v" + __version__

def parse_options():
"""parse_options() -> opts, args

Parse and command-line options given returning both
the parsed options and arguments.
"""

parser = optparse.OptionParser(usage=USAGE, version=VERSION)

parser.add_option("-v", "--verbose",
action="store_true", default=False, dest="verbose",
help="Verbose output during operation.")

opts, args = parser.parse_args()
if len(args) < 1:
parser.print_help()
raise SystemExit, 1

return opts, args

def main():
opts, args = parse_options()

if opts.verbose:
print args[0]

if __name__ == "__main__":
main()
----------------------------------------

cheers
James
 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      01-27-2009
On 2009-01-26 19:02, Pat wrote:
> Up until today, I never needed to pass any arguments to a Python program.
>
> I did all the requisite reading and found that I should use optparse
> instead of getopt. I read the documentation and since the words "simple"
> and "easy" often appeared in the examples and documentation, I just knew
> that it would be a snap to implement.
>
> Problem is that all I wanted to do was pass a one flag to the program
> "-d", for to enable debug mode. Several hours later I gave up after
> optparse complained about every variation I tried.


parser = optparse.OptionParser()
parser.add_option('-d', '--debug', action='store_true')

options, args = parser.parse_args()
if options.debug:
# Do debugging stuff.
else:
# Do non-debugging stuff.

> What does it take to pass single parameter to a program?
> http://docs.python.org/library/optparse.html stated that programs always
> have options. Is that so? What about "dir /s"?


Can you quote exactly the part that you are talking about? I don't see any such
claim.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Matimus
Guest
Posts: n/a
 
      01-27-2009
> I did all the requisite reading and found that I should use optparse
> instead of getopt. * I read the documentation and since the words
> "simple" and "easy" often appeared in the examples and documentation, I
> just knew that it would be a snap to implement.


I don't know where you got that. 'getopt' works just fine. 'optparse'
works fine too. I don't think anybody is going to get too worked up
over which you decide to use for such a simple case.

Matt
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-27-2009
On Jan 27, 12:02*pm, Pat <P...@junk.net> wrote:
> Up until today, I never needed to pass any arguments to a Python program.
>
> I did all the requisite reading and found that I should use optparse
> instead of getopt. * I read the documentation and since the words
> "simple" and "easy" often appeared in the examples and documentation, I
> just knew that it would be a snap to implement.
>
> Problem is that all I wanted to do was pass a one flag to the program
> "-d", for to enable debug mode. *Several hours later I gave up after
> optparse complained about every variation I tried.
>
> What does it take to pass single parameter to a program?


I'm assuming that question 2 starts here. To help answer question 1
without just writing the code for you, it might help if you (a) showed
what you regard as your best effort (b) explained what part of
http://docs.python.org/library/optpa...n-flag-options
you had trouble with.


> http://docs.python.org/library/optparse.htmlstated that programs always
> have options.


Does it?

> *Is that so? *What about "dir /s"?


That has one option, /s.

And this must be question 3:

>
> getopt resolved my immediate need, but I would like to know how one
> could use optparse to extract out the options from something like "dir
> /s /b".


If you mean with "/" as the option designator instead of "-": there
doesn't appear to be a documented way of doing it. You would have to
do some social engineering on the users to get them used to doing "dir
-s -b". In any case I thought the number of Windows users who know how
to fire up a Command Prompt window was diminishingly small ... you
actually have users who know how to use commands like "dir /s /b"?

Cheers,
John
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      01-27-2009
* Pat (Mon, 26 Jan 2009 20:02:59 -0500)
> Up until today, I never needed to pass any arguments to a Python
> program.
> [...]
> getopt resolved my immediate need, but I would like to know how one
> could use optparse to extract out the options from something like "dir
> /s /b".


If you actually read the documentation (it's right at the top) you knew
that this is not possible:
"There are many different syntaxes for options; the traditional Unix
syntax is a hyphen (“-“) followed by a single letter [...] The GNU
project introduced "--" [...] These are the only two option syntaxes
provided by optparse.
Some other option syntaxes that the world has seen include:
[...]
a slash followed by a letter, or a few letters, or a word, e.g. "/f",
"/file"

These option syntaxes are not supported by optparse, and they never will
be. This is deliberate: [...] the last only makes sense if you’re
exclusively targeting VMS, MS-DOS, and/or Windows."

Thorsten
 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      01-27-2009

>
> If you mean with "/" as the option designator instead of "-": there
> doesn't appear to be a documented way of doing it. You would have to
> do some social engineering on the users to get them used to doing "dir
> -s -b". In any case I thought the number of Windows users who know how
> to fire up a Command Prompt window was diminishingly small ... you
> actually have users who know how to use commands like "dir /s /b"?
>


I used dir /s /b as a trivial Windows example. I use Windows for
personal use but Ubuntu for work and programming.

Personally, I use dir /s /b all the time on Windows since the /b option
finds files *much* faster; maybe 10x or 100x faster but I didn't get out
a stopwatch.
 
Reply With Quote
 
Pat
Guest
Posts: n/a
 
      01-27-2009
Thorsten Kampe wrote:
> * Pat (Mon, 26 Jan 2009 20:02:59 -0500)
>> Up until today, I never needed to pass any arguments to a Python
>> program.
>> [...]
>> getopt resolved my immediate need, but I would like to know how one
>> could use optparse to extract out the options from something like "dir
>> /s /b".

>
> If you actually read the documentation (it's right at the top) you knew
> that this is not possible:
> "There are many different syntaxes for options; the traditional Unix
> syntax is a hyphen (“-“) followed by a single letter [...] The GNU
> project introduced "--" [...] These are the only two option syntaxes
> provided by optparse.
> Some other option syntaxes that the world has seen include:
> [...]
> a slash followed by a letter, or a few letters, or a word, e.g. "/f",
> "/file"
>
> These option syntaxes are not supported by optparse, and they never will
> be. This is deliberate: [...] the last only makes sense if you’re
> exclusively targeting VMS, MS-DOS, and/or Windows."
>
> Thorsten


Sigh. I used dir /s /b as a simple Windows command with a flag (it
could have been dir /s) because it was the first thing that popped into
my mind.

I had no idea people were going to get so upset that I used a Windows
example and go off on a tear.
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-27-2009
On Jan 28, 12:06*am, Pat <P...@junk.net> wrote:
> Thorsten Kampe wrote:
> > * Pat (Mon, 26 Jan 2009 20:02:59 -0500)
> >> Up until today, I never needed to pass any arguments to a Python
> >> program.
> >> [...]
> >> getopt resolved my immediate need, but I would like to know how one
> >> could use optparse to extract out the options from something like "dir
> >> /s /b".

>
> > If you actually read the documentation (it's right at the top) you knew
> > that this is not possible:
> > "There are many different syntaxes for options; the traditional Unix
> > syntax is a hyphen (“-“) followed by a single letter [...] The GNU
> > project introduced "--" [...] These are the only two option syntaxes
> > provided by optparse.
> > Some other option syntaxes that the world has seen include:
> > [...]
> > a slash followed by a letter, or a few letters, or a word, e.g. "/f",
> > "/file"

>
> > These option syntaxes are not supported by optparse, and they never will
> > be. This is deliberate: [...] the last only makes sense if you’re
> > exclusively targeting VMS, MS-DOS, and/or Windows."

>
> > Thorsten

>
> Sigh. *I used dir /s /b as a simple Windows command with a flag (it
> could have been dir /s) because it was the first thing that popped into
> my mind.
>
> I had no idea people were going to get so upset that I used a Windows
> example and go off on a tear.


Nobody is upset, and nobody has "gone off on a tear". The point about
the "Windows example" is that the docs say in a close-to-screamingly-
obvious manner that /options are not supported, no matter what
religion uses them. It was not, and still is not, apparent what you
really wanted. We're all patiently waiting for you to rephrase the
question(s).
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      01-27-2009
* John Machin (Tue, 27 Jan 2009 05:31:42 -0800 (PST))
> Nobody is upset, and nobody has "gone off on a tear". The point about
> the "Windows example" is that the docs say in a close-to-screamingly-
> obvious manner that /options are not supported, no matter what
> religion uses them. It was not, and still is not, apparent what you
> really wanted. We're all patiently waiting for you to rephrase the
> question(s).


Amen, brother.

Thorsten
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse question, passing unknown flags to subprocess Joseph Garvin Python 0 05-20-2009 09:50 PM
Retry:Question about optparse/OptionParser callback. Steven W. Orr Python 3 02-11-2007 06:01 AM
Question about optparse/OptionParser callback. Steven W. Orr Python 0 02-09-2007 04:45 PM
An optparse question T Python 8 07-21-2006 11:13 PM
optparse question GMTaglia Python 5 09-19-2004 08:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57