[PythonCE] Re: PythonCE Digest, Vol 21, Issue 6

Michael Murdock michaelmurdock at gmail.com
Mon Apr 25 22:24:26 CEST 2005


>It seems that there is no support for sys.argv[], so how does one add
parameters?

Somewhat related to your question, this is a really lame class that I
use. It's (over) documented below. But basically if you're writing a
program that requires passed-in arguments and you want to run it by
double-clicking on it (as opposed to running it from the command
line), then import this class and declare an instance of it. It will
pop up a Tkinter Entry box asking for your args.

One of the instance's methods returns a list of typed-in strings, the
other method returns one of the typed-in arguments but converts it to
an integer or float, if necessary.

Like I said, It's pretty lame. But I use it a lot on my Axim.

~Michael.

# ---------------------------------------------------------------------------
# cmdline.py
# Michael Murdock
#
# This code uses Tkinter's Entry box, so it imports Tkinter.
#
# Public Methods: 
#
#     GetArgs() - Returns a list of strings. Each string is one of the args.
#     GetArg(idx) - Returns the specified argument.
# 
# Here's how I use this code:
#
# 1. I import this code into my PyCE app that requires passed-in 
# command line arguments.
#
# 2. At or near the start of my program's main() I create an 
# instance of the ccmdline class and pass into the class constructor 
# my program's usage string:
#
#      cLine = ccmdline("filename numItems verboseFlag")
#
# 3. When this line of code is executed, a modal dialog box is displayed 
# asking for your program's arguments. When you enter the arguments and 
# press the 'OK' button, the dialog box will go away and the next line 
# in your program after the constructor will proceed with execution.
#
# 4. I then use one of this class's public methods, GetArgs(), which 
# returns a list with the first list element containing the program 
# name and the subsequent elements containing the white-space-delimited 
# words that were typed in the Entry box. Note that each item in this 
# list is a string. Example:
#
#      cLineList = cLine.GetArgs()
#
# 5. Alternatively, I can use the other public method, GetArg,
# specifiying a zero-based index, to request one of the typed-in
# command line arguments.
#
#      progName = cLine.GetArg(0)
#      firstArg = cLine.GetArg(1)
#
# However, the list element returned from GetArg is different from what 
# is returned with GetArgs. If the requested item is an integer (according 
# to the String method isdigit()), then that string is converted to an 
# integer before it is returned. If the requested item is a float (according
# to my own private method), then that string is converted to a float before
# it is returned.
#
# ---------------------------------------------------------------------------

from Tkinter import *
import os

# --------------------------------------------------------------------------
# ccmdline -
# A program creates an instance of this class (which uses a Tkinter Entry 
# box) if it wants to elicit its command line arguments from the program's 
# user.
#
# When the program calls this class's constructor, a modal dialog box is
# created and displayed until the user presses the OK button.
#
# I use an underscore before a variable name as a notational reminder
# to help me remember which members are private.
# --------------------------------------------------------------------------
class ccmdline:
    def __init__(self, UsageString):
        self._root = Tk()
        self._progName = os.path.basename(sys.argv[0])
        self._root.title(self._progName)
        self._frame = Frame(self._root)
        self._frame.pack()
        self._label = Label(self._frame,text=UsageString)
        self._label.pack()
        self._entry = Entry(self._frame)
        self._entry.pack()
        self._button =
Button(self._frame,text="OK",fg="red",command=self._SaveText)
        self._button.pack()
        self._root.mainloop()


    # -------------------------------------------------------
    # GetArgs - Public Method.
    # Called by client to get the argument list, including 
    # calling program, which is zeroth item in list.
    # -------------------------------------------------------
    def GetArgs(self):
        return self._args


    # ---------------------------------------------------------
    # GetArg - Public Method.
    # Called by client with a 0-based index to get one of the
    # typed-in command line arguments. If the item is an int,
    # it is converted from a string to an integer. Ditto with
    # a type-in floating point number.
    # ---------------------------------------------------------
    def GetArg(self,elementIdx):
        # Is this arg an integer?
        if self._args[elementIdx].isdigit():
            self._args[elementIdx] = int(self._args[elementIdx])
            
        # Is this arg a floating point?  
        elif self._IsFloat(self._args[elementIdx]):
            self._args[elementIdx] = float(self._args[elementIdx])
            
        return self._args[elementIdx]
        

    # --------------------------------------------------------
    # _SaveText - Private Method.
    # Called when user presses the OK button to save the
    # user-typed text string into private list, _args.
    # --------------------------------------------------------
    def _SaveText(self):
        self._args = self._entry.get().split()
        self._args.insert(0,self._progName)
        self._root.destroy()

    # --------------------------------------------------------
    # _IsFloat - Private Method.
    # Used as part of process of converting a typed-in float
    # from a string to a floating point number.
    # --------------------------------------------------------
    def _IsFloat(self, str):
        answer = False
        try:
            float(str)
            answer = True
        finally:
            return answer


More information about the PythonCE mailing list