osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?


On 2018-08-14 16:45, Malcolm Greene wrote:
> When you run a script via "python3 script.py" you can include command
> line options like -b, -B, -O, -OO, etc between the "python3" interpreter
> reference and the script.py file, eg. "python3 -b -B -O -OO script.py".
> When you create a script that is executable directly, eg. script.py with
> execution bit set on Linux or on Windows where the .py file extension is
> associated with a specific Python executable, there doesn't appear to be
> a way to pass command line options to the script. In this later case,
> how can I pass my script command line options without having these
> options confused with command line arguments?

You might try:
from getopt import getopt
or the (apparently newer):
from optparse import OptionParser

These appear to both be deprecated in favor of argparse. I haven't made
the switch myself because argparse appears (upon a cursory reading of
the documentation) to muddle the difference between options and
arguments.

-- 
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.