473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Forcing getopt to process a specific option first??

Is there any way to force getopt to process one option first?? I'd like to be able to pass in the name of a
configuration file for my application, then have the remaining command-line parameters over-ride the configuration file
if they are present.

For the moment, I'm restricted to 2.2 versions.

TIA . . . .

Dan

Jul 18 '05 #1
4 2270
Dan Rawson wrote:
Is there any way to force getopt to process one option first?? I'd like
to be able to pass in the name of a configuration file for my application,
then have the remaining command-line parameters over-ride the
configuration file if they are present.


The easiest solution would be to make the config file the mandatory first
argument of the command line and then call

getopt.getopt(sys.argv[2:], options)

Below is another approach, that might fit your needs:

import getopt

#sample args
args = ["-c", "config.txt", "-r", "-i", "specialin.txt"]

options, args = getopt.getopt(args, "c:ri:o:")
useroptions = dict(options)

if "-c" in useroptions:

#set defaults
defaultoptions = {"-i": "defaultin.txt", "-o": "defaultout.txt"}

del useroptions["-c"]

#override defaults
defaultoptions.update(useroptions)
options = defaultoptions.items()

print options

Peter
Jul 18 '05 #2

Dan> Is there any way to force getopt to process one option first?? I'd
Dan> like to be able to pass in the name of a configuration file for my
Dan> application, then have the remaining command-line parameters
Dan> over-ride the configuration file if they are present.

Not that I'm aware of. If the order of the remaining options doesn't
matter, you might try splitting the argument list before the option which
introduces your config file. For example, given this set of command line
args:

-f bar -o baz -c configfile -o bump -h

locate the '-c' and use it to split the argument list, then swap the two
pieces:
s = "-f bar -o baz -c configfile -o bump -h"
args = s.split()
args ['-f', 'bar', '-o', 'baz', '-c', 'configfile', '-o', 'bump', '-h'] index = args.index('-c')
index 4 args[:index], args[index:] (['-f', 'bar', '-o', 'baz'], ['-c', 'configfile', '-o', 'bump', '-h']) args = args[index:] + args[:index]
args ['-c', 'configfile', '-o', 'bump', '-h', '-f', 'bar', '-o', 'baz']

Now pass that list to getopt.getopt().

If the order of the remaining arguments does matter, you just split the
argument list into three pieces, the part before rearranging:
args[:index], args[index:index+2], args[index+2:] (['-f', 'bar', '-o', 'baz'], ['-c', 'configfile'], ['-o', 'bump', '-h']) args = args[index:index+2] + args[:index] + args[index+2:]
args

['-c', 'configfile', '-f', 'bar', '-o', 'baz', '-o', 'bump', '-h']

Skip

Jul 18 '05 #3
Skip Montanaro wrote:
Dan> Is there any way to force getopt to process one option first?? I'd
Dan> like to be able to pass in the name of a configuration file for my
Dan> application, then have the remaining command-line parameters
Dan> over-ride the configuration file if they are present.

Not that I'm aware of. If the order of the remaining options doesn't
matter, you might try splitting the argument list before the option which
introduces your config file. For example, given this set of command line
args:

-f bar -o baz -c configfile -o bump -h

locate the '-c' and use it to split the argument list, then swap the two
pieces:
>>> s = "-f bar -o baz -c configfile -o bump -h"
>>> args = s.split()
>>> args ['-f', 'bar', '-o', 'baz', '-c', 'configfile', '-o', 'bump', '-h'] >>> index = args.index('-c')
>>> index 4 >>> args[:index], args[index:] (['-f', 'bar', '-o', 'baz'], ['-c', 'configfile', '-o', 'bump', '-h']) >>> args = args[index:] + args[:index]
>>> args ['-c', 'configfile', '-o', 'bump', '-h', '-f', 'bar', '-o', 'baz']

Now pass that list to getopt.getopt().

If the order of the remaining arguments does matter, you just split the
argument list into three pieces, the part before rearranging:
>>> args[:index], args[index:index+2], args[index+2:] (['-f', 'bar', '-o', 'baz'], ['-c', 'configfile'], ['-o', 'bump', '-h']) >>> args = args[index:index+2] + args[:index] + args[index+2:]
>>> args

['-c', 'configfile', '-f', 'bar', '-o', 'baz', '-o', 'bump', '-h']

Skip

Thanks! Either this solution or the one Peter Otten proposed will work . . .

Wow, two good solutions in an hour . . . .

Dan

Jul 18 '05 #4
Care for another solution?

I'd simply do two passes over the argument list. This has an advantage
over both the other alternatives proposed. First, it works if the user
writes something like "-bccf" (equivalent to the arguments ["-b", "-c", "cf"]),
and second it works with repeated options like [-a", "x", "-a", "y"].

This doesn't work if something in the configuration file could affect
the available set of commandline switches. There may be some other
shortcoming I haven't considered yet, too.

Jeff

################################################## ######################
# Program
import getopt

def main(argv):
args, rest = getopt.getopt(argv, "c:a:b")

# First pass, parse any -c option
for k, v in args:
if k == "-c":
print "Using config file", v

# Next, parse the other options
for k, v in args:
if k == "-c": continue
print "Processing switch", k, v
print "Remaining positional arguments", rest
argv = "-a x -bccf -a y ...".split()
main(argv)

################################################## ######################
# Output
Using config file cf
Processing switch -a x
Processing switch -b
Processing switch -a y
Remaining positional arguments ['...']

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Bear | last post by:
I'm stumped. Trying to follow the docs and .. failure. here's the args >>> args '-Middwb@mainex1.asu.edu -AKHAAM@prlinux+898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux...
3
by: Don Low | last post by:
Hi, I'm going over a script that demonstrates the getopt function. I include the script here: #! /usr/bin/python import sys, getopt, string
1
by: Arvind Kumar | last post by:
Hi Guys, I am summarizing this problem with a example. Appreciate if someone could help me. Say, I have a wrapper shell script which calls a perl program. I want to pass the command line...
2
by: Praveen | last post by:
Hi, Any one ever used getopt function more than once. Here is my example. I have to run a process as myProcess -x -y -c "-a -f myFile -o myOutput -d debugFile" First I have to check for -c...
4
by: pinkfloydhomer | last post by:
I want to be able to do something like: myscript.py * -o outputfile and then have the shell expand the * as usual, perhaps to hundreds of filenames. But as far as I can see, getopt can only...
1
by: Daniel Mark | last post by:
Hello all: I am using getopt package in Python and found a problem that I can not figure out an easy to do . // Correct input D:\>python AddArrowOnImage.py --imageDir=c:/ // Incorrect...
2
by: auditory | last post by:
I have sources written on linux quite long ago. They are compiled good on current linux machine. but not in VS2005. The cause of problem is #include<getopt.h>. Is there any correspoinding...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
0
by: nispe | last post by:
Hi, This is probably a really easy one but its got me; Im using getopt() and having some issues geting the specified options into a form where I can use them. Here a sample of the code Im using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.