473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Similar topics

5
by: Eric Chong | last post by:
I created a Windows Service in C# that requires to get passed command arguments like a Console App. I noticed that there is an option "Start parameters" text box in the property of a Windows...
1
by: TEK | last post by:
Hello I'm wondering if anyone out there might give some input/suggestions/viewpoints around the Command pattern. In my case, the number one priority for using the pattern is undo support. Some...
7
by: m7b52000 | last post by:
Some time ago I wrote a little program in Tcl/Tk that took the values from 3 sliders and performed a calculation using these values. The calculation was of course automatically repeated each time a...
12
by: Phoe6 | last post by:
The Program Fragment is this: int choice; /* Users Input Command */ .. .. .. printf("Enter a command: "); fflush(stdin); choice = getc(stdin); printf("\n");
6
by: PAPutzback | last post by:
The process and execute methods want a path to the executable otherwise they kick out a file not found. So how can I execute the following. It works fine from a command window. echo password|...
3
by: Steven | last post by:
I am using VC++ 6.0 to build a windows based application, however, my application allows user to input command arguments in the dos prompt windows. I want to have a warning message for user if they...
5
by: Kevin Walzer | last post by:
I'm having difficulty structuring a Tkinter menu entry. Here is the command in question: self.finkmenu.add_command(label='Update List of Packages',...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
4
by: jhatch2007a | last post by:
I have just started learning visual basic 2005. I tried entering the command: Wait(1000) in order for the program to wait for 1 second before proceeding, but it rejected this saying Wait wasn't...
5
by: kyosohma | last post by:
I have created what amounts to a simple GUI email sending program using Python + wxPython. I have modified the mailto registration in the Windows Registry so that it launches the script when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.