Sending parameters
Hi, how are you? I would like to know if there is any way to send parameters to a sage script.
Hi, how are you? I would like to know if there is any way to send parameters to a sage script.
To add to Emmanuel's answer, in addition to inspecting sys.argv
directly, you can easily implement clean and well-documented command line argument handling with argparse (it takes a little getting used to, but the examples at the top of the documentation contain most of what you need to know to get started).
Keep in mind that "Sage" code, including .sage
scripts, is just the Python programming language with a few specialized bells and whistles, so any question you might have about how to do X with Sage, that isn't explicitly mathematics-related, is a question you can also ask about how to do that in Python and hence find myriad existing resources.
From the Python docs:
sys.argv
The list of command line arguments passed to a Python script. argv[0]
is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c
command line option to the interpreter, argv[0]
is set to the string '-c'
. If no script name was passed to the Python interpreter, argv[0]
is the empty string.
To loop over the standard input, or the list of files given on the command line, see the fileinput module.
Note On Unix, command line arguments are passed by bytes from OS. Python decodes them with filesystem encoding and “surrogateescape” error handler. When you need original bytes, you can get it by [os.fsencode(arg) for arg in sys.argv]
.
The metacommentary is, IMHO, necessary, not only for this case, but for a multitude of other cases (reviewiong "old" quiestions is instructive...).
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2019-11-11 06:54:14 +0100
Seen: 1,062 times
Last updated: Nov 12 '19