473,614 Members | 2,076 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 1763
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
7165
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 advance. Eric
1
2546
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 implementation I'm htting a couple of quite large design considerations that I'm not sure I have the answare...
7
4197
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 option for each slider? e.g command = Calc(a.get()). Obviously not cos it doesn't work. I am not a...
12
5165
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
5997
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
2263
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
2551
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 fashion is that this command takes another command as an argument--in this case, self.ScanPackages.
13
4486
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
11175
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 without them whizzing right by.Thank you!
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 the email argument to the script, it doesn't work at all if I turn my python file into an exe using...
0
8182
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8279
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7093
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4052
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.