473,473 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getopt and options with multiple arguments

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 get one argument
with each option. In the above case, there isn't even an option string
before the *, but even if there was, I don't know how to get getopt to
give me all the expanded filenames in an option.

Help! :)

/David

Dec 19 '05 #1
4 5655
On 19 Dec 2005 02:29:41 -0800, pi************@gmail.com
<pi************@gmail.com> wrote:
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 get one argument
with each option. In the above case, there isn't even an option string
before the *, but even if there was, I don't know how to get getopt to
give me all the expanded filenames in an option.

Help! :)


You could use the glob module to expand the asterisk yourself.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Dec 19 '05 #2
PoD
On Mon, 19 Dec 2005 02:29:41 -0800, pi************@gmail.com wrote:
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 get one argument
with each option. In the above case, there isn't even an option string
before the *, but even if there was, I don't know how to get getopt to
give me all the expanded filenames in an option.

Help! :)

/David


The convention is to put options first and then any other stuff such as
filenames:-
myscript.py -o outputfile *

In this case getopt will return the options as one list and the rest as
another list.

Dec 19 '05 #3
On Mon, 19 Dec 2005 02:29:41 -0800, pi************@gmail.com wrote:
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.
If you are calling this from most Linux and Unix shells, the shell will
already have expanded the * and myscript.py will never see the asterisk.

But as far as I can see, getopt can only get one argument
with each option.
The getopt module has two main functions you probably want to call.

getopt.getopt expects options like -o arg followed by any remaining args.

getopt.gnu_getopt allows mixed args and options.

But frankly, the easiest way of dealing with your problem is just to
change your user-interface. What is wrong with using

myscript.py -o outputfile *

instead?
In the above case, there isn't even an option string
before the *, but even if there was, I don't know how to get getopt to
give me all the expanded filenames in an option.


If you use a shell that doesn't expand the filenames, you can use the glob
module to expand it yourself.

--
Steven.

Dec 19 '05 #4
On Mon, 19 Dec 2005, pi************@gmail.com wrote:
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 get one argument
with each option. In the above case, there isn't even an option string
before the *, but even if there was, I don't know how to get getopt to
give me all the expanded filenames in an option.


I'm really surprised that getopt doesn't handle this properly by default
(so getopt.getopt mimics unices with crappy getopts - since when was that
a feature?), but as Steven pointed out, getopt.gnu_getopt will float your
boat.

I have an irrational superstitious fear of getopt, so this is what i use
(it returns a list of arguments, followed by a dict mapping flags to
values; it only handles long options, but uses a single dash for them, as
is, for some reason, the tradition in java, where i grew up):

def arguments(argv, expand=True):
argv = list(argv)
args = []
flags = {}
while (len(argv) > 0):
arg = argv.pop(0)
if (arg == "--"):
args.extend(argv)
break
elif (expand and arg.startswith("@")):
if (len(arg) > 1):
arg = arg[1:]
else:
arg = argv.pop(0)
argv[0:0] = list(stripped(file(arg)))
elif (arg.startswith("-") and (len(arg) > 1)):
arg = arg[1:]
if (":" in arg):
key, value = arg.split(":")
else:
key = arg
value = ""
flags[key] = value
else:
args.append(arg)
return args, flags

def stripped(f):
"""Return an iterator over the strings in the iterable f in which
strings are stripped of #-delimited comments and leading and
trailing whitespace, and blank strings are skipped.

"""
for line in f:
if ("#" in line): line = line[:line.index("#")]
line = line.strip()
if (line == ""): continue
yield line
raise StopIteration

As a bonus, you can say @foo or @ foo to mean "insert the lines contained
in file foo in the command line here", which is handy if, say, you have a
file containing a list of files to be processed, and you want to invoke a
script to process them, or if you want to put some standard flags in a
file and pull them in on the command line. Yes, you could use xargs for
this, but this is a bit easier. If you don't want this, delete the elif
block mentioning the @, and the stripped function. A slightly neater
implementation not involving list.pop also then becomes possible.

tom

--
Hit to death in the future head
Dec 21 '05 #5

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

Similar topics

1
by: grandeandy | last post by:
question: in the code below, and in general, how would I input multiple functions into the code, and get multiple outputs? basically, I want to be able to add say 2 or 3 of the div's to the link so...
1
by: JS | last post by:
I have this form in test.jsp, that consists of 3 drop/down menus (sel1, sel2 and sel3): <body onload="createFirstMenu('sel1');"> <form name="sels" method="post" action=""> <select...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
1
by: Shaun Jackman | last post by:
I'd like to call getopt with one set of arguments, and once that's completely done call it again with an entirely different set of arguments. I've found though that at least with the getopt...
1
by: CroDude | last post by:
Hi all! Please help me with this, I'm stuck here. I have a command line .exe file which needs a few arguments passed to do it's job. In help there's a example like this: ...
8
by: Ritesh Raj Sarraf | last post by:
Hi, I'm having some minor problems with optparse. I'm just worried that someone shouldn't say that multiple argument feature isn't implemented in optpartse. How to tackle multiple arguments...
1
by: ashokbio | last post by:
How to return multiple values by passing multiple arguments in function or subroutine using VB6? T. Ashok Kumar
1
by: Ben Warren | last post by:
Hello, Let's say I have a function with a variable number of arguments(please ignore syntax errors): def myfunc(a,b,c,d,...): and I have a tuple whose contents I want to pass to the...
0
by: yshali | last post by:
Hi, I am trying to write sql for passing multiple arguments through a function and handle NULL conditions ? here is the part of the query which throws error: ... and a.itemnum in...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.