optparse question

P

Pat

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".
 
J

James Mills

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()
 
R

Robert Kern

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
 
M

Matimus

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
 
J

John Machin

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/optparse.html#handling-boolean-flag-options
you had trouble with.


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
 
T

Thorsten Kampe

* 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
 
P

Pat

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.
 
P

Pat

Thorsten said:
* 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.
 
J

John Machin

Thorsten said:
* 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."

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).
 
T

Thorsten Kampe

* 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
 
P

Pat

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).

Sigh. I used an incorrect example (I sincerely apologize to the world
for that egregious error on my part).

I''m totally cognizant that the documentation states '-'' or '--' need
to be used for flags.

The question was it possible to add a simple flag like 'd-' to optparse
with no other parameters? I'm guessing from the vitriolic diatribes
here that the answer is no.

To those who wrote that no one uses command line windows and no one uses
'dir /s /b' command is totally irrelevant. I'm writing a proprietary
program used solely by my company. Not everyone writes programs for the
same target audience as you do. The program is written in Python on
Ubuntu.

If you don't want to answer the question or don't have any meaningful to
add, please don't pollute the forum with further vacuous responses. I
didn't ask, or expect, you to write the code for me.

christ on a stick, so many of you behave like prima donnas.
 
P

Peter Otten

Pat said:
The question was it possible to add a simple flag like 'd-' to optparse
with no other parameters?

Do you mean "d-" or "-d"? If the latter, what's wrong with Robert Kern's
answer?

Peter
 
S

Steve Holden

Peter said:
Do you mean "d-" or "-d"? If the latter, what's wrong with Robert Kern's
answer?
I guess it got crowded out by the prima donnas anxious to help Pat
phrase his/her question better ...

regards
Steve
 
P

Pat

Peter said:
Do you mean "d-" or "-d"? If the latter, what's wrong with Robert Kern's
answer?

Peter

I mean "-d" since that's what Unix commands expect for flags.

My sole intention for the "-d" was to put my program into debug mode so
that while I'm using the Wing IDE, I can debug my program using just
one child process (instead of dozens) without having to change a
constant within my program. I haven't figured out a way to debug
multiple child processes in Wing. But, I confess, I haven't been very
vigorous in learning how.

My other intention was to learn the optparse module just for the sake of
learning.
 
T

Thorsten Kampe

* Pat (Tue, 27 Jan 2009 14:04:28 -0500)
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).

Sigh. I used an incorrect example (I sincerely apologize to the world
for that egregious error on my part).

I''m totally cognizant that the documentation states '-'' or '--' need
to be used for flags.

The question was it possible to add a simple flag like 'd-' to optparse
with no other parameters? I'm guessing from the vitriolic diatribes
here that the answer is no.
[...]
If you don't want to answer the question or don't have any meaningful
to add, please don't pollute the forum with further vacuous responses.
I didn't ask, or expect, you to write the code for me.

christ on a stick, so many of you behave like prima donnas.

You have a problem. The problem is not a Python or a technical one. It's
described here ->
http://en.wikipedia.org/wiki/Attention_Deficit_Syndrome

Thorsten
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top