Hans
Hans

Reputation: 591

getting command line arguements from optparser without using argv[x]

so I'm trying to add functionality to a script which takes a variable number of command-line arguments, for example:

python -u hi.py ENTRY_METHOD /onefolder/ /twofolder/ a b c d e f

or

python -u hi.py ENTRY_METHOD /onefolder/ /twofolder/ a b c d

So the argv[s] are like so:

argv[0] == hi.py
argv[1] == ENTRY_METHOD
argv[2] == /onefolder/
argv[3] == /twofolder/
argv[4] == a
argv[5] == b
argv[6] == c
argv[7] == d

I need to use optparser (i know, old and deprecated).

I would like to make this work like so:

python -u hi.py ENTRY_METHOD -q /onefolder/ /twofolder/ a b c d

where -q does what I need to but doesn't effect the argv[s] value (ie. d will continue to equal argv[7] and because it's a variable number -q wouldn't count as argv[8] given the command:

python -u hi.py ENTRY_METHOD /onefolder/ /twofolder/ a b c d -q

does anyone have a suggestion as to how I can accomplish this?

Any help is greatly appreciated!

Upvotes: 0

Views: 44

Answers (1)

MegaIng
MegaIng

Reputation: 7886

You can just reset sys.argv:

argv = list(sys.argv)
argv.remove("-q")
sys.argv = tuple(argv)

Upvotes: 1

Related Questions