473,408 Members | 1,874 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,408 software developers and data experts.

command line arguments

TMS
119 100+
Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo, xHi, eps) assignment I've been working on.

I have the header that looks like this:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

Thank you
Jan 24 '07 #1
4 2154
bvdet
2,851 Expert Mod 2GB
Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo, xHi, eps) assignment I've been working on.

I have the header that looks like this:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

Thank you
This works at the windows command prompt:

C:\>python find_root.py x**2-4 3 0 0.0001

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Jan 24 '07 #2
bartonc
6,596 Expert 4TB
This works at the windows command prompt:

C:\>python find_root.py x**2-4 3 0 0.0001

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
is not quite complete because errors will be thrown when too few arguments a sent from the command line or if those arguments can't be converted to float. Most command line scripts will provide help when this happens. Elaborate on this idea:

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     try:
  6.         find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  7.     except (IndexError, TypeError):
  8.         print "Usage: Too few arguments"
  9.  
It's a little nicer than showing a python traceback.
Jan 25 '07 #3
TMS
119 100+
Thank you! Good information.

TMS
Jan 25 '07 #4
bvdet
2,851 Expert Mod 2GB
I have a suggestion for a slight improvement. If you find that you are entering in the same value for 'eps' consistently, add a default to the function argument list:
Expand|Select|Wrap|Line Numbers
  1. def find_root(f, xLo, xHi, eps=0.00001):
  2. ..................
  3. ..................
Then the function call could be:
Expand|Select|Wrap|Line Numbers
  1. find_root('5*x**3+2*x**2-x+1', -3, 4)
  2. # if you need a value for eps different from the default
  3. find_root('5*x**3+2*x**2-x+1', -3, 4, .001)
Jan 25 '07 #5

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

Similar topics

6
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
6
by: Jon Hewer | last post by:
hi i am writing a little script and currently implementing command line arguments following the guide by mark pilgrim from dive into python; ...
7
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
1
by: Rune Jacobsen | last post by:
Hi, I've been trying to figure this one out, but my experience just doesn't have what it takes... :| I am writing an application that reads an XML file and displays the contents in various...
4
by: Roland | last post by:
Hi, I am developing a C++ project and want to pass some command line arguments in VS .NET 2003. I am in debug mode, the configuration is set to Debug and I entered my argument list in Project ->...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
2
by: Milan | last post by:
Hi, Please guide me how to set command line argument and how to retrive command line argument. Senario: vb.net application should be able to execute from command prompt by passing login and...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
0
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...
0
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,...
0
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...

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.