getops not working

A forum for general discussion of the Python programming language.

getops not working

Postby swlinn » Thu May 02, 2013 3:37 pm

I have two laptops, both with Python 2.5 installed. One is XP where I have limited permissions, the others is a Win 7 platform where I have full admin rights. I have code similar to the tutorial example, eg,

Code: Select all
import getopt
import sys

version = '1.0'
verbose = False
output_filename = 'default.out'

print 'ARGV      :', sys.argv[1:]

options, remainder = getopt.getopt(sys.argv[1:], 'o:v', ['output=',
                                                         'verbose',
                                                         'version=',
                                                         ])
print 'OPTIONS   :', options

for opt, arg in options:
    if opt in ('-o', '--output'):
        output_filename = arg
    elif opt in ('-v', '--verbose'):
        verbose = True
    elif opt == '--version':
        version = arg

print 'VERSION   :', version
print 'VERBOSE   :', verbose
print 'OUTPUT    :', output_filename
print 'REMAINING :', remainder


This code works perfectly on my Win 7 full admin rights laptop, but on my laptop where I have limited permissions, testing the above code gives me zero command line options. Would my windows permissions be causing this issue? I thought I'd tested this on that laptop successfully in the past when I also had admin rights, so the permissions is the only thing I can think of that would cause it to fail. Has anyone run into this?

Thanks,
Steve
swlinn
 
Posts: 2
Joined: Thu May 02, 2013 2:10 pm

Re: getops not working

Postby gratuitous_arp » Sun May 05, 2013 5:14 pm

If it's useful, this is what my box does with your script (Linux, Python 2.7):

> python getopt_test.py -o outputname leftover
ARGV : ['-o', 'outputname', 'leftover']
OPTIONS : [('-o', 'outputname')]
VERSION : 1.0
VERBOSE : False
OUTPUT : outputname
REMAINING : ['leftover']

Since you are using the tutorial code it looks like you are trying to learn a way to parse command line arguments (maybe as part of some larger project). Consider using optparse or argparse instead of getopt. In Python 2.5 optparse is part of the standard library. Argparse is the preferred method in 2.7 and up, but if you want to use it in 2.5 then you'd need to download it because it's not in 2.5's standard library. Either of these other libraries will make life easier for you because you don't have to parse arguments by hand as with getopt, and learning them would be far more useful to you because getopt is unused in modern code.

This does a reasonable job of explaining the progression from getopt to optparse to argparse:

http://ttboj.wordpress.com/2010/02/03/g ... -argparse/
gratuitous_arp
 
Posts: 6
Joined: Tue Apr 16, 2013 11:10 pm

Re: getops not working

Postby swlinn » Tue May 14, 2013 2:04 pm

You're correct in that I have a program I wrote years ago that would either take command line args or would pop up a dialog. I normally use the dialog, but was surprised recently when the code ignored my command line args and would continue to popup the dialog. That's when I went to the tutorial code just as a simplified test case as it does pretty much what my code is doing with respect to the command line parsing, and when I run it on my Win 7 full admin rights laptop, I get the same results as you did ...

C:\>testcli.py -o outputname leftover
ARGV : ['-o', 'outputname', 'leftover']
OPTIONS : [('-o', 'outputname')]
VERSION : 1.0
VERBOSE : False
OUTPUT : outputname
REMAINING : ['leftover']

However, when I run the exact tutorial code on my Win XP environment, which I am positive worked in the past with my program, but now I get this for the tutorial ...

C:\ >testcli.py -o outputname leftover
ARGV : []
OPTIONS : []
VERSION : 1.0
VERBOSE : False
OUTPUT : default.out
REMAINING : []

OS differences aside, given I'm sure the command line was working in the past on the failing laptop, is that I now no longer have admin rights. Of course, their could have been other things with the OS that could have changed without my knowing with automated updates happening, etc.

Just scratching my head as to what the root cause is.

Regards,
Steve
swlinn
 
Posts: 2
Joined: Thu May 02, 2013 2:10 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests