pythoncommand-line-arguments

Is it possible to get the exact script invocation in Python?


I would like my python script to produce a text file containing the exact command line the user entered to invoke the script. In the past, I have done the following in bash:

echo "$0" "$@" > "${output_dir}/configuration/invocation.txt"

Is there something equivalent in python? As noted here, "the command line arguments are already handled by the shell before they are sent into sys.argv. Therefore, shell quoting and whitespace are gone and cannot be exactly reconstructed."


Solution

  • The suggestions in the comments to use shlex are proving sufficient for my needs.