473,573 Members | 3,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

entering arguments at the command-line

Deathwing
32 New Member
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,2 3689.36,1423.35 ,8236.32,2000,p oints.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 1761
ghostdog74
511 Recognized Expert Contributor
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
Deathwing
32 New Member
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 Recognized Expert New Member
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
Deathwing
32 New Member
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 Recognized Expert Expert
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
7157
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 Service in MMC. Is there any way to get arguments using this option? If possible, what framework method should I use to get an arguement? Thanks in...
1
2542
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 now, a lot more in the future. I think I have a pretty good understanding about how the command pattern is tought to work, however during...
7
4193
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 slider was moved. It is proving most difficult in Python. How do I pass the .get() values to my calculating function? Do I use the command...
12
5161
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
5994
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| gpg.exe --yes --output c:\working\inbound\test.txt --passphrase 0 -d c:\working\inbound\anstest.gpg
3
2262
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 have input the wrong arguments. How can I write text to the dos prompt windows if user input the wrong arguments? Thanks in advance. Steven.
5
2548
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', command=self.authorizeCommand(self.scanPackages)) When I start my program, it crashes because it's trying to run the command self.authorizeCommand. The reason I'm structuring it in this...
13
4475
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 command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
4
11172
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 defined. How do I enter a pause between lines of code? Since I'm very new to visual basic, I would like to be able to follow things along on the screen...
5
4488
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 someone clicks on someone's email link in a web page. While this works great if I create a convoluted path command as the registry entry and pass in...
0
7783
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8029
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5293
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2222
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.