Command-line arguments: Difference between revisions

no edit summary
(Adds slope example)
No edit summary
 
(21 intermediate revisions by 16 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,299 ⟶ 1,348:
In a non-interactive context, we would instead use <code>echo ARGV</code>.
 
=={{header|JavaJakt}}==
The main function can recieve the command line arguments as an arbitrarily named array of String. argv[0] will be the name of the program.
 
<syntaxhighlight lang="jakt">
fn main(arguments: [String]) {
println("{}", arguments)
}
</syntaxhighlight>
 
=={{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,329 ⟶ 1,404:
<syntaxhighlight lang="javascript">for (var i = 0; i < arguments.length; i++)
print(arguments[i]);</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">#!/usr/bin/joy
 
argv rest put.</syntaxhighlight>
{{out}}
<pre>["-c" "alpha beta" "-h" "gamma"]</pre>
 
=={{header|jq}}==
Line 1,420 ⟶ 1,502:
{{out}}
See Java output.
 
=={{header|Lang}}==
In lang command line arguments are stored in &LANG_ARGS.
<syntaxhighlight lang="lang">
$ele
foreach($[ele], &LANG_ARGS) {
fn.println($ele)
}
</syntaxhighlight>
Calling a Lang 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 Lang program):
<syntaxhighlight lang="shell">
$ lang cmdarg.lang -- 2 abc test text
</syntaxhighlight>
{{out}}
<pre>
2
abc
test
text
</pre>
 
=={{header|Lasso}}==
Line 1,730 ⟶ 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,789 ⟶ 1,910:
done</syntaxhighlight>
 
=== Using the [httphttps://camlocaml.inria.fr/pub/docs/manual-ocamlorg/librefapi/Arg.html Arg] module ===
 
<syntaxhighlight lang="ocaml">(* default values *)
let somebool = ref false
Line 1,810 ⟶ 1,930:
(fun x -> raise (Arg.Bad ("Bad argument : " ^ x)))
usage;
Printf.printf " %b %d '%s'\n" !somebool !someint !somestr</syntaxhighlight>
<pre>
$ ocaml arg.ml --help
usage: arg.ml [-b] [-s string] [-d int]
-b : set somebool to true
-s : what follows -s sets some string
-d : some int parameter
-help Display this list of options
--help Display this list of options
 
$ ocaml arg.ml -d 4 -b -s blabla
Printf.printf " %b %d '%s'\n" !somebool !someint !somestr;
true 4 'blabla'
;;</syntaxhighlight>
 
$ ocaml arg.ml
false 0 ''
</pre>
 
=={{header|Odin}}==
% ocaml arg.ml --help
 
usage: tmp.ml [-b] [-s string] [-d int]
<syntaxhighlight lang="odin">package main
-b : set somebool to true
 
-s : what follows -s sets some string
import "core:os"
-d : some int parameter
import "core:fmt"
--help Display this list of options
 
main :: proc() {
% ocaml arg.ml -d 4 -b -s blabla
fmt.println(os.args)
true 4 'blabla'
}
 
% ocaml arg.ml
// Run: ./program -c "alpha beta" -h "gamma"
false 0 <nowiki>''</nowiki>
// Output: ["./program", "-c", "alpha beta", "-h", "gamma"]
</syntaxhighlight>
 
=={{header|Oforth}}==
Line 1,868 ⟶ 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,034 ⟶ 2,191:
Write-Host Argument (++$i) is $s
}</syntaxhighlight>
 
=={{header|Prolog}}==
The command line arguments supplied to a Prolog interpreter can be accessed by passing <code>os_argv</code> to <code>current_prolog_flag/2</code>.
<syntaxhighlight lang="prolog">:-
current_prolog_flag(os_argv, Args),
write(Args).</syntaxhighlight>
 
Alternatively, <code>argv</code> can be used to access only the arguments *not* consumed by the Prolog interpreter.
<syntaxhighlight lang="prolog">:-
current_prolog_flag(argv, Args),
write(Args).</syntaxhighlight>
 
This omits the interpreter name, the input Prolog filename, and any other arguments directed at the Prolog interpreter.
 
=={{header|Pure}}==
Line 2,412 ⟶ 2,582:
<syntaxhighlight lang="smalltalk">Smalltalk commandLineArguments printCR.
Smalltalk commandLineArguments do:[:each | each printCR]</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "printargs" )
@( description, "Retrieve the list of command-line arguments given to the program." )
@( description, "Example command line: " )
@( description, "myprogram -c 'alpha beta' -h 'gamma'" )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Command-line_arguments" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure printargs is
begin
put_line( "The command is '" & command_line.command_name & "'" );
for Arg in 1..command_line.argument_count loop
put( "Argument" ) @ (Arg ) @ ( " is '" ) @
( command_line.argument(Arg) ) @ ("'");
new_line;
end loop;
end printargs;</syntaxhighlight>
 
=={{header|Standard ML}}==
Line 2,473 ⟶ 2,668:
[ dup . char: = emit space ] is #=
1 #args [ i #= show-arg ] countedLoop</syntaxhighlight>
 
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
_start: (λ
(textout "Number of arguments: " @numArgs
"\nArgument list: " @progArgs)
)
}</syntaxhighlight>
 
Linux command ('tree3' is the name of Transd interpreter):
tree3 progname -c "alpha beta" -h "gamma"
{{out}}
<pre>
Number of arguments: 5
Argument list: ["progname", "-c", "alpha beta", "-h", "gamma"]
</pre>
 
=={{header|TXR}}==
Line 2,611 ⟶ 2,825:
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
This assumes that the following script, myscript.v, is run as follows:
$ v run myscript.v -c "alpha beta" -h "gamma"
<syntaxhighlight lang="v (vlang)">import os
fn main() {
Line 2,631 ⟶ 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>
Line 2,674 ⟶ 2,888:
{{omit from|Commodore BASIC}}
{{omit from|dc}}
{{omit from|EasyLang|Programs cannot be executed outside the IDE, not even in a command line.}}
{{omit from|GUISS}}
{{omit from|Lotus 123 Macro Scripting}}
44

edits