Command-line Interface
Using arguments inside scripts
عند تشغيل نص باستخدام julia
، يمكنك تمرير معلمات إضافية إلى نصك:
$ julia script.jl arg1 arg2...
تُمرر هذه المعاملات الإضافية لواجهة الأوامر في الثابت العالمي ARGS
. يتم تمرير اسم البرنامج النصي نفسه كـ PROGRAM_FILE
العالمي. لاحظ أن ARGS
يتم تعيينه أيضًا عندما يتم إعطاء تعبير جوليا باستخدام خيار -e
في سطر الأوامر (انظر مخرجات مساعدة julia
أدناه) ولكن سيكون PROGRAM_FILE
فارغًا. على سبيل المثال، لطباعة المعاملات المعطاة لبرنامج نصي، يمكنك القيام بذلك:
$ julia -e 'println(PROGRAM_FILE); for x in ARGS; println(x); end' foo bar
foo
bar
أو يمكنك وضع هذا الرمز في نص وتشغيله:
$ echo 'println(PROGRAM_FILE); for x in ARGS; println(x); end' > script.jl
$ julia script.jl foo bar
script.jl
foo
bar
يمكن استخدام الفاصل --
لفصل وسائط سطر الأوامر المخصصة لملف البرنامج النصي عن الوسائط المخصصة لجوليا:
$ julia --color=yes -O -- script.jl arg1 arg2..
انظر أيضًا Scripting لمزيد من المعلومات حول كتابة نصوص جوليا.
The Main.main
entry point
اعتبارًا من جوليا 1.11، يقوم Base
بتصدير الماكرو @main
. هذا الماكرو يتوسع إلى الرمز main
، ولكن عند الانتهاء من تنفيذ نص أو تعبير، سيحاول julia
تنفيذ الدالة Main.main(ARGS)
إذا تم تعريف مثل هذه الدالة وتم اختيار هذا السلوك باستخدام الماكرو @main
.
تُعَد هذه الميزة مخصصة للمساعدة في توحيد سير العمل المجمعة والتفاعلية. في سير العمل المجمعة، قد يكون تحميل الكود الذي يُعرّف دالة main
مفصولًا مكانيًا وزمنيًا عن الاستدعاء. ومع ذلك، بالنسبة لسير العمل التفاعلية، فإن السلوك يعادل استدعاء exit(main(ARGS))
بشكل صريح في نهاية البرنامج النصي أو التعبير المُقيَّم.
تمت إضافة نقطة الدخول الخاصة Main.main
في Julia 1.11. من أجل التوافق مع إصدارات جوليا السابقة، أضف @isdefined(var"@main") ? (@main) : exit(main(ARGS))
بشكل صريح في نهاية سكريبتاتك.
لرؤية هذه الميزة قيد التنفيذ، اعتبر التعريف التالي، الذي سينفذ دالة الطباعة على الرغم من عدم وجود استدعاء صريح لـ main
:
$ julia -e '(@main)(args) = println("Hello World!")'
Hello World!
$
فقط الربط main
في وحدة Main
لديه هذا السلوك وفقط إذا تم استخدام الماكرو @main
داخل الوحدة المعرفة.
على سبيل المثال، استخدام hello
بدلاً من main
لن يؤدي إلى تنفيذ دالة hello
:
$ julia -e 'hello(ARGS) = println("Hello World!")'
$
ولن يكون تعريف بسيط لـ main
:
$ julia -e 'main(ARGS) = println("Hello World!")'
$
ومع ذلك، لا يلزم أن يحدث الاشتراك في وقت التعريف:
$ julia -e 'main(ARGS) = println("Hello World!"); @main'
Hello World!
$
يمكن استيراد الربط main
من حزمة. حزمة hello world المعرفة كالتالي
module Hello
export main
(@main)(args) = println("Hello from the package!")
end
قد يُستخدم كـ:
$ julia -e 'using Hello'
Hello from the package!
$ julia -e 'import Hello' # N.B.: Execution depends on the binding not whether the package is loaded
$
ومع ذلك، لاحظ أن التوصية الحالية لأفضل الممارسات هي عدم خلط كود التطبيق وكود المكتبة القابلة لإعادة الاستخدام في نفس الحزمة. يمكن توزيع التطبيقات المساعدة كحزم منفصلة أو كبرامج نصية مع نقاط دخول main
منفصلة في مجلد bin
الخاص بالحزمة.
Parallel mode
يمكن بدء Julia في وضع التوازي باستخدام إما خيارات -p
أو --machine-file
. ستقوم -p n
بإطلاق n
عمليات عامل إضافية، بينما ستقوم --machine-file file
بإطلاق عامل لكل سطر في الملف file
. يجب أن تكون الآلات المعرفة في file
قابلة للوصول عبر تسجيل دخول ssh
بدون كلمة مرور، مع تثبيت Julia في نفس الموقع مثل المضيف الحالي. تأخذ كل تعريف آلة الشكل [count*][user@]host[:port] [bind_addr[:port]]
. الافتراضي لـ user
هو المستخدم الحالي، و port
هو منفذ ssh القياسي. count
هو عدد العمال الذين سيتم إنشاؤهم على العقدة، والافتراضي هو 1. يحدد الخيار الاختياري bind-to bind_addr[:port]
عنوان IP والمنفذ الذي يجب أن تستخدمه العمال الآخرون للاتصال بهذا العامل.
Startup file
إذا كان لديك كود تريد تنفيذه كلما تم تشغيل جوليا، يمكنك وضعه في ~/.julia/config/startup.jl
:
$ echo 'println("Greetings! 你好! 안녕하세요?")' > ~/.julia/config/startup.jl
$ julia
Greetings! 你好! 안녕하세요?
...
لاحظ أنه على الرغم من أنه يجب أن يكون لديك دليل ~/.julia
بمجرد تشغيلك لجوليا لأول مرة، قد تحتاج إلى إنشاء مجلد ~/.julia/config
وملف ~/.julia/config/startup.jl
إذا كنت تستخدمه.
لتشغيل كود بدء التشغيل فقط في The Julia REPL (وليس عند تشغيل julia
على سبيل المثال في نص برمجي)، استخدم atreplinit
في startup.jl
:
atreplinit() do repl
# ...
end
Command-line switches for Julia
هناك طرق مختلفة لتشغيل كود جوليا وتوفير خيارات، مشابهة لتلك المتاحة لبرامج perl
و ruby
:
julia [switches] -- [programfile] [args...]
القائمة التالية هي قائمة كاملة من مفاتيح سطر الأوامر المتاحة عند تشغيل جوليا (علامة '*' تشير إلى القيمة الافتراضية، إذا كانت قابلة للتطبيق؛ الإعدادات المميزة '($)' قد تؤدي إلى تجميع الحزم مسبقًا):
Switch | Description |
---|---|
-v , --version | Display version information |
-h , --help | Print command-line options (this message) |
--help-hidden | Print uncommon options not shown by -h |
--project[={<dir>|@.}] | Set <dir> as the active project/environment. The default @. option will search through parent directories until a Project.toml or JuliaProject.toml file is found. |
-J , --sysimage <file> | Start up with the given system image file |
-H , --home <dir> | Set location of julia executable |
--startup-file={yes*|no} | Load JULIA_DEPOT_PATH/config/startup.jl ; if JULIA_DEPOT_PATH environment variable is unset, load ~/.julia/config/startup.jl |
--handle-signals={yes*|no} | Enable or disable Julia's default signal handlers |
--sysimage-native-code={yes*|no} | Use native code from system image if available |
--compiled-modules={yes*|no|existing|strict} | Enable or disable incremental precompilation of modules. The existing option allows use of existing compiled modules that were previously precompiled, but disallows creation of new precompile files. The strict option is similar, but will error if no precompile file is found. |
--pkgimages={yes*|no|existing} | Enable or disable usage of native code caching in the form of pkgimages. The existing option allows use of existing pkgimages but disallows creation of new ones |
-e , --eval <expr> | Evaluate <expr> |
-E , --print <expr> | Evaluate <expr> and display the result |
-m , --module <Package> [args] | Run entry point of Package (@main function) with `args' |
-L , --load <file> | Load <file> immediately on all processors |
-t , --threads {auto|N[,auto|M]} | Enable N[+M] threads; N threads are assigned to the default threadpool, and if M is specified, M threads are assigned to the interactive threadpool; auto tries to infer a useful default number of threads to use but the exact behavior might change in the future. Currently sets N to the number of CPUs assigned to this Julia process based on the OS-specific affinity assignment interface if supported (Linux and Windows) or to the number of CPU threads if not supported (MacOS) or if process affinity is not configured, and sets M to 1. |
--gcthreads=N[,M] | Use N threads for the mark phase of GC and M (0 or 1) threads for the concurrent sweeping phase of GC. N is set to half of the number of compute threads and M is set to 0 if unspecified. |
-p , --procs {N|auto} | Integer value N launches N additional local worker processes; auto launches as many workers as the number of local CPU threads (logical cores) |
--machine-file <file> | Run processes on hosts listed in <file> |
-i , --interactive | Interactive mode; REPL runs and isinteractive() is true |
-q , --quiet | Quiet startup: no banner, suppress REPL warnings |
--banner={yes|no|short|auto*} | Enable or disable startup banner |
--color={yes|no|auto*} | Enable or disable color text |
--history-file={yes*|no} | Load or save history |
--depwarn={yes|no*|error} | Enable or disable syntax and method deprecation warnings (error turns warnings into errors) |
--warn-overwrite={yes|no*} | Enable or disable method overwrite warnings |
--warn-scope={yes*|no} | Enable or disable warning for ambiguous top-level scope |
-C , --cpu-target <target> | Limit usage of CPU features up to <target> ; set to help to see the available options |
-O , --optimize={0|1|2*|3} | Set the optimization level (level is 3 if -O is used without a level) ($) |
--min-optlevel={0*|1|2|3} | Set the lower bound on per-module optimization |
-g , --debug-info={0|1*|2} | Set the level of debug info generation (level is 2 if -g is used without a level) ($) |
--inline={yes|no} | Control whether inlining is permitted, including overriding @inline declarations |
--check-bounds={yes|no|auto*} | Emit bounds checks always, never, or respect @inbounds declarations ($) |
--math-mode={ieee,fast} | Disallow or enable unsafe floating point optimizations (overrides @fastmath declaration) |
--polly={yes*|no} | Enable or disable the polyhedral optimizer Polly (overrides @polly declaration) |
--code-coverage[={none*|user|all}] | Count executions of source lines (omitting setting is equivalent to user ) |
--code-coverage=@<path> | Count executions but only in files that fall under the given file path/directory. The @ prefix is required to select this option. A @ with no path will track the current directory. |
--code-coverage=tracefile.info | Append coverage information to the LCOV tracefile (filename supports format tokens). |
--track-allocation[={none*|user|all}] | Count bytes allocated by each source line (omitting setting is equivalent to "user") |
--track-allocation=@<path> | Count bytes but only in files that fall under the given file path/directory. The @ prefix is required to select this option. A @ with no path will track the current directory. |
--bug-report=KIND | Launch a bug report session. It can be used to start a REPL, run a script, or evaluate expressions. It first tries to use BugReporting.jl installed in current environment and falls back to the latest compatible BugReporting.jl if not. For more information, see --bug-report=help . |
--heap-size-hint=<size> | Forces garbage collection if memory usage is higher than the given value. The value may be specified as a number of bytes, optionally in units of KB, MB, GB, or TB, or as a percentage of physical memory with %. |
--compile={yes*|no|all|min} | Enable or disable JIT compiler, or request exhaustive or minimal compilation |
--output-o <name> | Generate an object file (including system image data) |
--output-ji <name> | Generate a system image data file (.ji) |
--strip-metadata | Remove docstrings and source location info from system image |
--strip-ir | Remove IR (intermediate representation) of compiled functions |
--output-unopt-bc <name> | Generate unoptimized LLVM bitcode (.bc) |
--output-bc <name> | Generate LLVM bitcode (.bc) |
--output-asm <name> | Generate an assembly file (.s) |
--output-incremental={yes|no*} | Generate an incremental output file (rather than complete) |
--trace-compile={stderr|name} | Print precompile statements for methods compiled during execution or save to a path |
--image-codegen | Force generate code in imaging mode |
--permalloc-pkgimg={yes|no*} | Copy the data section of package images into memory |
في جوليا 1.0، لم يكن الخيار الافتراضي --project=@.
يبحث من الجذر الدليل لمستودع Git عن ملف Project.toml
. بدءًا من جوليا 1.1، أصبح يفعل ذلك.