Passing multiple arguments using the .Net StartInfo object

I'm trying to run a Python script from C# using the Process class.  Preferably, the script would contain one or more classes, each with def's.  Even without that, I would need to pass both the name of the script and a version name argument.

The closest code I can find to accomplish the latter of these possibilities is:

                    string pyscript = "../../ChangeVersion.py";
                    ProcessStartInfo sInfo = new ProcessStartInfo(@"C:\\Python26\\ArcGIS10.0\\python.exe");
                    string x = "pyscript.ToString() childVersion.VersionName";
                    sInfo.Arguments = x;
                    Process proc = new Process();
                    proc.StartInfo = sInfo;
                    proc.Start();

The Python script will further process childVersion.VersionName.

Now this won't work because both pyscript.ToString() and childVersion.VersionName are already strings.

More broadly, Im not clear how the compiler would sort out the arguments beyond the script name.  For example, suppose that my arguments statement was:
 
string x = "pyscript.ToString() myClass myDef childVersion.VersionName";

how would the program know that myClass is a class name and myDef is a method name?

Finally, what's the best way to account for gaps (e.g., "C:\\Program Files") when passing arguments?

I'm using .Net 2008 on a Windows XP SP3 OS.  The Python version is 2.6.
cshore12Asked:
Who is Participating?
 
cshore12Author Commented:
The simple way to do it: using the Process class, pass in your arguments (Process.StartInfo.Arguments) as a string separated by a space delimiter.  Then, place a calling routine for the function you're calling within the Python script itself.  The Python function will correctly identify the arguments.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I don't know Python, so I can't supply code.

Paths with spaces - typically one would enclose the path inside quote marks eg.  'C:\\program files\\my app\\xyz'

Multiple arguements.  Here you have to code something in the recipient to pull them apart, you specify some sort of separator and, optionally, some ID for the type of arguement being passed.

eg.

'First argument - second argument - third argument'   here strip out the - to leave the three arguments being passed

or

-N'name' -P'path' -X'x option'
-N'name' -X'x option' -P'path'
here your app will identify the arguements based on the ID passed as the first two chars, so it is not requiring a fixed format for the string and parameters can be optional.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You're building your arguments string wrong...

Change:

    string x = "pyscript.ToString() childVersion.VersionName";

To:

    string x = pyscript.ToString() + " " + childVersion.VersionName;

I don't understand what you want to do with "myClass" and "myDef" though.  Are those literals?...if not, can you show their variable declarations?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
cshore12Author Commented:
myClass and myDef are not literals; they refer to the class name and definition I would use in the Python script.  So, for example, my input string may include:

str x = ... + "Math" + " " + "Add"

I would like to set up the actual Python script so that it would start with:

Class Math
   def Add:

IdleMind--

1) If I use Andy's approach to provide individual IDs, how do I code the script know that these arguments represent the class and definition?
2) Alternatively, if I use the space separator as you suggest, can I refer to the argument list (e.g., argv[3], argv[4]) so that the script recognizes these arguments as class and def, respectively?

Thanks!  
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't know Python at all so I can't help you on that side.

You are correct, though, in that you either use the absolute position (index number) or have to parse each individual argument and determine what it is "tagged" as.
0
 
cshore12Author Commented:
Other commenters were only tangentially addressing the question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.