Command-line arguments: Difference between revisions

no edit summary
No edit summary
 
(6 intermediate revisions by 6 users not shown)
Line 93:
 
For an EXE file, both the <code>DS</code> and <code>ES</code> registers are set to the program segment prefix as soon as the program begins. You'll need to load from offset 81h to FFh to get the command line arguments.
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program commandLine64.s */
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szCarriageReturn: .asciz "\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
.align 4
/************************************/
/* code section */
/************************************/
.text
.global main
main: // entry of program
mov fp,sp // fp <- start address
ldr x4,[fp] // number of Command line arguments
add x5,fp,#8 // first parameter address
mov x2,#0 // init loop counter
1:
ldr x0,[x5,x2,lsl #3] // string address parameter
bl affichageMess // display string
ldr x0,qAdrszCarriageReturn
bl affichageMess // display carriage return
add x2,x2,#1 // increment counter
cmp x2,x4 // number parameters ?
blt 1b // loop
 
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc 0 // perform the system call
 
qAdrszCarriageReturn: .quad szCarriageReturn
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
~/.../rosetta/asm4 $ commandLine64 toto tutu
commandLine64
toto
tutu
</pre>
 
=={{header|Ada}}==
Line 627 ⟶ 687:
std::cout << "The argument #" << i << " is " << argv[i] << '\n';
}</syntaxhighlight>
 
=={{header|C3}}==
 
Command line arguments are passed to main and will be converted to UTF-8 strings on all platforms.
 
<syntaxhighlight lang="c3">import std::io;
 
fn void main(String[] args)
{
io::printfn("This program is named %s.", args[0]);
for (int i = 1; i < args.len; i++)
{
io::printfn("the argument #%d is %s\n", i, args[i]);
}
}</syntaxhighlight>
 
 
=={{header|Clean}}==
Line 968 ⟶ 1,044:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 974 ⟶ 1,050:
public program()
{
program_arguments.forEvery::(int i)
{ console.printLine("Argument ",i," is ",program_arguments[i]) }
}</syntaxhighlight>
Line 1,127 ⟶ 1,203:
<pre>
The program was invoked like this => myprogram -c alpha beta -h gamma
</pre>
 
=={{header|Free Pascal}}==
<syntaxhighlight lang="pascal">
Program listArguments(input, output, stdErr);
 
Var
i: integer;
Begin
writeLn('program was called from: ',paramStr(0));
For i := 1 To paramCount() Do
Begin
writeLn('argument',i:2,' : ', paramStr(i));
End;
End.
</syntaxhighlight>
{{out}}
<pre>
./Commandlinearguments -c "alpha beta" -h "gamma"
program was called from: /home/user/Documents/GitHub/rosettacode/Commandlinearguments
argument 1 : -c
argument 2 : alpha beta
argument 3 : -h
argument 4 : gamma
</pre>
 
Line 1,459 ⟶ 1,511:
}
</syntaxhighlight>
Calling a langLang program with command line arguments depends on the implementation.
The following example shows, how this can be achieved in Standard Lang (-- defines the start of the command line arguments for the langLang program):
<syntaxhighlight lang="shell">
$ lang cmdarg.lang -- 2 abc test text
Line 1,781 ⟶ 1,833:
for arg in commandLineParams():
echo arg</syntaxhighlight>
 
=={{header|Nu}}==
In Nu, the special <code>main</code> function can be declared, which gets passed the cli arguments.
<syntaxhighlight lang="nu">
def main [...argv] {
$argv | print
}
</syntaxhighlight>
{{out}}
<pre>
~> nu cli.nu A B C "Hello World!"
╭───┬──────────────╮
│ 0 │ A │
│ 1 │ B │
│ 2 │ C │
│ 3 │ Hello World! │
╰───┴──────────────╯
</pre>
 
=={{header|Oberon-2}}==
Line 1,932 ⟶ 2,002:
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Depends on implementation.
<syntaxhighlight lang="pascal">
Program listArguments(input, output, stdErr);
 
Var
i: integer;
Begin
writeLn('program was called from: ',paramStr(0));
For i := 1 To paramCount() Do
Begin
writeLn('argument',i:2,' : ', paramStr(i));
End;
End.
</syntaxhighlight>
{{out}}
<pre>
./Commandlinearguments -c "alpha beta" -h "gamma"
program was called from: /home/user/Documents/GitHub/rosettacode/Commandlinearguments
argument 1 : -c
argument 2 : alpha beta
argument 3 : -h
argument 4 : gamma
</pre>
 
 
=={{header|Perl}}==
Line 2,752 ⟶ 2,845:
 
=={{header|Wren}}==
This assumes that the following script, myscriptCommand-line_arguments.wren, is run as follows:
$ wren myscriptCommand-line_arguments.wren -c "alpha beta" -h "gamma"
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
 
System.print(Process.arguments)</syntaxhighlight>
44

edits