2

I am writing a generic script that can be run by a user, or via a script. I want to set it up so that when a user double-clicks the script, they can see the output, but if they run in the terminal, there is no prompt for input. For this reason, in my script's arguments, I set it up the following way:

import argparse
import os

parser = argparse.ArgumentParser()

parser.add_argument(
    '--interactive', '-i', action='store_true',
    default='PROMPT' not in os.environ,
    help='put the application in interactive mode'
)
args = parser.parse_args()

# Do Stuff

if args.interactive:
    input('Press enter to continue...')

This default rule (checking 'PROMPT' not in os.environ) works perfect for regular command line and batch scripts, as PROMPT gets automatically set. However, it does not work with Powershell. Is there a way to determine if the script has been run in a Powershell environment, so as to provide a similar solution of not being interactive without the user needing to specify anything?

I know the user can run the script this way in powershell:

$env:PROMPT = "NotTheEmptyString"; py .\test.py

However, this seems like a poor interface, and I don't know powershell well enough to check for things in the environment.

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
  • you can add a test for `$Host`. that will error out in cmd, but will give you the PoSh host if in powershell. – Lee_Dailey Aug 16 '19 at 02:50

0 Answers0