473,890 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

command line arguments

TMS
119 New Member
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 2186
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Expert
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 New Member
Thank you! Good information.

TMS
Jan 25 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
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
2619
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 prompt but how can i achieve it in an application in VS.NET? Thank you
6
628
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; http://diveintopython.org/scripts_and_streams/command_line_arguments.html thats all fine, however i am not sure of the BEST way to handle multiple command line arguments
7
4743
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 following for the command-line arguments: java -jar sforcedataloader.jar -Dsalesforce.config.dir=CONFIG_DIRECTORY They also give an example:
2
4209
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 how to use Command-Line Arguments inside C program.
1
7910
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. I have to do it this way as the consumer of the service should not be able to change the argument - except by uninstalling and reinstalling the service. I created the service and the service itself works great. However, when I try to install...
1
2463
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 ways to the end user. This works fine. My challenge lies in the fact that these XML files are generated by various (third party) applications. Which application generates them depends on the user, the country they are in, their personal preferences,...
4
1994
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 -> Project Properties -> Configuration Properties -> Debugging -> Command Arguments. The blurb provided in the properties window for this field reads "The command line arguments to pass to the application." which sounded promising.
40
2768
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 name with a space in it) because that filename gets split into 2. I tried
2
2766
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 password and should be able to execute process form (with his parameter) User should not be able to see GUI
7
9382
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 display the Windows form. The form is being displayed but the command only returns when the form is closed. I want the command line to return immediately, leaving the form displayed.
0
9975
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
11212
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...
0
10799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
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
9614
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
8004
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
7154
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
5832
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
6032
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.