Program name: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 9: | Line 9: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">:start: |
||
print(‘Program: ’:argv[0])</ |
print(‘Program: ’:argv[0])</syntaxhighlight> |
||
=={{header|68000 Assembly}}== |
=={{header|68000 Assembly}}== |
||
{{works with|NEOGEO}} |
{{works with|NEOGEO}} |
||
The name of the game is stored on the cartridge at memory address $000200. It is exactly 16 characters and is padded with spaces (ASCII 32). Since there is no null terminator after this string, use its size to terminate a printing loop. |
The name of the game is stored on the cartridge at memory address $000200. It is exactly 16 characters and is padded with spaces (ASCII 32). Since there is no null terminator after this string, use its size to terminate a printing loop. |
||
< |
<syntaxhighlight lang="68000devpac">LEA $000200,A3 |
||
JSR PrintString ;(my print routine is 255-terminated and there just so happens to be an FF after the name of the game.)</ |
JSR PrintString ;(my print routine is 255-terminated and there just so happens to be an FF after the name of the game.)</syntaxhighlight> |
||
;Output |
;Output |
||
Line 27: | Line 27: | ||
Without built-in CRT, <code>argc</code> and <code>argv</code> are stored in the stack. The format looks like: |
Without built-in CRT, <code>argc</code> and <code>argv</code> are stored in the stack. The format looks like: |
||
<lang>sp+0 = argc |
<syntaxhighlight lang="text">sp+0 = argc |
||
sp+8 = argv[0] |
sp+8 = argv[0] |
||
sp+16 = argv[1] ...</ |
sp+16 = argv[1] ...</syntaxhighlight> |
||
Each item of <code>argv</code> is a pointer to a null-terminated 8-bit string. |
Each item of <code>argv</code> is a pointer to a null-terminated 8-bit string. |
||
< |
<syntaxhighlight lang="arm_assembly">.equ STDOUT, 1 |
||
.equ SVC_WRITE, 64 |
.equ SVC_WRITE, 64 |
||
.equ SVC_EXIT, 93 |
.equ SVC_EXIT, 93 |
||
Line 76: | Line 76: | ||
_exit: |
_exit: |
||
mov x8, #SVC_EXIT |
mov x8, #SVC_EXIT |
||
svc #0</ |
svc #0</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Being a compiled language, Ada has difficulties accessing source code filenames. But it is easy to access the executable's filename, using the function Command_Name defined in Ada.Command_Line: |
Being a compiled language, Ada has difficulties accessing source code filenames. But it is easy to access the executable's filename, using the function Command_Name defined in Ada.Command_Line: |
||
< |
<syntaxhighlight lang="ada">with Ada.Command_Line, Ada.Text_IO; |
||
procedure Command_Name is |
procedure Command_Name is |
||
begin |
begin |
||
Ada.Text_IO.Put_Line(Ada.Command_Line.Command_Name); |
Ada.Text_IO.Put_Line(Ada.Command_Line.Command_Name); |
||
end Command_Name;</ |
end Command_Name;</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
The program command line arguments are accessible via the argc()/argv() functions. The program name is the first in the list of arguments. |
The program command line arguments are accessible via the argc()/argv() functions. The program name is the first in the list of arguments. |
||
< |
<syntaxhighlight lang="aime">o_text(argv(0)); |
||
o_byte('\n');</ |
o_byte('\n');</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
BEGIN |
BEGIN |
||
print ((program idf, newline)) |
print ((program idf, newline)) |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 107: | Line 107: | ||
=={{header|Amazing Hopper}}== |
=={{header|Amazing Hopper}}== |
||
El primer parámetro en Hopper y sus sabores, siempre será el nombre del programa. |
El primer parámetro en Hopper y sus sabores, siempre será el nombre del programa. |
||
<syntaxhighlight lang="amazing hopper"> |
|||
<lang Amazing Hopper> |
|||
#include <hbasic.h> |
#include <hbasic.h> |
||
Begin |
Begin |
||
Line 113: | Line 113: | ||
Print("My Program name: ", name File,Newl) |
Print("My Program name: ", name File,Newl) |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 122: | Line 122: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
<syntaxhighlight lang="applesoftbasic"> |
|||
<lang ApplesoftBASIC> |
|||
10 GOSUB 40"GET PROGRAM NAME |
10 GOSUB 40"GET PROGRAM NAME |
||
20 PRINT N$ |
20 PRINT N$ |
||
Line 184: | Line 184: | ||
670 N$ = " |
670 N$ = " |
||
680 RETURN |
680 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 244: | Line 244: | ||
bx lr /* retour procedure */ |
bx lr /* retour procedure */ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang="autohotkey"> |
|||
<lang AutoHotkey> |
|||
MsgBox, % A_ScriptName |
MsgBox, % A_ScriptName |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: TAWK -f PROGRAM_NAME.AWK |
# syntax: TAWK -f PROGRAM_NAME.AWK |
||
# |
# |
||
Line 271: | Line 271: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 290: | Line 290: | ||
Unlike most MS BASICs, [[FreeBASIC]] provides a parsed version of <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgCommand COMMAND$]</code> (called as <code>COMMAND$(n)</code>). <code>COMMAND$(0)</code> is the program's name: |
Unlike most MS BASICs, [[FreeBASIC]] provides a parsed version of <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgCommand COMMAND$]</code> (called as <code>COMMAND$(n)</code>). <code>COMMAND$(0)</code> is the program's name: |
||
< |
<syntaxhighlight lang="qbasic">appname = COMMAND$(0)</syntaxhighlight> |
||
Additionally, FreeBASIC also provides an analog of [[#C|C's]] <code>argc/argv[]</code>, called <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargc __FB_ARGC__]</code> and <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargv __FB_ARGV__]</code>. __FB_ARGV__ can be used to get the program's name like this: |
Additionally, FreeBASIC also provides an analog of [[#C|C's]] <code>argc/argv[]</code>, called <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargc __FB_ARGC__]</code> and <code>[http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDdfbargv __FB_ARGV__]</code>. __FB_ARGV__ can be used to get the program's name like this: |
||
< |
<syntaxhighlight lang="qbasic">appname = *__FB_ARGV__(0)</syntaxhighlight> |
||
See also: [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#Visual Basic|Visual Basic]]. |
See also: [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#Visual Basic|Visual Basic]]. |
||
Line 299: | Line 299: | ||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> SYS "GetCommandLine" TO cl% |
||
PRINT $$cl%</ |
PRINT $$cl%</syntaxhighlight> |
||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Name1.bas"(A,B,C,S$) |
||
110 CHAIN "Name2.BAS"(A,B,C,S$) |
110 CHAIN "Name2.BAS"(A,B,C,S$) |
||
Line 311: | Line 311: | ||
edit 0 |
edit 0 |
||
start(1,2,3,"Hello")</ |
start(1,2,3,"Hello")</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
To get the name with which the program was invoked: |
To get the name with which the program was invoked: |
||
< |
<syntaxhighlight lang="bacon">PRINT TOKEN$(ARGUMENT$, 1)</syntaxhighlight> |
||
To get the full path: |
To get the full path: |
||
<lang |
<syntaxhighlight lang="bacon">PRINT ME$</syntaxhighlight> |
||
==={{header|uBasic/4tH}}=== |
==={{header|uBasic/4tH}}=== |
||
'''CMD(1)''' returns a string with the filename - '''SHOW()''' prints this string. |
'''CMD(1)''' returns a string with the filename - '''SHOW()''' prints this string. |
||
<lang>Print Show(Cmd(1))</ |
<syntaxhighlight lang="text">Print Show(Cmd(1))</syntaxhighlight> |
||
=={{header|Blue}}== |
=={{header|Blue}}== |
||
Line 326: | Line 326: | ||
Linux/x86-64. Will print the name of the executable from "argv[0]" as provided. |
Linux/x86-64. Will print the name of the executable from "argv[0]" as provided. |
||
< |
<syntaxhighlight lang="blue">global _start |
||
: syscall ( num:eax -- result:eax ) syscall ; |
: syscall ( num:eax -- result:eax ) syscall ; |
||
Line 350: | Line 350: | ||
: arg0 ( rsp -- rsp ) 8 add @ ; inline |
: arg0 ( rsp -- rsp ) 8 add @ ; inline |
||
: _start ( rsp -- noret ) arg0 print-arg bye ;</ |
: _start ( rsp -- noret ) arg0 print-arg bye ;</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 357: | Line 357: | ||
* <code>argv[0]</code> might be the name of an executable in the PATH, or it might be an absolute or relative path to the executable. At least with [[Unix]], the parent process can set <code>argv[0]</code> to any string, so <code>argv[0]</code> might not be the real name. It is best to pretend that <code>argv[0]</code> has the correct value, but mind that <code>argv[0]</code> might not be an actual file. |
* <code>argv[0]</code> might be the name of an executable in the PATH, or it might be an absolute or relative path to the executable. At least with [[Unix]], the parent process can set <code>argv[0]</code> to any string, so <code>argv[0]</code> might not be the real name. It is best to pretend that <code>argv[0]</code> has the correct value, but mind that <code>argv[0]</code> might not be an actual file. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
||
Line 363: | Line 363: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
To get the source information about some part of code, use compiler defined macros. Most compilers support them or some variation of. |
To get the source information about some part of code, use compiler defined macros. Most compilers support them or some variation of. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int main() |
int main() |
||
Line 373: | Line 373: | ||
__FILE__, __FUNCTION__, __LINE__); |
__FILE__, __FUNCTION__, __LINE__); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=== BSD === |
=== BSD === |
||
Line 386: | Line 386: | ||
{{works with|OpenBSD|5.0}} '''To compile myname.c:''' <code>make myname LDLIBS=-lkvm</code> |
{{works with|OpenBSD|5.0}} '''To compile myname.c:''' <code>make myname LDLIBS=-lkvm</code> |
||
< |
<syntaxhighlight lang="c">/* myname.c */ |
||
#include <sys/param.h> |
#include <sys/param.h> |
||
Line 422: | Line 422: | ||
kvm_close(kd); |
kvm_close(kd); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
The program can have three different names! |
The program can have three different names! |
||
Line 435: | Line 435: | ||
{{libheader|Win32}} |
{{libheader|Win32}} |
||
< |
<syntaxhighlight lang="c">#include <windows.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <wchar.h> |
#include <wchar.h> |
||
Line 491: | Line 491: | ||
free(path); |
free(path); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<pre>Path to executable: C:\Users\kernigh\Documents\field\scratch.exe</pre> |
<pre>Path to executable: C:\Users\kernigh\Documents\field\scratch.exe</pre> |
||
Line 497: | Line 497: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
This effectively outputs the executable name, file path, and any arguments for the current program. |
This effectively outputs the executable name, file path, and any arguments for the current program. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace ProgramName |
namespace ProgramName |
||
{ |
{ |
||
Line 507: | Line 507: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
In a C# application with a reference to System.Windows.Forms, the following can be used to retrieve the executable name and arguments without the full path. |
In a C# application with a reference to System.Windows.Forms, the following can be used to retrieve the executable name and arguments without the full path. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
namespace ProgramName |
namespace ProgramName |
||
{ |
{ |
||
Line 527: | Line 527: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
C++ can access the executable's filename through the arguments to main(). |
C++ can access the executable's filename through the arguments to main(). |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 539: | Line 539: | ||
char *program = argv[0]; |
char *program = argv[0]; |
||
cout << "Program: " << program << endl; |
cout << "Program: " << program << endl; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"} |
||
":";exit |
":";exit |
||
Line 553: | Line 553: | ||
(when (.contains (first *command-line-args*) *source-path*) |
(when (.contains (first *command-line-args*) *source-path*) |
||
(apply -main (rest *command-line-args*)))</ |
(apply -main (rest *command-line-args*)))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Line 559: | Line 559: | ||
COBOL has an internal PROGRAM-ID name, and then the external invocation name. |
COBOL has an internal PROGRAM-ID name, and then the external invocation name. |
||
< |
<syntaxhighlight lang="cobol"> identification division. |
||
program-id. sample. |
program-id. sample. |
||
Line 576: | Line 576: | ||
goback. |
goback. |
||
end program sample.</ |
end program sample.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 585: | Line 585: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
scriptname.coffee: |
scriptname.coffee: |
||
< |
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee |
||
main = () -> |
main = () -> |
||
Line 591: | Line 591: | ||
console.log "Program: " + program |
console.log "Program: " + program |
||
if not module.parent then main()</ |
if not module.parent then main()</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Shebangs require a special tweak to ~/.clisprc.lisp. |
Shebangs require a special tweak to ~/.clisprc.lisp. |
||
< |
<syntaxhighlight lang="lisp">;;; Play nice with shebangs |
||
(set-dispatch-macro-character #\# #\! |
(set-dispatch-macro-character #\# #\! |
||
(lambda (stream character n) |
(lambda (stream character n) |
||
(declare (ignore character n)) |
(declare (ignore character n)) |
||
(read-line stream nil nil t) |
(read-line stream nil nil t) |
||
nil))</ |
nil))</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisp">#!/bin/sh |
||
#| |
#| |
||
exec clisp -q -q $0 $0 ${1+"$@"} |
exec clisp -q -q $0 $0 ${1+"$@"} |
||
Line 632: | Line 632: | ||
args |
args |
||
:test #'(lambda (x y) (search x y :test #'equalp))) |
:test #'(lambda (x y) (search x y :test #'equalp))) |
||
(main args)))</ |
(main args)))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">#!/usr/bin/env rdmd |
||
import std.stdio; |
import std.stdio; |
||
Line 641: | Line 641: | ||
void main(in string[] args) { |
void main(in string[] args) { |
||
writeln("Program: ", args[0]); |
writeln("Program: ", args[0]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>C:\rosetta>program_name.exe |
<pre>C:\rosetta>program_name.exe |
||
Program: program_name.exe</pre> |
Program: program_name.exe</pre> |
||
< |
<syntaxhighlight lang="sh">$ dmd scriptname.d |
||
$ ./scriptname |
$ ./scriptname |
||
Program: ./scriptname</ |
Program: ./scriptname</syntaxhighlight> |
||
If the system is configured, the D programming language offers an 'interpreted-looking' mode, which exhibits slightly different behavior than the normal compilation: |
If the system is configured, the D programming language offers an 'interpreted-looking' mode, which exhibits slightly different behavior than the normal compilation: |
||
< |
<syntaxhighlight lang="sh">$ ./scriptname.d |
||
Program: /tmp/.rdmd/Users/andrew/Desktop/src/scriptname/scriptname.d.D3B32385A31B968A3CF8CAF1E1426E5F</ |
Program: /tmp/.rdmd/Users/andrew/Desktop/src/scriptname/scriptname.d.D3B32385A31B968A3CF8CAF1E1426E5F</syntaxhighlight> |
||
Alternative method using built-in function [http://dlang.org/changelog.html#current_path thisExePath()] |
Alternative method using built-in function [http://dlang.org/changelog.html#current_path thisExePath()] |
||
{{works with|D|2.064+}} |
{{works with|D|2.064+}} |
||
< |
<syntaxhighlight lang="d">// thisExePath function was introduced in D 2.064 (November 5, 2013) |
||
import std.file; |
import std.file; |
||
Line 665: | Line 665: | ||
{ |
{ |
||
writeln("Program: ", thisExePath()); |
writeln("Program: ", thisExePath()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} <pre>Z:\rosettacode>program_name.exe |
{{out}} <pre>Z:\rosettacode>program_name.exe |
||
Program: Z:\rosettacode\program_name.exe</pre> |
Program: Z:\rosettacode\program_name.exe</pre> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">#!/usr/bin/env dart |
||
main() { |
main() { |
||
var program = new Options().script; |
var program = new Options().script; |
||
print("Program: ${program}"); |
print("Program: ${program}"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program ProgramName; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 685: | Line 685: | ||
Writeln('Program name: ' + ParamStr(0)); |
Writeln('Program name: ' + ParamStr(0)); |
||
Writeln('Command line: ' + CmdLine); |
Writeln('Command line: ' + CmdLine); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">!print( "Name of this file: " get-from !args 0 )</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(js-eval "window.location.href") |
(js-eval "window.location.href") |
||
→ "http://www.echolalie.org/echolisp/" |
→ "http://www.echolalie.org/echolisp/" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x : |
ELENA 4.x : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
public program() |
public program() |
||
Line 706: | Line 706: | ||
console.printLine(program_arguments[0]); // the program name |
console.printLine(program_arguments[0]); // the program name |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $* |
||
;;; Shebang from John Swaby |
;;; Shebang from John Swaby |
||
Line 716: | Line 716: | ||
(defun main () |
(defun main () |
||
(let ((program (nth 2 command-line-args))) |
(let ((program (nth 2 command-line-args))) |
||
(message "Program: %s" program)))</ |
(message "Program: %s" program)))</syntaxhighlight> |
||
<code>load-file-name</code> is the ".el" or ".elc" currently being loaded. Within a batch <code>-l</code> it will be the script name, but within sub-loads like <code>require</code> etc it is that sub-load. |
<code>load-file-name</code> is the ".el" or ".elc" currently being loaded. Within a batch <code>-l</code> it will be the script name, but within sub-loads like <code>require</code> etc it is that sub-load. |
||
Line 724: | Line 724: | ||
When compiled Erlang's macros hold information about the running module. |
When compiled Erlang's macros hold information about the running module. |
||
< |
<syntaxhighlight lang="erlang">%% Compile |
||
%% |
%% |
||
%% erlc scriptname.erl |
%% erlc scriptname.erl |
||
Line 738: | Line 738: | ||
Program = ?FILE, |
Program = ?FILE, |
||
io:format("Program: ~s~n", [Program]), |
io:format("Program: ~s~n", [Program]), |
||
init:stop().</ |
init:stop().</syntaxhighlight> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">constant cmd = command_line() |
||
puts(1,cmd[2])</ |
puts(1,cmd[2])</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Line 752: | Line 752: | ||
* Run as a dotslashed program in Unix (./scriptname.fsx) |
* Run as a dotslashed program in Unix (./scriptname.fsx) |
||
< |
<syntaxhighlight lang="fsharp">#light (* |
||
exec fsharpi --exec $0 --quiet |
exec fsharpi --exec $0 --quiet |
||
*) |
*) |
||
Line 771: | Line 771: | ||
let main = |
let main = |
||
printfn "%s" scriptname</ |
printfn "%s" scriptname</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">#! /usr/bin/env factor |
||
USING: namespaces io command-line ; |
USING: namespaces io command-line ; |
||
Line 782: | Line 782: | ||
: main ( -- ) script get print ; |
: main ( -- ) script get print ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|GNU Forth}} |
{{works with|GNU Forth}} |
||
< |
<syntaxhighlight lang="forth">0 arg type cr \ gforth or gforth-fast, for example |
||
1 arg type cr \ name of script</ |
1 arg type cr \ name of script</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Please find example runs in the comments at the beginning of the FORTRAN2003 source. Program name verification can deter system attackers. Therefore, the code is provided as a separate easily reused module. |
Please find example runs in the comments at the beginning of the FORTRAN2003 source. Program name verification can deter system attackers. Therefore, the code is provided as a separate easily reused module. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
! program run with invalid name path/f |
! program run with invalid name path/f |
||
! |
! |
||
Line 839: | Line 839: | ||
write(6,*)'program continues...' |
write(6,*)'program continues...' |
||
end program name |
end program name |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Print "The program was invoked like this => "; Command(0) + " " + Command(-1) |
Print "The program was invoked like this => "; Command(0) + " " + Command(-1) |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
There are several ways to retrieve the program name. These functions return a string containing the name that can be used in various ways. |
There are several ways to retrieve the program name. These functions return a string containing the name that can be used in various ways. |
||
< |
<syntaxhighlight lang="futurebasic"> |
||
print fn ProcessInfoProcessName |
print fn ProcessInfoProcessName |
||
print fn RunningApplicationLocalizedName( fn RunningApplicationCurrentApplication ) |
print fn RunningApplicationLocalizedName( fn RunningApplicationCurrentApplication ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=fc8af45b13a9bb52b6955bab487fc7ac Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=fc8af45b13a9bb52b6955bab487fc7ac Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sTemp As String |
Dim sTemp As String |
||
Line 867: | Line 867: | ||
Next |
Next |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 874: | Line 874: | ||
=={{header|Genie}}== |
=={{header|Genie}}== |
||
< |
<syntaxhighlight lang="genie">[indent=4] |
||
init |
init |
||
print args[0] |
print args[0] |
||
print Path.get_basename(args[0]) |
print Path.get_basename(args[0]) |
||
print Environment.get_application_name() |
print Environment.get_application_name() |
||
print Environment.get_prgname()</ |
print Environment.get_prgname()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 892: | Line 892: | ||
scriptname.go: |
scriptname.go: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 901: | Line 901: | ||
func main() { |
func main() { |
||
fmt.Println("Program:", os.Args[0]) |
fmt.Println("Program:", os.Args[0]) |
||
}</ |
}</syntaxhighlight> |
||
{{out|Command line session}} |
{{out|Command line session}} |
||
<pre> |
<pre> |
||
Line 917: | Line 917: | ||
If you want the script filename, use: |
If you want the script filename, use: |
||
< |
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy |
||
def program = getClass().protectionDomain.codeSource.location.path |
def program = getClass().protectionDomain.codeSource.location.path |
||
println "Program: " + program</ |
println "Program: " + program</syntaxhighlight> |
||
But if you just want the class name, there are easier ways. |
But if you just want the class name, there are easier ways. |
||
So, just typing in and running the following in the GroovyConsole environment: |
So, just typing in and running the following in the GroovyConsole environment: |
||
<lang |
<syntaxhighlight lang="groovy">println this.class.getName()</syntaxhighlight> |
||
yields the following on the first run: |
yields the following on the first run: |
||
<pre>ConsoleScript0</pre> |
<pre>ConsoleScript0</pre> |
||
Line 940: | Line 940: | ||
Haskell has an impure function for this. |
Haskell has an impure function for this. |
||
< |
<syntaxhighlight lang="haskell">import System (getProgName) |
||
main :: IO () |
main :: IO () |
||
main = getProgName >>= putStrLn . ("Program: " ++)</ |
main = getProgName >>= putStrLn . ("Program: " ++)</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
write(&progname) # obtain and write out the program name from the keyword &progname |
write(&progname) # obtain and write out the program name from the keyword &progname |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">#!/usr/bin/env io |
||
main := method( |
main := method( |
||
Line 959: | Line 959: | ||
) |
) |
||
if (System args size > 0 and System args at(0) containsSeq("scriptname"), main)</ |
if (System args size > 0 and System args at(0) containsSeq("scriptname"), main)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">#!/usr/bin/env jconsole |
||
program =: monad : 0 |
program =: monad : 0 |
||
Line 975: | Line 975: | ||
echo 'Program: ', program 0 |
echo 'Program: ', program 0 |
||
exit ''</ |
exit ''</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 984: | Line 984: | ||
You can get the listing of the arguments to the <code>java</code> command through a system property. The first one is the name of the main class that was run. This depends on a system property named "sun.java.command", which might not exist on all Java virtual machines. |
You can get the listing of the arguments to the <code>java</code> command through a system property. The first one is the name of the main class that was run. This depends on a system property named "sun.java.command", which might not exist on all Java virtual machines. |
||
< |
<syntaxhighlight lang="java">public class ScriptName { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
String program = System.getProperty("sun.java.command").split(" ")[0]; |
String program = System.getProperty("sun.java.command").split(" ")[0]; |
||
System.out.println("Program: " + program); |
System.out.println("Program: " + program); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
An alternate solution is to create a dummy inner class, and then retrieve its enclosing class (which is the class the main method is in) through reflection (though this will not work if the code is placed in a method in another source file--it will give the name of the class it is in inside that source file): |
An alternate solution is to create a dummy inner class, and then retrieve its enclosing class (which is the class the main method is in) through reflection (though this will not work if the code is placed in a method in another source file--it will give the name of the class it is in inside that source file): |
||
< |
<syntaxhighlight lang="java">public class ScriptName { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Class c = new Object(){}.getClass().getEnclosingClass(); |
Class c = new Object(){}.getClass().getEnclosingClass(); |
||
System.out.println("Program: " + c.getName()); |
System.out.println("Program: " + c.getName()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
A solution using the security manager: |
A solution using the security manager: |
||
< |
<syntaxhighlight lang="java">public class ScriptName { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Class c = System.getSecurityManager().getClassContext()[0]; |
Class c = System.getSecurityManager().getClassContext()[0]; |
||
System.out.println("Program: " + c.getName()); |
System.out.println("Program: " + c.getName()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
A solution using the stack trace (requires Java 1.5+): |
A solution using the stack trace (requires Java 1.5+): |
||
< |
<syntaxhighlight lang="java">public class ScriptName { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
String program = Thread.currentThread().getStackTrace()[1].getClassName(); |
String program = Thread.currentThread().getStackTrace()[1].getClassName(); |
||
System.out.println("Program: " + program); |
System.out.println("Program: " + program); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 1,022: | Line 1,022: | ||
In some implementations, the following (non–standard) code will work: |
In some implementations, the following (non–standard) code will work: |
||
< |
<syntaxhighlight lang="javascript">function foo() { |
||
return arguments.callee.name; |
return arguments.callee.name; |
||
}</ |
}</syntaxhighlight> |
||
But it will fail in in others. JavaScript also has anonymous functions that don't have a name, e.g.: |
But it will fail in in others. JavaScript also has anonymous functions that don't have a name, e.g.: |
||
< |
<syntaxhighlight lang="javascript">(function(){alert(arguments.callee.name);}())</syntaxhighlight> |
||
returns an empty string or <code>undefined</code> even where the first example works. |
returns an empty string or <code>undefined</code> even where the first example works. |
||
Line 1,033: | Line 1,033: | ||
Node.js has a global variable for this. |
Node.js has a global variable for this. |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env node |
||
/*jslint nodejs:true */ |
/*jslint nodejs:true */ |
||
Line 1,041: | Line 1,041: | ||
} |
} |
||
if (!module.parent) { main(); }</ |
if (!module.parent) { main(); }</syntaxhighlight> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish |
||
/* Program name, in Jsish */ |
/* Program name, in Jsish */ |
||
puts('Executable:', Info.executable()); |
puts('Executable:', Info.executable()); |
||
puts('Argv0 :', Info.argv0());</ |
puts('Argv0 :', Info.argv0());</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,056: | Line 1,056: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Julia strips the program file name from <tt>ARGS</tt>, so this information is not available to the program from its command line arguments. Instead it is accessible via <tt>Base.source_path()</tt>. |
Julia strips the program file name from <tt>ARGS</tt>, so this information is not available to the program from its command line arguments. Instead it is accessible via <tt>Base.source_path()</tt>. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
prog = basename(Base.source_path()) |
prog = basename(Base.source_path()) |
||
println("This program file is \"", prog, "\".") |
println("This program file is \"", prog, "\".") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,067: | Line 1,067: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
// 'progname.kt' packaged as 'progname.jar' |
// 'progname.kt' packaged as 'progname.jar' |
||
Line 1,074: | Line 1,074: | ||
println(System.getProperty("sun.java.command")) // may not exist on all JVMs |
println(System.getProperty("sun.java.command")) // may not exist on all JVMs |
||
println(System.getProperty("java.vm.name")) |
println(System.getProperty("java.vm.name")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,084: | Line 1,084: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
The script name is in the _script variable (separate from the arguments, in the _args variable). |
The script name is in the _script variable (separate from the arguments, in the _args variable). |
||
< |
<syntaxhighlight lang="langur">writeln "script: ", _script |
||
writeln "script args: ", _args</ |
writeln "script args: ", _args</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,092: | Line 1,092: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9 |
||
stdoutnl("Program: " + $argv->first)</ |
stdoutnl("Program: " + $argv->first)</syntaxhighlight> |
||
Output: |
Output: |
||
< |
<syntaxhighlight lang="shell">$ lasso9 script_name.lasso |
||
Program: script_name.lasso</ |
Program: script_name.lasso</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
nSize = _MAX_PATH + 2 |
nSize = _MAX_PATH + 2 |
||
lpFilename$ = space$(nSize); chr$(0) |
lpFilename$ = space$(nSize); chr$(0) |
||
Line 1,143: | Line 1,143: | ||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
< |
<syntaxhighlight lang="text"> |
||
Path to LB exe |
Path to LB exe |
||
C:\progs\Liberty BASIC v4.04\liberty.exe |
C:\progs\Liberty BASIC v4.04\liberty.exe |
||
current program file (:last one on LRU list) |
current program file (:last one on LRU list) |
||
C:\progs\Liberty BASIC v4.04\untitled.bas |
C:\progs\Liberty BASIC v4.04\untitled.bas |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">put _player.applicationName |
||
-- "lsw.exe" |
-- "lsw.exe" |
||
put _movie.name |
put _movie.name |
||
-- "lsw_win_d11.dir"</ |
-- "lsw_win_d11.dir"</syntaxhighlight> |
||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
Like C, LLVM can use argv to access the executable's filename. |
Like C, LLVM can use argv to access the executable's filename. |
||
< |
<syntaxhighlight lang="sh">$ make |
||
llvm-as scriptname.ll |
llvm-as scriptname.ll |
||
llc -disable-cfi scriptname.bc |
llc -disable-cfi scriptname.bc |
||
gcc -o scriptname scriptname.s |
gcc -o scriptname scriptname.s |
||
./scriptname |
./scriptname |
||
Program: ./scriptname</ |
Program: ./scriptname</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<syntaxhighlight lang="make">all: scriptname.ll |
||
llvm-as scriptname.ll |
llvm-as scriptname.ll |
||
llc scriptname.bc |
llc scriptname.bc |
||
Line 1,180: | Line 1,180: | ||
-rm scriptname |
-rm scriptname |
||
-rm scriptname.s |
-rm scriptname.s |
||
-rm scriptname.bc</ |
-rm scriptname.bc</syntaxhighlight> |
||
< |
<syntaxhighlight lang="llvm">@msg_main = internal constant [13 x i8] c"Program: %s\0A\00" |
||
declare i32 @printf(i8* noalias nocapture, ...) |
declare i32 @printf(i8* noalias nocapture, ...) |
||
Line 1,191: | Line 1,191: | ||
ret i32 0 |
ret i32 0 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Lua's arg is like C's argv. |
Lua's arg is like C's argv. |
||
< |
<syntaxhighlight lang="lua">#!/usr/bin/env lua |
||
function main(arg) |
function main(arg) |
||
Line 1,207: | Line 1,207: | ||
else |
else |
||
module(..., package.seeall) |
module(..., package.seeall) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
Module Checkit { |
||
Declare GetModuleFileName Lib "kernel32.GetModuleFileNameW" {Long hModule, &lpFileName$, Long nSize} |
Declare GetModuleFileName Lib "kernel32.GetModuleFileNameW" {Long hModule, &lpFileName$, Long nSize} |
||
Line 1,228: | Line 1,228: | ||
} |
} |
||
SayIt |
SayIt |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Make}}== |
=={{header|Make}}== |
||
< |
<syntaxhighlight lang="make">NAME=$(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) |
||
all: |
all: |
||
@echo $(NAME) |
@echo $(NAME) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script |
||
ScriptName[] = Piecewise[ |
ScriptName[] = Piecewise[ |
||
{ |
{ |
||
Line 1,247: | Line 1,247: | ||
] |
] |
||
Program = ScriptName[]; |
Program = ScriptName[]; |
||
Print["Program: " <> Program]</ |
Print["Program: " <> Program]</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
< |
<syntaxhighlight lang="mercury">:- module program_name. |
||
:- interface. |
:- interface. |
||
Line 1,266: | Line 1,266: | ||
io.print_line(ProgName, !IO). |
io.print_line(ProgName, !IO). |
||
:- end_module program_name.</ |
:- end_module program_name.</syntaxhighlight> |
||
=={{header|Mozart/Oz}}== |
=={{header|Mozart/Oz}}== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: test |
||
test: scriptname |
test: scriptname |
||
Line 1,281: | Line 1,281: | ||
clean: |
clean: |
||
-rm scriptname |
-rm scriptname |
||
-rm *.exe</ |
-rm *.exe</syntaxhighlight> |
||
scriptname.oz: |
scriptname.oz: |
||
< |
<syntaxhighlight lang="oz">functor |
||
import |
import |
||
System |
System |
||
Line 1,295: | Line 1,295: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
<lang |
<syntaxhighlight lang="nanoquery">println args[1]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>programname.nq</pre> |
<pre>programname.nq</pre> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System.Environment; |
||
... |
... |
||
def program_name = GetCommandLineArgs()[0]; |
def program_name = GetCommandLineArgs()[0]; |
||
...</ |
...</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,317: | Line 1,317: | ||
say 'Program:' System.getProperty('sun.java.command') |
say 'Program:' System.getProperty('sun.java.command') |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output''' |
'''Output''' |
||
Line 1,337: | Line 1,337: | ||
newLISP has a function, (main-args int) for this. |
newLISP has a function, (main-args int) for this. |
||
< |
<syntaxhighlight lang="lisp">#!/usr/bin/env newlisp |
||
(let ((program (main-args 1))) |
(let ((program (main-args 1))) |
||
(println (format "Program: %s" program)) |
(println (format "Program: %s" program)) |
||
(exit))</ |
(exit))</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import os |
||
echo getAppFilename() # Prints the full path of the executed file |
echo getAppFilename() # Prints the full path of the executed file |
||
echo paramStr(0) # Prints argv[0]</ |
echo paramStr(0) # Prints argv[0]</syntaxhighlight> |
||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c Version 2 |
Works with oo2c Version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE ProgramName; |
MODULE ProgramName; |
||
IMPORT |
IMPORT |
||
Line 1,358: | Line 1,358: | ||
Out.Object("Program name: " + Args.Get(0));Out.Ln |
Out.Object("Program name: " + Args.Get(0));Out.Ln |
||
END ProgramName. |
END ProgramName. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,368: | Line 1,368: | ||
scriptname.m: |
scriptname.m: |
||
< |
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h> |
||
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
||
Line 1,383: | Line 1,383: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="sh">$ gcc -o scriptname -framework foundation scriptname.m |
||
$ ./scriptname |
$ ./scriptname |
||
Program: ./scriptname</ |
Program: ./scriptname</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let () = |
||
print_endline Sys.executable_name; |
print_endline Sys.executable_name; |
||
print_endline Sys.argv.(0)</ |
print_endline Sys.argv.(0)</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 1,413: | Line 1,413: | ||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">function main() |
||
program = program_name(); |
program = program_name(); |
||
printf("Program: %s", program); |
printf("Program: %s", program); |
||
endfunction |
endfunction |
||
main();</ |
main();</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
First argument of *vm-args* is an executing program name. |
First argument of *vm-args* is an executing program name. |
||
< |
<syntaxhighlight lang="scheme"> |
||
(print (car *vm-args*)) |
(print (car *vm-args*)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Order}}== |
=={{header|Order}}== |
||
This is relatively trivial in Order, as the program being executed is a macro expression in the current C program file being read by the compiler: |
This is relatively trivial in Order, as the program being executed is a macro expression in the current C program file being read by the compiler: |
||
<lang |
<syntaxhighlight lang="c">__FILE__</syntaxhighlight> |
||
When including another file containing another <code>ORDER_PP</code> expression, within that file the <code>__FILE__</code> macro will expand to the name of that file; but arguably that expression constitutes a separate Order program within the greater C project. |
When including another file containing another <code>ORDER_PP</code> expression, within that file the <code>__FILE__</code> macro will expand to the name of that file; but arguably that expression constitutes a separate Order program within the greater C project. |
||
Line 1,436: | Line 1,436: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program ScriptName; |
||
var |
var |
||
prog : String; |
prog : String; |
||
Line 1,443: | Line 1,443: | ||
write('Program: '); |
write('Program: '); |
||
writeln(prog) |
writeln(prog) |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/env perl |
||
use strict; |
use strict; |
||
Line 1,456: | Line 1,456: | ||
} |
} |
||
unless(caller) { main; }</ |
unless(caller) { main; }</syntaxhighlight> |
||
<code>$0</code> includes the full path if a script is run as say <code>perl /some/dir/foo.pl</code>. The <code>FindBin</code> module can give just the basename. This can be good for printing in diagnostics etc. |
<code>$0</code> includes the full path if a script is run as say <code>perl /some/dir/foo.pl</code>. The <code>FindBin</code> module can give just the basename. This can be good for printing in diagnostics etc. |
||
< |
<syntaxhighlight lang="perl">use FindBin; |
||
print "Program name $FindBin::Script\n";</ |
print "Program name $FindBin::Script\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">cl2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">cl2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> |
||
Line 1,469: | Line 1,469: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_name</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test.exw or test.exe or test.htm</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_name</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test.exw or test.exe or test.htm</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- eg test</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
<lang |
<syntaxhighlight lang="phixmonti">argument 1 get ?</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
PHP has a global dictionary for this. |
PHP has a global dictionary for this. |
||
< |
<syntaxhighlight lang="php"><?php |
||
$program = $_SERVER["SCRIPT_NAME"]; |
$program = $_SERVER["SCRIPT_NAME"]; |
||
echo "Program: $program\n"; |
echo "Program: $program\n"; |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
The function '[http://software-lab.de/doc/refC.html#cmd cmd]' returns the command name. |
The function '[http://software-lab.de/doc/refC.html#cmd cmd]' returns the command name. |
||
< |
<syntaxhighlight lang="picolisp">: (cmd) |
||
-> "/usr/bin/picolisp"</ |
-> "/usr/bin/picolisp"</syntaxhighlight> |
||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
Line 1,491: | Line 1,491: | ||
Previous versions of PowerBASIC ([[PB/Win]] 8 or older; [[PB/CC]] 4 or older) have to make an [[API]] call: |
Previous versions of PowerBASIC ([[PB/Win]] 8 or older; [[PB/CC]] 4 or older) have to make an [[API]] call: |
||
< |
<syntaxhighlight lang="powerbasic">#INCLUDE "Win32API.inc" |
||
'[...] |
'[...] |
||
DIM fullpath AS ASCIIZ * 260, appname AS STRING |
DIM fullpath AS ASCIIZ * 260, appname AS STRING |
||
Line 1,499: | Line 1,499: | ||
ELSE |
ELSE |
||
appname = fullpath |
appname = fullpath |
||
END IF</ |
END IF</syntaxhighlight> |
||
{{works with|PowerBASIC for Windows|9}} |
{{works with|PowerBASIC for Windows|9}} |
||
Line 1,505: | Line 1,505: | ||
Recent versions of PowerBASIC provide the <code>EXE</code> object; <code>EXE.NAME$</code> returns the program's name, while <code>EXE.NAMEX$</code> returns the program's name and extension. (<code>EXE.EXTN$</code> returns the extension only.) So, to get the program's name, we do this: |
Recent versions of PowerBASIC provide the <code>EXE</code> object; <code>EXE.NAME$</code> returns the program's name, while <code>EXE.NAMEX$</code> returns the program's name and extension. (<code>EXE.EXTN$</code> returns the extension only.) So, to get the program's name, we do this: |
||
< |
<syntaxhighlight lang="powerbasic">appname = EXE.NAMEX$</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
# write this in file <program.ps1> |
# write this in file <program.ps1> |
||
$MyInvocation.MyCommand.Name |
$MyInvocation.MyCommand.Name |
||
# launch with <.\program> |
# launch with <.\program> |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 1,519: | Line 1,519: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog">% SWI-Prolog version 8.0.0 for i686-linux. |
||
% This will find itself, and return the knowledge base it is in. |
% This will find itself, and return the knowledge base it is in. |
||
file_name(F) :- true |
file_name(F) :- true |
||
Line 1,526: | Line 1,526: | ||
, source_file(M:P, F) % F is the file . |
, source_file(M:P, F) % F is the file . |
||
, \+ predicate_property(M:P, imported_from(_)) |
, \+ predicate_property(M:P, imported_from(_)) |
||
.</ |
.</syntaxhighlight> |
||
Alternatively, you may prefer a list of all your knowledge bases; adding the following code to each of your knowledge bases will allow you to query <code>findall(F, source_file(this_is_one_of_my_files, F), L).</code>. |
Alternatively, you may prefer a list of all your knowledge bases; adding the following code to each of your knowledge bases will allow you to query <code>findall(F, source_file(this_is_one_of_my_files, F), L).</code>. |
||
<lang |
<syntaxhighlight lang="prolog">:- multifile(this_is_one_of_my_files). this_is_one_of_my_files.</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
PureBasic provides a way to retrieve the filename of the program being executed. It includes the file's path. |
PureBasic provides a way to retrieve the filename of the program being executed. It includes the file's path. |
||
< |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
PrintN(ProgramFilename()) |
PrintN(ProgramFilename()) |
||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output when executing the above program compiled to an executible file called 'program name.exe': |
Sample output when executing the above program compiled to an executible file called 'program name.exe': |
||
<pre>H:\Data\Rosetta Code\program name.exe</pre> |
<pre>H:\Data\Rosetta Code\program name.exe</pre> |
||
Line 1,545: | Line 1,545: | ||
Python has at least two ways to get the script name: the traditional ARGV and the inspect module. |
Python has at least two ways to get the script name: the traditional ARGV and the inspect module. |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python |
||
import sys |
import sys |
||
Line 1,554: | Line 1,554: | ||
if __name__ == "__main__": |
if __name__ == "__main__": |
||
main()</ |
main()</syntaxhighlight> |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python |
||
import inspect |
import inspect |
||
Line 1,566: | Line 1,566: | ||
if __name__ == "__main__": |
if __name__ == "__main__": |
||
main()</ |
main()</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
R's syntax is complicated, but doable. |
R's syntax is complicated, but doable. |
||
< |
<syntaxhighlight lang="r">#!/usr/bin/env Rscript |
||
getProgram <- function(args) { |
getProgram <- function(args) { |
||
Line 1,582: | Line 1,582: | ||
cat("Program: ", program, "\n") |
cat("Program: ", program, "\n") |
||
q("no")</ |
q("no")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#!/usr/bin/env racket |
||
#lang racket |
#lang racket |
||
(define (program) (find-system-path 'run-file)) |
(define (program) (find-system-path 'run-file)) |
||
(module+ main (printf "Program: ~a\n" (program)))</ |
(module+ main (printf "Program: ~a\n" (program)))</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,596: | Line 1,596: | ||
{{works with|rakudo|2015-10-16}} |
{{works with|rakudo|2015-10-16}} |
||
In Raku, the name of the program being executed is in the special global variable <tt>$*PROGRAM-NAME</tt>. |
In Raku, the name of the program being executed is in the special global variable <tt>$*PROGRAM-NAME</tt>. |
||
<lang |
<syntaxhighlight lang="raku" line>say $*PROGRAM-NAME;</syntaxhighlight> |
||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
< |
<syntaxhighlight lang="raven">ARGS list " " join "%s\n" print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>raven arg_file.rv</pre> |
<pre>raven arg_file.rv</pre> |
||
Line 1,612: | Line 1,612: | ||
<br><br>It should be noted that the format of the complete path varies depending upon the operating system. |
<br><br>It should be noted that the format of the complete path varies depending upon the operating system. |
||
< |
<syntaxhighlight lang="rexx">/* Rexx */ |
||
Parse source . . pgmPath |
Parse source . . pgmPath |
||
Say pgmPath |
Say pgmPath |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
<pre> |
<pre> |
||
Line 1,625: | Line 1,625: | ||
REXX does not support the use of arg(0) to access the program name. A workaround is to use a shell wrapper script to obtain and provide the invocation name of the wrapper: |
REXX does not support the use of arg(0) to access the program name. A workaround is to use a shell wrapper script to obtain and provide the invocation name of the wrapper: |
||
< |
<syntaxhighlight lang="sh">#!/bin/sh |
||
rexxbit.rexx $0 $*</ |
rexxbit.rexx $0 $*</syntaxhighlight> |
||
Here is a rexx script that makes use of this: |
Here is a rexx script that makes use of this: |
||
< |
<syntaxhighlight lang="rexx">say "The program is called " arg(1)</syntaxhighlight> |
||
On TSO, this program |
On TSO, this program |
||
< |
<syntaxhighlight lang="rexx">/*REXX program RANG1 in PDS N561985.PRIV.CLIST W. Pachl */ |
||
Parse Source a b c |
Parse Source a b c |
||
Say 'a='a |
Say 'a='a |
||
Say 'b='!!b |
Say 'b='!!b |
||
Say 'c='c </ |
Say 'c='c </syntaxhighlight> |
||
yields |
yields |
||
<pre> |
<pre> |
||
Line 1,653: | Line 1,653: | ||
:::* ROO REXX |
:::* ROO REXX |
||
:::* ooRexx |
:::* ooRexx |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm displays the name (& possible path) of the REXX program name.*/ |
||
parse version _version |
parse version _version |
||
parse source _system _howInvoked _path |
parse source _system _howInvoked _path |
||
Line 1,667: | Line 1,667: | ||
call prog_nam , 'subroutine' /*call ourself as a subroutine. */ |
call prog_nam , 'subroutine' /*call ourself as a subroutine. */ |
||
zz = prog_nam( , 'function') /* " " " " function. */ |
zz = prog_nam( , 'function') /* " " " " function. */ |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
'''output''' when using '''BREXX''' with the input of: <tt> command </tt> |
'''output''' when using '''BREXX''' with the input of: <tt> command </tt> |
||
<pre> |
<pre> |
||
Line 1,767: | Line 1,767: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "Active Source File Name : " + filename() + nl |
see "Active Source File Name : " + filename() + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
output |
output |
||
< |
<syntaxhighlight lang="ring"> |
||
Active Source File Name : tests\filename.ring |
Active Source File Name : tests\filename.ring |
||
</syntaxhighlight> |
|||
</lang> |
|||
Check the main file in the program |
Check the main file in the program |
||
< |
<syntaxhighlight lang="ring"> |
||
if sysargv[2] = filename() |
if sysargv[2] = filename() |
||
see "I'm the main program file!" + nl |
see "I'm the main program file!" + nl |
||
Line 1,783: | Line 1,783: | ||
see "I'm a sub file in a program" + nl |
see "I'm a sub file in a program" + nl |
||
ok |
ok |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">#!/usr/bin/env ruby |
||
puts "Path: #{$PROGRAM_NAME}" # or puts "Path: #{$0}" |
puts "Path: #{$PROGRAM_NAME}" # or puts "Path: #{$0}" |
||
puts "Name: #{File.basename $0}"</ |
puts "Name: #{File.basename $0}"</syntaxhighlight> |
||
For example, |
For example, |
||
Line 1,807: | Line 1,807: | ||
scriptname.rs: |
scriptname.rs: |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
println!("Program: {}", std::env::args().next().unwrap()); |
println!("Program: {}", std::env::args().next().unwrap()); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ rustc scriptname.rs |
||
$ ./scriptname |
$ ./scriptname |
||
Program: ./scriptname</ |
Program: ./scriptname</syntaxhighlight> |
||
=={{header|SAC}}== |
=={{header|SAC}}== |
||
Line 1,821: | Line 1,821: | ||
scriptname.sac: |
scriptname.sac: |
||
< |
<syntaxhighlight lang="c">use StdIO: all; |
||
use Array: all; |
use Array: all; |
||
use String: { string }; |
use String: { string }; |
||
Line 1,830: | Line 1,830: | ||
printf("Program: %s\n", program); |
printf("Program: %s\n", program); |
||
return(0); |
return(0); |
||
}</ |
}</syntaxhighlight> |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptname |
||
scriptname: scriptname.sac |
scriptname: scriptname.sac |
||
Line 1,841: | Line 1,841: | ||
clean: |
clean: |
||
-rm scriptname |
-rm scriptname |
||
-rm scriptname.c</ |
-rm scriptname.c</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ make |
||
sac2c -o scriptname scriptname.sac |
sac2c -o scriptname scriptname.sac |
||
$ ./scriptname |
$ ./scriptname |
||
Program: ./scriptname</ |
Program: ./scriptname</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object ScriptName extends App { |
||
println(s"Program of instantiated object: ${this.getClass.getName}") |
println(s"Program of instantiated object: ${this.getClass.getName}") |
||
// Not recommended, due various implementations |
// Not recommended, due various implementations |
||
println(s"Program via enviroment: ${System.getProperty("sun.java.command")}") |
println(s"Program via enviroment: ${System.getProperty("sun.java.command")}") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 1,861: | Line 1,861: | ||
Getting the program name is tricky. When interpreted, the script name will be printed. When compiled, the executable name will be printed. |
Getting the program name is tricky. When interpreted, the script name will be printed. When compiled, the executable name will be printed. |
||
< |
<syntaxhighlight lang="scheme">#!/bin/sh |
||
#| |
#| |
||
exec csi -ss $0 ${1+"$@"} |
exec csi -ss $0 ${1+"$@"} |
||
Line 1,885: | Line 1,885: | ||
(if (equal? (car (program)) 'compiled) |
(if (equal? (car (program)) 'compiled) |
||
(main (cdr (argv))))</ |
(main (cdr (argv))))</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 1,892: | Line 1,892: | ||
When the program is compiled this is the path of the executable. |
When the program is compiled this is the path of the executable. |
||
The functions ''dir(PROGRAM)'' and ''file(PROGRAM)'' deliver the directory respectivly file name of the program path. |
The functions ''dir(PROGRAM)'' and ''file(PROGRAM)'' deliver the directory respectivly file name of the program path. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 1,901: | Line 1,901: | ||
writeln("Program directory: " <& dir(PROGRAM)); |
writeln("Program directory: " <& dir(PROGRAM)); |
||
writeln("Program file: " <& file(PROGRAM)); |
writeln("Program file: " <& file(PROGRAM)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output when the program is interpreted: |
Output when the program is interpreted: |
||
Line 1,918: | Line 1,918: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">say __MAIN__; |
||
if (__MAIN__ != __FILE__) { |
if (__MAIN__ != __FILE__) { |
||
say "This file has been included!"; |
say "This file has been included!"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 1,928: | Line 1,928: | ||
Note that this only works when run as "./scriptname.st", because the shebang must force the script name onto ARGV. |
Note that this only works when run as "./scriptname.st", because the shebang must force the script name onto ARGV. |
||
< |
<syntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@" |
||
"exit" |
"exit" |
||
Line 1,935: | Line 1,935: | ||
program := Smalltalk getArgv: 1. |
program := Smalltalk getArgv: 1. |
||
Transcript show: 'Program: ', program; cr.</ |
Transcript show: 'Program: ', program; cr.</syntaxhighlight> |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
Works when run in script mode or in a workspace. |
Works when run in script mode or in a workspace. |
||
< |
<syntaxhighlight lang="smalltalk">| program | |
||
program := Smalltalk commandLine first. |
program := Smalltalk commandLine first. |
||
Transcript show: 'Program: ', program; cr.</ |
Transcript show: 'Program: ', program; cr.</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">#!/usr/bin/env sml |
||
let |
let |
||
Line 1,952: | Line 1,952: | ||
in |
in |
||
print ("Program: " ^ program ^ "\n") |
print ("Program: " ^ program ^ "\n") |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">#!/usr/bin/env tclsh |
||
proc main {args} { |
proc main {args} { |
||
Line 1,964: | Line 1,964: | ||
if {$::argv0 eq [info script]} { |
if {$::argv0 eq [info script]} { |
||
main {*}$::argv |
main {*}$::argv |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 1,970: | Line 1,970: | ||
Given this code in <code>program-name.txr</code>, marked executable: |
Given this code in <code>program-name.txr</code>, marked executable: |
||
< |
<syntaxhighlight lang="txr">#!/usr/local/bin/txr -B |
||
@(bind my-name @self-path)</ |
@(bind my-name @self-path)</syntaxhighlight> |
||
If we run it as an executable: |
If we run it as an executable: |
||
Line 1,996: | Line 1,996: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
echo "Program: $0"</ |
echo "Program: $0"</syntaxhighlight> |
||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
Get name of program and print to console: |
Get name of program and print to console: |
||
< |
<syntaxhighlight lang="vala"> |
||
public static void main(string[] args){ |
public static void main(string[] args){ |
||
string command_name = args[0]; |
string command_name = args[0]; |
||
Line 2,008: | Line 2,008: | ||
stdout.printf("%s\n", command_name); |
stdout.printf("%s\n", command_name); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output for program named "filename": |
Output for program named "filename": |
||
Line 2,018: | Line 2,018: | ||
VBA can retrieve the name of the program hosting the VBA code using the <code>Application</code> object: |
VBA can retrieve the name of the program hosting the VBA code using the <code>Application</code> object: |
||
<lang |
<syntaxhighlight lang="vb">Debug.Print Application.Name</syntaxhighlight> |
||
This is mostly useful for code that is shared between, say, [[wp:Microsoft Excel|Microsoft Excel]] and [[wp:Microsoft Word|Microsoft Word]], but has different requirements or actions depending on where it's running. |
This is mostly useful for code that is shared between, say, [[wp:Microsoft Excel|Microsoft Excel]] and [[wp:Microsoft Word|Microsoft Word]], but has different requirements or actions depending on where it's running. |
||
Line 2,028: | Line 2,028: | ||
vbscript provides the Wscript object. Among its properties are the following: |
vbscript provides the Wscript object. Among its properties are the following: |
||
< |
<syntaxhighlight lang="vbscript"> |
||
Wscript.Echo "FullName:",Wscript.FullName |
Wscript.Echo "FullName:",Wscript.FullName |
||
Wscript.Echo "Name:",Wscript.Name |
Wscript.Echo "Name:",Wscript.Name |
||
Line 2,034: | Line 2,034: | ||
Wscript.Echo "ScriptFullName:",Wscript.ScriptFullName |
Wscript.Echo "ScriptFullName:",Wscript.ScriptFullName |
||
Wscript.Echo "ScriptName:",Wscript.ScriptName |
Wscript.Echo "ScriptName:",Wscript.ScriptName |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,047: | Line 2,047: | ||
Visual Basic provides the <code>App</code> object, which has a property called <code>EXEName</code> that contains the program's filename ''without the extension''. (For most uses, this doesn't matter, but for code shared between, for example, a program and a screensaver, it can be important.) So, if a program is called "MyVBapp.exe", retreiving the value of <code>App.EXEName</code> would look like this: |
Visual Basic provides the <code>App</code> object, which has a property called <code>EXEName</code> that contains the program's filename ''without the extension''. (For most uses, this doesn't matter, but for code shared between, for example, a program and a screensaver, it can be important.) So, if a program is called "MyVBapp.exe", retreiving the value of <code>App.EXEName</code> would look like this: |
||
< |
<syntaxhighlight lang="vb">appname = App.EXEName 'appname = "MyVBapp"</syntaxhighlight> |
||
Alternately, Visual Basic can make an [[API]] call: |
Alternately, Visual Basic can make an [[API]] call: |
||
< |
<syntaxhighlight lang="vb">Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long |
||
Dim fullpath As String * 260, appname As String, namelen As Long |
Dim fullpath As String * 260, appname As String, namelen As Long |
||
namelen = GetModuleFileName (0, fullpath, 260) |
namelen = GetModuleFileName (0, fullpath, 260) |
||
Line 2,058: | Line 2,058: | ||
Else |
Else |
||
appname = fullpath |
appname = fullpath |
||
End If</ |
End If</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Assuming we're running a script file named "program_name.wren": |
Assuming we're running a script file named "program_name.wren": |
||
< |
<syntaxhighlight lang="ecmascript">import "os" for Process |
||
System.print("My name is %(Process.allArguments[1])")</ |
System.print("My name is %(Process.allArguments[1])")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,075: | Line 2,075: | ||
===Linux=== |
===Linux=== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">FORMAT=-f elf |
||
RUN=./ |
RUN=./ |
||
BIN=scriptname |
BIN=scriptname |
||
Line 2,093: | Line 2,093: | ||
clean: |
clean: |
||
-rm $(BIN) |
-rm $(BIN) |
||
-rm $(OBJ)</ |
-rm $(OBJ)</syntaxhighlight> |
||
scriptname.asm: |
scriptname.asm: |
||
< |
<syntaxhighlight lang="asm">bits 32 |
||
section .data |
section .data |
||
Line 2,167: | Line 2,167: | ||
mov eax, sys_exit |
mov eax, sys_exit |
||
mov ebx, 0 |
mov ebx, 0 |
||
int kernel</ |
int kernel</syntaxhighlight> |
||
===FreeBSD/Mac OS X=== |
===FreeBSD/Mac OS X=== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make"># FreeBSD defaults |
||
FORMAT=-f elf |
FORMAT=-f elf |
||
Line 2,197: | Line 2,197: | ||
clean: |
clean: |
||
-rm $(BIN) |
-rm $(BIN) |
||
-rm $(OBJ)</ |
-rm $(OBJ)</syntaxhighlight> |
||
scriptname.asm: |
scriptname.asm: |
||
< |
<syntaxhighlight lang="asm">bits 32 |
||
section .data |
section .data |
||
Line 2,278: | Line 2,278: | ||
mov eax, sys_exit |
mov eax, sys_exit |
||
sub esp, 4 |
sub esp, 4 |
||
int kernel</ |
int kernel</syntaxhighlight> |
||
===Windows=== |
===Windows=== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">FORMAT=-f win32 |
||
BIN=scriptname.exe |
BIN=scriptname.exe |
||
OBJ=scriptname.obj |
OBJ=scriptname.obj |
||
Line 2,300: | Line 2,300: | ||
clean: |
clean: |
||
-rm $(BIN) |
-rm $(BIN) |
||
-rm $(OBJ)</ |
-rm $(OBJ)</syntaxhighlight> |
||
scriptname.asm: |
scriptname.asm: |
||
< |
<syntaxhighlight lang="asm">bits 32 |
||
section .data |
section .data |
||
Line 2,396: | Line 2,396: | ||
push 0 |
push 0 |
||
call ExitProcess</ |
call ExitProcess</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">print peek$("program_name") |
||
s$ = system$("cd") |
s$ = system$("cd") |
||
n = len(s$) |
n = len(s$) |
||
print left$(s$, n - 2), "\\", peek$("program_name")</ |
print left$(s$, n - 2), "\\", peek$("program_name")</syntaxhighlight> |
||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
const debug = std.debug; |
const debug = std.debug; |
||
Line 2,419: | Line 2,419: | ||
debug.warn("{}\n", .{program_name}); |
debug.warn("{}\n", .{program_name}); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
C's argv is exposed to the zkl runtime so if file bbb.zkl contains: |
C's argv is exposed to the zkl runtime so if file bbb.zkl contains: |
||
< |
<syntaxhighlight lang="zkl">#!/Homer/craigd/Bin/zkl |
||
println(System.argv);</ |
println(System.argv);</syntaxhighlight> |
||
Then (on Unix like OSes) |
Then (on Unix like OSes) |
||
< |
<syntaxhighlight lang="zkl">./bbb.zkl |
||
zkl bbb.zkl</ |
zkl bbb.zkl</syntaxhighlight> |
||
both print |
both print |
||
<pre> |
<pre> |
||
Line 2,433: | Line 2,433: | ||
</pre> |
</pre> |
||
On Unix, zkl is actually a shell script: |
On Unix, zkl is actually a shell script: |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
# A bash script to run zkl if you haven't jumped |
# A bash script to run zkl if you haven't jumped |
||
# through all the Unix hoops to put the bits in the "right" places |
# through all the Unix hoops to put the bits in the "right" places |
||
Line 2,447: | Line 2,447: | ||
export zklRoot |
export zklRoot |
||
#set -o noglob |
#set -o noglob |
||
LD_LIBRARY_PATH=$zklRoot/Lib $zklRoot/Bin/zkl "$@"</ |
LD_LIBRARY_PATH=$zklRoot/Lib $zklRoot/Bin/zkl "$@"</syntaxhighlight> |
||
On Windows, no launch script (windows knows where the DLLs are) but argv[0] can be messed up. |
On Windows, no launch script (windows knows where the DLLs are) but argv[0] can be messed up. |
||