Title here
Summary here
Command-line arguments are a common way to parameterize execution of programs. For example, godot --script hello.gd
uses --script
and hello.gd
arguments to the godot
program.
extends SceneTree
func _init():
# OS.get_cmdline_args() provides access to raw command-line
# arguments. Note that the first value in this array
# is the path to the program, and OS.get_cmdline_args().slice(1)
# holds the arguments to the program.
var args_with_prog = OS.get_cmdline_args()
var args_without_prog = OS.get_cmdline_args().slice(1)
# You can get individual args with normal indexing.
var arg = OS.get_cmdline_args()[3]
print(args_with_prog)
print(args_without_prog)
print(arg)
quit()
To experiment with command-line arguments, it’s best to run the script directly with Godot.
$ godot --script command_line_arguments.gd a b c d
["/path/to/godot", "--script", "command_line_arguments.gd", "a", "b", "c", "d"]
["a", "b", "c", "d"]
c
In GDScript, we use OS.get_cmdline_args()
to access command-line arguments. The first argument is typically the path to the Godot executable, followed by any script-specific arguments.
Note that in GDScript, arrays are zero-indexed, so OS.get_cmdline_args()[3]
would give us the fourth argument (in this case, “c”).
Next, we’ll look at more advanced command-line processing with parsing options.