Ted Stern
unread,Jun 29, 2012, 5:07:27 PM6/29/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to argpars...@googlegroups.com
[Using argparse 1.2.1 with Python 2.6]
I have developed a semi-standard way of writing python scripts for my
non-programmer users.
I save the entire list of processed commandline arguments into a file,
with a bit of reformating so it looks like
--option1 <option1 argument>
--option2 <option2 argument>
<etc.>
Then, the next time I run the script, I process the init file using
shlex.split(), append on any new commandline arguments, and then
parse_args on the result.
This makes it easy to re-run the script as it had been run previously,
and is a record of how the script was actually called.
I'm leaving out some details, but don't worry, I have a way of
handling positional arguments ;-). That's not my problem.
The problem I'm encountering is that by doing this many times, a naive
user might enter the same argument over and over again. For example,
repeatedly running with the commandline
scriptname --config foobar
and then running
scriptname --config something_else --flag-option
would generate a file containing
--config foobar
--config foobar
--config foobar
--config foobar
--config foobar
--config foobar
--config foobar
--config something_else
--flag-option
and of course only the last --config argument would be effective, so
after
args = parser.parse_args(argv)
args would contain
args.config == 'something_else'
There's nothing wrong with keeping all that information, but if the
user is that naive, they are also likely to become confused by the
repetition.
What I would like to do is to remove redundant optional arguments by
iterating through the args in the order they were defined, finding the
option's destination name, and printing out its effective value.
In the example above, the resulting commandline arguments would look
like
--config something_else
--flag-option
Does that make sense?
I was able to achieve this some years back with optparse, but
ArgumentParser, unlike OptionParser, does not have an option_list. I
do see _optionals, but I'm not sure how to use it.
Ted
--
Frango ut patefaciam -- I break so that I may reveal