Command-line arguments: Difference between revisions

no edit summary
(Jakt)
No edit summary
 
(9 intermediate revisions by 8 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 617 ⟶ 677:
Command line arguments are passed the same way as in C.
 
This example uses <code><iostream></code>. Traditional C-style Ii/Oo also works.
 
<syntaxhighlight lang="cpp">#include <iostream>
 
int main(int argc, const char* argv[]) {
std::cout << "This program is named " << argv[0] << '\n'
{
std::cout << "ThisThere program is namedare " << argv[0]argc - 1 << std::endl" arguments given.\n";
for (int i = 1; i < argc; ++i)
std::cout << "There are " << argc-1 << " arguments given." << std::endl;
std::cout << "The argument #" << i << " is " << argv[i] << '\n';
for (int i = 1; i < argc; ++i)
}</syntaxhighlight>
std::cout << "the argument #" << i << " is " << argv[i] << std::endl;
 
=={{header|C3}}==
return 0;
 
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 971 ⟶ 1,044:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 977 ⟶ 1,050:
public program()
{
program_arguments.forEvery::(int i)
{ console.printLine("Argument ",i," is ",program_arguments[i]) }
}</syntaxhighlight>
Line 1,130 ⟶ 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,309 ⟶ 1,358:
 
=={{header|Java}}==
The arguments will be passed to <code>main</code> as the only parameter.<br />
 
The array is non-null.
<syntaxhighlight lang="java">
public static void main(String[] args)
</syntaxhighlight>
Running this command
<syntaxhighlight lang="bash">
myprogram -c "alpha beta" -h "gamma"
</syntaxhighlight>
Will produce the following
<pre>
-c
alpha beta
-h
gamma
</pre>
<br />
And alternate demonstration.
<syntaxhighlight lang="java">public class Arguments {
public static void main(String[] args) {
Line 1,445 ⟶ 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,767 ⟶ 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,918 ⟶ 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,738 ⟶ 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