AlanGhalan
AlanGhalan

Reputation: 396

How to open a CLI program in python and enter a command(Mac OSX)?

I have a Mac program with a command line interface that I am trying to automate from Python. So far I have:

os.system("cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt;")

Which runs the CLI. Now I want to enter a command into the command line from Python. Is there a way to pass this in as an argument to the first command?

Example:

os.system("cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt; and then run MySpecialCommand in CLI")

I'm not commited to a specific approach, I just need to be able to enter the command into the CLI from a python script.

Upvotes: 0

Views: 64

Answers (1)

s3bw
s3bw

Reputation: 3049

Try using sys.argv:

import os
import sys

my_string = ' '.join(sys.argv[1:])

template_cmd = "cd /Applications/program/MyApp.app/Contents/bin/; ./MyApp -prompt; {additional_arg}"

os.system(template_cmd.format(additional_arg=my_string))

This would be run as follows:

python my_script.py ls -l

Where ls -l would be your entered command.

Upvotes: 1

Related Questions