Ask Your Question
0

Sending parameters

asked 2019-11-11 06:54:14 +0200

kevin gravatar image

Hi, how are you? I would like to know if there is any way to send parameters to a sage script.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2019-11-12 11:36:11 +0200

Iguananaut gravatar image

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.

edit flag offensive delete link more
1

answered 2019-11-12 07:42:04 +0200

Emmanuel Charpentier gravatar image

updated 2019-11-12 11:33:10 +0200

Iguananaut gravatar image

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].

edit flag offensive delete link more

Comments

[Edited to remove unnecessary metacommentary.]

Iguananaut gravatar imageIguananaut ( 2019-11-12 11:33:22 +0200 )edit

The metacommentary is, IMHO, necessary, not only for this case, but for a multitude of other cases (reviewiong "old" quiestions is instructive...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-11-12 13:55:18 +0200 )edit
1

Please don't browbeat people for asking questions.

Iguananaut gravatar imageIguananaut ( 2019-11-13 14:20:43 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2019-11-11 06:54:14 +0200

Seen: 784 times

Last updated: Nov 12 '19