0

Context

For example, there is a python file named helloworld.py, and I am using python helloworld.py --arg1 --arg2 to run this python file.

Question

What should I write in the python file helloworld.py to obtain its running command-line script, i.e., python helloworld.py --arg1 --arg2?

Dylan Wang
  • 86
  • 1
  • 8
  • 2
    Laziest way `import sys; print(sys.executable, *sys.argv)`, but the args that have whitespaces will not be quoted properly on output but otherwise will be represented properly internally as a complete entry inside its element in `sys.argv`. – metatoaster Jun 23 '22 at 01:31
  • This is actually pretty difficult to do reliably. What is your OS/platform? – wim Jun 23 '22 at 01:42
  • It seems like @metatoaster solved my problem, thank you! One more thing, what if I add other environment variables like `CUDA_VISIBLE_DEVICES=0,1`? Is that possible to print this? – Dylan Wang Jun 23 '22 at 13:18
  • @wim, I am using Linux. – Dylan Wang Jun 23 '22 at 13:19
  • @DylanWang On Linux reading `/proc/self/cmdline` will be closer to the original cmdline than `sys.argv`. It's still not perfect though, since the shell has already processed the cmdline to some extent (env vars, escaping, etc) – wim Jun 23 '22 at 18:07
  • 1
    @metatoaster In general `sys.argv` can be missing some important parts (try example with `python3 -S helloworld.py`, the `-S` will not be there). – wim Jun 23 '22 at 18:09

1 Answers1

0

2 options: sys.argv and argparse

sys.argv:

  • Simpler, provides the command line arguments as a list of strings. So running

python helloworld.py --arg1 --arg2

will mean that sys.argv contains

['helloworld.py', '--arg1', '--arg2']

argparse:

  • More feature rich. Unquestionably my recommendation for anything more than a singular argument.
  • Supports positional and keyword arguments.
  • Arguments mat have values and may be limited to presets.
  • help is automatically generated
  • Example:
## helloworld.py
import argparse
parser = argparse.ArgumentParser()
# Positional argument
parser.add_argument(
    "operator",
    help="Operator to use",
    choices=("add","subtract")
)
# Optional argument with value
parser.add_argument(
    "-i",
    "--integer",
    help="An integer",
    default=0
)
# Optional argument, True if present, otherwise False
parser.add_argument(
    "-sq",
    "--square-everything",
    action="store_true",
    help="Whether all numbers will get squared"
)
args = parser.parse_args() 
argument_dict = dict(args) # parse_args returns a namespace object
print(argument_dict)
python helloworld.py add -i 4 -sq
{"operator":"add", "integer":4, "square-everything": True}
Immot
  • 241
  • 2
  • 7
  • 1
    The question asks how to get the original command line that was entered. I don't see what argparse has to do with anything here. – wim Jun 23 '22 at 01:44
  • Like what @wim said, I am not asking for printing arguments, in your case, I need the program to print `python helloworld.py add -i 4 -sq` – Dylan Wang Jun 23 '22 at 13:10