optparse options

I

icarus

Why do I need to put two options for this script to print the path?

if I just specify the option and argument...
$ python <script>.py -p xxxx
Usage: <script>.py [-p dir] [--part=dir]

<script>.py: error: No options specified

So I need to give it two arguments...
$ python <script>.py --part xxxx y
xxxx


Desired output:
$ python <script>.py --part xxxx
xxxx

$ python <script>.py --part xxxx y
Usage: <script>.py [-p dir] [--part=dir]
<script>.py: error: some message


----
#/usr/bin/python

import optparse

def main():
parser = optparse.OptionParser(usage="%prog [-p dir] [--part=dir] ",
version="%prog 1.0")

parser.add_option( "-p", "--part", dest="directory",
help="process target directory", metavar="dir")
(options, args) = parser.parse_args()

if len(args) != 1:
parser.error("No options specified")


path = options.directory
print path

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

Robert Kern

Why do I need to put two options for this script to print the path?

if I just specify the option and argument...
$ python<script>.py -p xxxx
Usage:<script>.py [-p dir] [--part=dir]

<script>.py: error: No options specified

So I need to give it two arguments...
$ python<script>.py --part xxxx y
xxxx

You have this piece of code:

if len(args) != 1:
parser.error("No options specified")

The args list contains all of the arguments after the options. Unlike sys.argv,
it does not contain the executable's name in the first slot.

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

Mike Kazantsev

Ben said:
icarus said:
parser = optparse.OptionParser(usage="%prog [-p dir] [--part=dir] ",
version="%prog 1.0")

parser.add_option( "-p", "--part", dest="directory",
help="process target directory", metavar="dir")
(options, args) = parser.parse_args()
...
if len(args) != 1:
parser.error("No options specified")

The message is confusing, since it doesn't match the condition; it would
be correct to say “Did not specify exactly one non-option argumentâ€.

In this case, it looks like you don't want to check this at all, and
should instead operate on the basis of the options only.

I also wanted to note that it looks quite illogical and
counter-intuitive to create "required options", since by definition they
should be optional.
Try using arguments instead, with some type-switching flags, if
necessary - it should make CLI more consistent and save some typing by
omitting otherwise always-required option argument ("--part").

--
Mike Kazantsev // fraggod.net


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAkoTt1IACgkQASbOZpzyXnEYRwCgkMwKuOnkbrdIr3MSVo24YEcp
eyoAn27se+NLYO8OC8nYLDwg/o6SWS3V
=vWBf
-----END PGP SIGNATURE-----
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top