Dan
Dan

Reputation: 45752

sys.argv vs argparse to get data from C# into python via command line arguments

I need to call a python script from C#, I'll be doing it like this:

   System.Diagnostics.Process process = new System.Diagnostics.Process();
   System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
   startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
   startInfo.FileName = "cmd.exe";
   startInfo.Arguments = "/C python \"C:\\working\\projects\\python tests\\Sample Data.py\"";
   process.StartInfo = startInfo;
   process.Start();

but I now need to add in command line arguments. I'm trying to decide between just using sys.argv which seems very simple to implement or to go with argparse. If I will always pass one and only one parameter (a date), is there any advantage to using argparse?

Additional info regarding the problem (slightly tangential to the question):

The date I'm passing is a parameter for a SQL query. I could instead run this query in C# (which I would prefer) but then I will need to pass the result to python via command line arguments which seems to me to be a terrible idea but maybe this is something argparse can handle? The table has two date columns and 4 float columns. Can such a table be passed this way?

The reason I am calling python via cmd.exe and not using IronPython is (A) I only need to pass information once from C# to Python so the communication between the two is very limited and (B) the result of which is a 3D surface plot generated by mplot3d which seems like a huge hassle to make work in IronPython (which actually generally confuses me anyway), so if I am just passing the single date then this doesn't seem unreasonable. But if I could pass that entire table easily, either by a command line argument or else some other not overly complicated method, I would be very interested in hearing how.

Upvotes: 0

Views: 1018

Answers (1)

John Mee
John Mee

Reputation: 52313

Since you're a nice clean slate of knowledgeless bliss, learn argparse. It's piss-easy and replaces sys.arg which is now considered old, archaic, and (probably) deprecated; although you'll find it far more common, for now, because it's been around since Guido was a baby.

Upvotes: 1

Related Questions