0

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.

AlanGhalan
  • 198
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – foxyblue Sep 08 '17 at 19:32
  • Agreed with duplicate. The first (accepted) answer even explains why to use `subprocess.call` instead of `os.system`. – Matthew Cole Sep 08 '17 at 19:43

1 Answers1

1

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.

foxyblue
  • 2,114
  • 1
  • 19
  • 26
  • 1
    here, argparse is probably overkill (although it's a very useful module). The OP is only expecting one argument, with no need to transform or inspect them. – Matthew Cole Sep 08 '17 at 19:22
  • How would you write this If there were only ever going to be one command and it would always be the same? – AlanGhalan Sep 08 '17 at 19:26
  • Why would you have it in the CLI if it were always the same? – foxyblue Sep 08 '17 at 19:28
  • I want python to enter it into the programs command line. I don't want to enter it from the command line myself. – AlanGhalan Sep 08 '17 at 19:30