469,894 Members | 2,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,894 developers. It's quick & easy.

entering arguments at the command-line

Deathwing
Helll I am trying to figure out how to enter arguments throught he command line for a program I have created that is already working. I just want to adapt it to work at the command line. Here is the code.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. print\
  4.        """
  5.                             #---------------#
  6.                             #    RPG 1.0    #
  7.                             #---------------#
  8.  
  9. Welcome to Random_Point_Generator 1.0 (RPG 1.0),RPG is very simple to use.
  10. RPG generates a series of random points and writes these points to a .xyz
  11. file.  In order to generate these points RPG asks the user to input two sets
  12. of coordinates and the number of points the user wishs to generate.RPG first
  13. asks for an upper_left and a lower_right set of coordinates.It will then ask
  14. the user for the number of points that the they wishs it to generate.
  15. Random points will be generated within the bounds of the upper_left and
  16. lower_right coordinate values.The program terminates when the user has
  17. finished entering their xLR and yLR values.
  18.       """
  19.  
  20.  
  21.  
  22. print "\nPlease enter the upper left coordinates\n"
  23. xUL = float(raw_input("enter your x value: "))
  24. yUL = float(raw_input("enter you y value: "))
  25.  
  26. print "\nPlease enter the lower right coordinates\n"
  27. xLR = float(raw_input("enter your x value: "))
  28. yLR = float(raw_input("enter your y value: "))
  29.  
  30.  
  31. points = int(raw_input("\n\nHow many points do you want generated? "))
  32.  
  33. f = open(r'points.xyz', 'w')
  34.  
  35.  
  36. f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
  37.  
  38.  
  39. f.close()
  40.  
  41. print '\nThanks for using RPG 1.0, your "points.xyz" file should now have been created.'
  42.  
  43. raw_input("\nPress Enter to exit.")
  44.  
  45.  
I would like to make it so that the user is never asked to enter anything just so that they can enter arguments from the c:\ for example the entering of carguments would look like this

c:\2535358.68,23689.36,1423.35,8236.32,2000,points .txt

basically in the above there are six arguments entered, X and Y coordinates for the upper left value, X and Y coordinates for the lower right value, the 2000 is for the amount of pionts the user wishs to generate and the points.txt is the file they wisht to be created where these points will be stored? I have looked at some command line parsing but it's rather technical. Any help would be great thanks.
Apr 11 '07 #1
5 1626
ghostdog74
511 Expert 256MB
what you need is the sys module. example
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. arguments =  sys.argv[1:]  #get all arguments
  3. ...
  4. ..
  5.  
Apr 11 '07 #2
what you need is the sys module. example
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. arguments =  sys.argv[1:]  #get all arguments
  3. ...
  4. ..
  5.  

Thanks I should have been more specific, I have looked at the sys module, but how do I specify the arguments and how do I then run the program at the command line ?
Apr 11 '07 #3
dshimer
136 Expert 100+
I just use
Expand|Select|Wrap|Line Numbers
  1. python progname.py arg1 arg2
remember that argv[0] is the program name itself, for example
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>cat testargv.py
  2. import sys
  3. print sys.argv
  4.  
  5. C:\tmp>python testargv.py a 2 III
  6. ['testargv.py', 'a', '2', 'III']
Apr 11 '07 #4
I just use
Expand|Select|Wrap|Line Numbers
  1. python progname.py arg1 arg2
remember that argv[0] is the program name itself, for example
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>cat testargv.py
  2. import sys
  3. print sys.argv
  4.  
  5. C:\tmp>python testargv.py a 2 III
  6. ['testargv.py', 'a', '2', 'III']

Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ?


Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3.  
  4.  
  5. def arguments(x1,y1,x2,y2,points,filename):
  6.      if __name__ == '__main__':
  7.          import sys
  8.      try:
  9.          arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
  10.          except (IndexError, TypeError):
  11.              print "Usage: Too few arguments"
  12.  
  13.  
  14. f = open(r'points.xyz', 'w')
  15.  
  16. f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
  17.  
  18. f.close()
  19.  
  20. raw_input("\nFinished, Press enter to quit.")
  21.  
Apr 12 '07 #5
bartonc
6,596 Expert 4TB
Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ?
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3.  
  4.  
  5. def arguments(x1,y1,x2,y2,points,filename):
  6.      if __name__ == '__main__':
  7.          import sys
  8.      try:
  9.          arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
  10.          except (IndexError, TypeError):
  11.              print "Usage: Too few arguments"
  12.  
  13.  
  14. f = open(r'points.xyz', 'w')
  15.  
  16. f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
  17.  
  18. f.close()
  19.  
  20. raw_input("\nFinished, Press enter to quit.")
  21.  
Use the following general format
Expand|Select|Wrap|Line Numbers
  1. def aFunction(arg):
  2.     pass
  3. if __name__ == '__main__':
  4.     import sys
  5.     # try
  6.         # convert sys.argvs
  7.     # except
  8.         # whatever
  9.     aFuncion(arg)
  10.  
Apr 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Eric Chong | last post: by
1 post views Thread by TEK | last post: by
7 posts views Thread by m7b52000 | last post: by
5 posts views Thread by Kevin Walzer | last post: by
13 posts views Thread by Chris Carlen | last post: by
5 posts views Thread by kyosohma | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.