Command-line arguments: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 99: Line 99:
end run
end run
</lang>
</lang>

=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
Line 197: Line 198:
}
}
}</lang>
}</lang>



=={{header|Babel}}==
=={{header|Babel}}==
Line 396: Line 396:
(void) printf("the argument #%d is %s\n", i, argv[i]);
(void) printf("the argument #%d is %s\n", i, argv[i]);
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>

=={{header|C++}}==
Command line arguments are passed the same way as in C.

This example uses iostream. Traditional C I/O also works.

<lang cpp>#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "This program is named " << argv[0] << std::endl;
std::cout << "There are " << argc-1 << " arguments given." << std::endl;
for (int i = 1; i < argc; ++i)
std::cout << "the argument #" << i << " is " << argv[i] << std::endl;

return 0;
}</lang>
}</lang>


Line 439: Line 422:
}
}
}
}
}</lang>

=={{header|C++}}==
Command line arguments are passed the same way as in C.

This example uses iostream. Traditional C I/O also works.

<lang cpp>#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "This program is named " << argv[0] << std::endl;
std::cout << "There are " << argc-1 << " arguments given." << std::endl;
for (int i = 1; i < argc; ++i)
std::cout << "the argument #" << i << " is " << argv[i] << std::endl;

return 0;
}</lang>
}</lang>


Line 613: Line 613:
the value of P7 is
the value of P7 is
the value of P8 is</pre>
the value of P8 is</pre>

=={{header|Delphi}}==

<lang delphi>// The program name and the directory it was called from are in
// param[0] , so given the axample of myprogram -c "alpha beta" -h "gamma"

for x := 0 to paramcount do
writeln('param[',x,'] = ',param[x]);

// will yield ( assuming windows and the c drive as the only drive) :

// param[0] = c:\myprogram
// param[1] = -c
// param[2] = alpha beta
// param[3] = -h
// param[4] = gamma
</lang>


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==
Line 636: Line 653:


The order of command line ''options'' is lost.
The order of command line ''options'' is lost.

=={{header|Delphi}}==

<lang delphi>// The program name and the directory it was called from are in
// param[0] , so given the axample of myprogram -c "alpha beta" -h "gamma"

for x := 0 to paramcount do
writeln('param[',x,'] = ',param[x]);

// will yield ( assuming windows and the c drive as the only drive) :

// param[0] = c:\myprogram
// param[1] = -c
// param[2] = alpha beta
// param[3] = -h
// param[4] = gamma
</lang>


=={{header|E}}==
=={{header|E}}==
Line 759: Line 759:
Array.iter (fun x -> printfn "%s" x) args
Array.iter (fun x -> printfn "%s" x) args
0</lang>
0</lang>

=={{header|Factor}}==
=={{header|Factor}}==
USING: io sequences command-line ;
USING: io sequences command-line ;
Line 798: Line 799:
delta
delta
$</lang>
$</lang>

=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|2003 and later}}
{{works with|Fortran|2003 and later}}
Line 1,177: Line 1,179:
dup arg dup 0 = || ,t 1 + repeat
dup arg dup 0 = || ,t 1 + repeat
drop</lang>
drop</lang>

=={{header|Lua}}==
=={{header|Lua}}==


Line 1,187: Line 1,190:
print( i," ", arg[i] )
print( i," ", arg[i] )
end</lang>
end</lang>

=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
function quote$("a") return "a" a string in ""
function quote$("a") return "a" a string in ""
Line 1,222: Line 1,226:
Checkit
Checkit
</lang>
</lang>



=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 1,511: Line 1,514:
% ocaml arg.ml
% ocaml arg.ml
false 0 <nowiki>''</nowiki>
false 0 <nowiki>''</nowiki>



=={{header|Oforth}}==
=={{header|Oforth}}==
Line 1,571: Line 1,573:
'verbose|v' => \my $verbose,
'verbose|v' => \my $verbose,
);</lang>
);</lang>

=={{header|Perl 6}}==
Perl 5's <code>@ARGV</code> is available as <code>@*ARGS</code>. Alternatively, if you define a subroutine named <code>MAIN</code>, Perl will automatically process <code>@*ARGS</code> according to Unix conventions and <code>MAIN</code>'s signature (or signatures, if your <code>MAIN</code> is a multi sub) and then call <code>MAIN</code> with appropriate arguments; see [http://perlcabal.org/syn/S06.html#Declaring_a_MAIN_subroutine Synopsis 6] or [http://perlgeek.de/en/article/5-to-6#post_14|5-to-6].

<lang perl6># with arguments supplied
$ perl6 -e 'sub MAIN($x, $y) { say $x + $y }' 3 5
8

# missing argument:
$ perl6 -e 'sub MAIN($x, $y) { say $x + $y }' 3
Usage:
-e '...' x y</lang>

If the program is stored in a file, the file name is printed instead of <code>-e '...'</code>.


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,819: Line 1,807:


(for ([arg (current-command-line-arguments)]) (displayln arg))</lang>
(for ([arg (current-command-line-arguments)]) (displayln arg))</lang>

=={{header|Raku}}==
(formerly Perl 6)
Perl 5's <code>@ARGV</code> is available as <code>@*ARGS</code>. Alternatively, if you define a subroutine named <code>MAIN</code>, Perl will automatically process <code>@*ARGS</code> according to Unix conventions and <code>MAIN</code>'s signature (or signatures, if your <code>MAIN</code> is a multi sub) and then call <code>MAIN</code> with appropriate arguments; see [http://perlcabal.org/syn/S06.html#Declaring_a_MAIN_subroutine Synopsis 6] or [http://perlgeek.de/en/article/5-to-6#post_14|5-to-6].

<lang perl6># with arguments supplied
$ perl6 -e 'sub MAIN($x, $y) { say $x + $y }' 3 5
8

# missing argument:
$ perl6 -e 'sub MAIN($x, $y) { say $x + $y }' 3
Usage:
-e '...' x y</lang>

If the program is stored in a file, the file name is printed instead of <code>-e '...'</code>.


=={{header|RapidQ}}==
=={{header|RapidQ}}==
Line 2,228: Line 2,231:
./args.v a b c
./args.v a b c
=[args.v a b c]</lang>
=[args.v a b c]</lang>

=={{header|Visual Basic}}==

Like [[#BASIC|Qbasic]], Visual Basic returns all of the args in the built-in variable <code>Command$</code>:
<lang vb>Sub Main
MsgBox Command$
End Sub</lang>

=={{header|Visual Basic .NET}}==

This syntax will tokenize the command line arguments. Tokens are normally delimited by spaces, but spaces can be part of a token if surrounded by quotes.

<lang vbnet>Sub Main(ByVal args As String())
For Each token In args
Console.WriteLine(token)
Next
End Sub</lang>



=={{header|vbScript}}==
=={{header|vbScript}}==
Line 2,268: Line 2,253:
Next
Next
</lang>
</lang>

=={{header|Visual Basic}}==

Like [[#BASIC|Qbasic]], Visual Basic returns all of the args in the built-in variable <code>Command$</code>:
<lang vb>Sub Main
MsgBox Command$
End Sub</lang>

=={{header|Visual Basic .NET}}==

This syntax will tokenize the command line arguments. Tokens are normally delimited by spaces, but spaces can be part of a token if surrounded by quotes.

<lang vbnet>Sub Main(ByVal args As String())
For Each token In args
Console.WriteLine(token)
Next
End Sub</lang>


=={{header|zkl}}==
=={{header|zkl}}==