Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python argparse how to get entire command as string

I have a script named patchWidth.py and it parses command line arguments with argparse:

# read command line arguments -- the code is able to process multiple files
parser = argparse.ArgumentParser(description='angle simulation trajectories')
parser.add_argument('filenames', metavar='filename', type=str, nargs='+')
parser.add_argument('-vec', metavar='v', type=float, nargs=3)

Suppose this script is run with the following:

>>> python patchWidth.py file.dat -vec 0. 0. 1.

Is there a way to get this entire thing as a string in python? I would like to be able to print to the output file what command was run with what arguments.

like image 777
sodiumnitrate Avatar asked Apr 18 '18 00:04

sodiumnitrate


1 Answers

Yes, you can use the sys module:

import sys
str(sys.argv) # arguments as string

Note that argv[0] is the script name. For more information, take a look at the sys module documentation.

like image 193
TwistedSim Avatar answered Sep 25 '22 14:09

TwistedSim