Command Line Interface
Using arguments inside scripts
When you run the script using julia
, you can pass additional arguments to it.:
$ julia script.jl arg1 arg2...
These additional command line arguments are passed in the global constant ARGS'. The name of the script itself is passed in the global constant `PROGRAM_FILE'. Please note: the 'ARGS
constant is also set when calculating the Julia expression passed via the command line using the -e
parameter (see the output of the julia
help below), but the PROGRAM_FILE
constant will be empty. For example, you can output only the arguments passed to the script like this.
$ julia -e 'println(PROGRAM_FILE); for x in ARGS; println(x); end' foo bar
foo
bar
This code can also be placed in a script and run:
$ echo 'println(PROGRAM_FILE); for x in ARGS; println(x); end' > script.jl $ julia script.jl foo bar script.jl foo bar
Using the '--` separator, you can separate the command-line arguments intended for the script file from the arguments intended for Julia.:
$ julia --color=yes -O -- script.jl arg1 arg2..
For more information about writing Julia scripts, see Scripts.
The entry point is Main.main
Starting from Julia (1.11), Base
exports the macro @main'. This macro expands to the character `main
, but upon completion of the script or expression execution, julia
will attempt to execute the function Main.main(ARGS)
if such a function has been defined and this behavior has been selected using the macro @main
.
This feature is designed to help unify compiled and interactive workflows. In compiled workflows, the loading of the code defining the main
function can be spatially and temporarily separated from its call. However, for interactive workflows, this behavior is similar to explicitly calling exit(main(ARGS))
at the end of the script or expression being defined.
Compatibility: Julia 1.11
The special entry point |
To see this feature in action, consider the following definition, which executes the print function, despite the absence of an explicit call to main
:
$ julia -e '(@main)(args) = println("Hello World!")' Hello World!
This applies only to the binding of main
in the Main
module and only if the macro @main
was used in the defining module.
For example, if you use hello
instead of main
, the hello
function will not be executed.:
$ julia -e 'hello(ARGS) = println("Hello World!")'
as well as the simple definition of main
:
$ julia -e 'main(ARGS) = println("Hello World!")'
However, it is not necessary that acceptance takes place during the definition.:
$ julia -e 'main(ARGS) = println("Hello World!"); @main' Hello World!
The main
binding can be imported from the package. The hello world package, defined as follows:
module Hello export main (@main)(args) = println("Hello from the package!") end
can be used as shown below:
$ julia -e 'using Hello' Hello from the package! $ julia -e 'import Hello' # Note. The execution depends on the binding, not on whether the package is loaded.
However, please note that it is currently recommended not to mix the application code and a reusable library in the same package. Auxiliary helper applications can be distributed as separate packages or as scripts with separate entry points main
in the bin
folder of the package.
Parallel mode
Julia can be run in parallel mode using the '-p` or --machine-file
option. The '-pn` parameter starts an additional n
workflows, and the --machine-file file
starts a workflow for each line in the file
file. The computers specified in the file
argument must be accessible via ssh without a password, and the Julia environment must be installed at the location of the current host. Each computer is specified in the following form: [count*][user@]host[:port] [bind_addr[:port]]'. By default, `user
is the current user, and port
is the standard SSH port. count
is the number of worker processes spawned on a node (1 by default). The optional bind-to bind_addr[:port]
element defines the IP address and port that other worker processes should use to connect to this process.
Launch file
The code that should be executed each time Julia is started can be placed in the file ~/.julia/config/startup.jl
:
$ echo 'println("Greetings! 你好! 안녕하세요?")' > ~/.julia/config/startup.jl
$ julia
Greetings! 你好! 안녕하세요?
...
Please note: the directory ~/.julia
must exist after the first launch of Julia, however, the folder ~/.julia/config
and the file ~/.julia/config/startup.jl
may need to be created manually.
So that the startup code is executed only in REPL Julia (and, for example, not when running julia
in the script), use atreplinit
in startup.jl
:
atreplinit() do repl
# ...
end
Command Line Options for Julia
Just like programs in perl and ruby, there are different ways to run Julia code with parameters.:
julia [switches] -- [programfile] [args...]
The following is a complete list of command-line options available at Julia startup (the "*" symbol marks the default value, if any; options marked "($)" can initiate pre-compilation of the package).
Parameter | Description |
---|---|
|
Displays information about the version. |
|
Outputs command line parameters (the contents of this table). |
|
Displays rare parameters that are not displayed when using the '-h` parameter. |
|
Sets the directory |
|
Launching using the specified system image file |
|
Sets the location of the `julia' executable file. |
|
Downloads the file |
|
Enables or disables Julia signal handlers by default. |
|
Uses machine code from the system image, if available. |
|
Enables or disables incremental pre-compilation of modules. The |
|
Enables or disables the use of machine code caching in the pkgimages form. The |
|
Evaluates the expression |
|
Evaluates the expression |
|
Executes the entry point |
|
Immediately loads the file |
|
Activates N[+M] threads. N threads are assigned to the |
|
Use N threads for the tagging stage during garbage collection and M threads (0 or 1) for the parallel cleanup stage. N is set to half the number of computing threads, and M is set to 0, unless otherwise specified. |
|
Starts N additional local workflows (N is an integer). When using the |
|
Runs processes on the nodes listed in `<file>'. |
|
Interactive mode; REPL is running, |
|
Running without messages: the banner is not displayed, the REPL warnings are hidden. |
|
Enables or disables the loading banner. |
|
Enables or disables colored text. |
|
Downloads or saves the history. |
|
Enables or disables warnings about syntax and method decommissioning (errors are displayed instead of warnings when using the |
|
Enables or disables warnings about overwriting methods. |
|
Enables or disables a warning about an ambiguous top-level area. |
|
Limits the use of CPU functions at the |
|
Specifies the optimization level (level 3 if the |
|
Sets the lower bound for optimization at the module level. |
|
Specifies the level of debugging information to be created (level 2 if the |
|
Determines whether embedding is allowed, including redefining |
|
Determines whether boundary checks are always performed, never, or with consideration for |
|
Enables or disables unsafe optimizations of floating-point values (overrides the |
|
Enables or disables the polyhedral Polly optimizer (overrides the @polly declaration). |
|
Counts the number of source code line executions (if no value is specified, the value |
|
Counts the executions, but only in files located on the specified path or in the specified directory. The prefix |
|
Adds code coverage information to the LCOV trace file (the file name supports format tokens). |
|
Counts the number of bytes allocated by each line of the source code (if no value is specified, the user value is used). |
|
Counts bytes, but only in files located at the specified path or in the specified directory. The prefix |
|
Starts an error reporting session. It can be used to run a REPL, execute a script, or evaluate expressions. First, it tries to use the BugReporting file.the jl installed in the current environment. If the attempt fails, the latest compatible BugReporting.jl file is applied. For more information, see the description of the |
|
It forcibly starts garbage collection if the memory usage exceeds the set value. The value can be specified as a number of bytes (including KB, MB, GB, or TB) or as a percentage (%) of the amount of physical memory. |
|
Enables or disables the JIT compiler, or requests an exhaustive or minimal compilation. |
|
Creates an object file (including system image data). |
|
Creates a system image data file (.ji). |
|
Deletes docstring strings and source code location information from the system image. |
|
Removes the intermediate representation (IR) of compiled functions. |
|
Creates an unoptimized LLVM bitcode (.bc). |
|
Creates an LLVM bitcode (.bc). |
|
Creates an assembly file (.s). |
|
Creates an additional (incomplete) output data file. |
|
Displays precompilation statements for methods compiled at runtime, or saves them to the specified path. |
|
Forcibly generates the code in the image creation mode. |
|
Copies the package image data section to memory. |
Compatibility: Julia 1.1
In Julia 1.0, when using the |