I want to write a Python application that "wraps" a command line utility, passing through all parameters intact, except for the parameters that I choose to modify.
I imagine there's an easy way to do this in Python but there's any number of ways to make this really hard.
Is there a "right" way to do this? Any suggestions valued. I don't want anyone to write the code for me, just comment on an effective approach. Thanks!
Using Python 3
-
Calling an external command in PythonRobert Harvey– Robert Harvey2016年08月10日 23:47:25 +00:00Commented Aug 10, 2016 at 23:47
-
1@RobertHarvey - there's alot more to this problem than calling the command.Duke Dougal– Duke Dougal2016年08月10日 23:52:18 +00:00Commented Aug 10, 2016 at 23:52
-
Yes... You need to accept command line parameters in your Python executable, change the ones that need changing, and then pass those parameters to your command line utility using the technique in the linked post.Robert Harvey– Robert Harvey2016年08月10日 23:55:17 +00:00Commented Aug 10, 2016 at 23:55
1 Answer 1
Read the parameters from sys.argv, and pass them to subprocess.call() (or perhaps one of the other functions in subprocess). subprocess.call() takes a list to specify the command to run and it's parameters, which is a good fit for sys.argv.
Could be as simple as this, in case there's no need to check, process or filter the parameters:
#!/usr/bin/env python3
import subprocess
import sys
subprocess.call(sys.argv[1:])
Called as e.g. wrap.py ls -l
, it will execute ls -l
.