-2

well I'm brand new to Python and I'm really struggling to solve the following puzzle. Namely, I'm writing a script that's supposed to take and process arguments from the command line in the following different cases:

--X x1 x2 x3

--X range xmin xmax

First command should do all the calculations in points x1, x2, x3. And when a user issues the second command it should do the same calculations in [Xmin, Xmax]. And "range" is a control word here implying that the calculations will be done in range. X is parameter's name.

Namely, when I issue the first command, my script should calculate I don't really know how to go about it, everything I tried didn't work the way I want it to...

Any help would be appreciated! Thank you!

PinguinoNero
  • 123
  • 1
  • 6

1 Answers1

1

Here's a simple example, showing how to obtain the command line arguments. The code is simple; I named my file so.py

import sys
print "arguments:", sys.argv

Now, when you execute with command-line arguments, sys.argv is a list of all the arguments:

$ python2.7 so.py --X 5 7 9
arguments: ['so.py', '--X', '5', '7', '9']

You can manipulate this just as any other list -- but I strongly recommend that you not alter the list. Access the elements as needed, copy it to another list, etc. -- but do not change the original.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I do know how to work with the parser. But what I want is nothing like what you wrote. As you can see, I've got two optional arguments, one of them is --X and the second one is "range" which is actually a command name. So in the first case everything is easy: parser.add_argument('--x', ......,nargs= '*'), that would allow a user to enter any number of points "x", but what if he wanted to make calculations in the range , say, from Xmin to Xmax. – PinguinoNero Jan 18 '16 at 23:36
  • You haven't yet posted your non-working code, as required by the StackOverflow guidelines. Where are you stuck? Clarify your question. Briefly, you check the first argument after **--X**: if it's "range", then you loop from Xmin through Xmax; otherwise, you iterate through the given list of values. – Prune Jan 19 '16 at 00:30