0

I want to parse the command line arguments while attempting to call the program in the following manner:

python plot.py --w --Develop origin plot --+3.5

I have been using sys.argv to parse them using a for loop:

for arg in sys,argv:
    print(arg)

Output:

plot.py
--w
--Developoriginplot
--+3.5

But I wish to get the output as follows:

plot.py
w
Develop origin plot
+3.5

Is there a way to split the line by specifying the delimeter -- ?

1

3 Answers 3

1

" ".join() the args first, then split by --.

import sys

args = " ".join(sys.argv) # join by spaces
args = [a.strip() for a in args.split("--")] # split by --, remove any extra spaces
print(args)
print("\n".join(args))

Output:

$ python plot.py --w --Develop origin plot --+3.5
['plot.py', 'w', 'Develop origin plot', '+3.5']
plot.py
w
Develop origin plot
+3.5
Sign up to request clarification or add additional context in comments.

1 Comment

@seam Thank you so much! I wish I could up vote this twice!
1

you need to use the argparser

and here is a sample code of usage:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                const=sum, default=max,
                help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

3 Comments

I don't see how this is relevant to my problem? Could you enlighten me?
you are trying to manage command line aguments to your application, right? and you are doing it manually; however the argparser is the one for this purpose, and if you read the documentation you will find the answer.
Thank you! Let me go through it in detail.
1

You can use split() and replace() functions. split() takes delimiter as an argument and replace takes two arguments - first one is the character you would like to replace (in your case the white space) and the second one is what you would like to replace it with.

#Your string
s = "--w --Develop origin plot --+3.5"

d = s.replace(' ','').split('--')[1:] 
print(d)

>>['w', 'Developoriginplot', '+3.5']

Then you can reference your arguments by the indices of this list.

Hope this helps.

4 Comments

Yes, split method can be used to break it down at the de-limited but you will notice that I get my desired output "Develop orign plot" as "Desiredorigin plot"
Updated as per your requirement.
Thank you Mick! But the thing is that I am parsing this from the argument line and the line is coming as a single string unless we add double quotes at beginning and end of the entire line. But had given a good work around for it.
No problem. I think i got your comment wrong and provided wrong solution anyway. Glad you got it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.