472,799 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

optparse global

Hello,

How do i make an option passed through command line, using optparse
global. I need to import this value in another file.

This is what iam trying to do.

$python test.py -d ffffffffffff

i should be able to store 'ffffffffffff' as global so that i could
access this value in another file. If 'ffffffffffff' is not given, then
a default value stored in
parser.add_options("-d", default="01ff010BFFFF", action="store")
will be returned.

Thanks,
-Ashton

Jul 19 '05 #1
6 2587

Ashton> How do i make an option passed through command line, using
Ashton> optparse global. I need to import this value in another file.

Place the parser's output at the module scope.

global options
parser = OptionParser()
parser.add_option("-p", "--port", dest="port", default=5007,
type="int",
help="port to connect to for remote interpreter")
...
options, args = parser.parse_args()

The user of your module can then execute

from othermodule import options
print options.port

Skip
Jul 19 '05 #2
This does not seem to work. I still get the default value 5007 when i
run
$python txd.py - p5006
$python testme.py

***txd.py***

global options
parser = OptionParser()
parser.add_option("-p", "--port", dest="port", default=5007,
type="int",
help="port to connect to for remote interpreter")

options, args = parser.parse_args()

**testme.py**
from txd import options

print options.port

-Ashton

Jul 19 '05 #3

Ashton> This does not seem to work. I still get the default value
Ashton> 5007...

Hmmm... Works for me (Python from CVS):

% python testme.py
5007
% python testme.py -p 5006
5006
% python testme.py -p5006
5006

The only change from what you posted was to add

from optparse import OptionParse

to txd.py.

Skip
Jul 19 '05 #4
Thanks. It works.
-Ashton

Jul 19 '05 #5
On 16 May 2005 14:41:58 -0700, "as*****@gmail.com" <do*****@gmail.com>
declaimed the following in comp.lang.python:
Thanks. It works.
-Ashton
So far as I understand, the

global

is not needed for your example. "global <name>", when used
/inside/ a function definition, grants modify access to <name> at the
module level.

But for an "import module", you already specify as module.name
to access the module level namespace.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #6

Dennis> So far as I understand, the
Dennis> global
Dennis> is not needed for your example.

I think the original context was that optparse was being used in the context
of a function.

Skip
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that...
3
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
3
by: Karlo Lozovina | last post by:
If I create a file with only one line: --- from optparse import OptionParser --- I get this when I try to run it from "DOS" prompt: Traceback (most recent call last): File "optparse.py",...
3
by: Bob | last post by:
I'd like to setup command line switches that are dependent on other switches, similar to what rpm does listed below. From the grammar below we see that the "query-options" are dependent on the...
0
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks...
1
by: Pupeno | last post by:
Hello, I am doing some extreme use of optparse, that is, extending it as explained on http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html I have subclassed OptionParser and...
2
by: Tim Arnold | last post by:
Hi, I'm writing a command-line interface using optparse. The cli takes several options with a single action and several parameters to be used in the resulting worker classes. I've been passing...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.