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 -- ?

baduker
  • 19,152
  • 9
  • 33
  • 56
naseefo
  • 720
  • 1
  • 9
  • 30
  • Can't have spaces in arguments, see https://stackoverflow.com/questions/11894815/specifying-arguments-with-spaces-for-running-a-python-script – frozen Mar 10 '18 at 21:35

3 Answers3

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))
dursun
  • 1,861
  • 2
  • 21
  • 38
  • I don't see how this is relevant to my problem? Could you enlighten me? – naseefo Mar 10 '18 at 21:45
  • 2
    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. – dursun Mar 10 '18 at 21:48
  • Thank you! Let me go through it in detail. – naseefo Mar 10 '18 at 21:49
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.

Mick_
  • 131
  • 9
  • 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" – naseefo Mar 10 '18 at 21:46
  • Updated as per your requirement. – Mick_ Mar 10 '18 at 21:54
  • 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. – naseefo Mar 10 '18 at 22:04
  • No problem. I think i got your comment wrong and provided wrong solution anyway. Glad you got it to work. – Mick_ Mar 10 '18 at 22:07
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
Sean Breckenridge
  • 1,932
  • 16
  • 26