Parse command-line arguments: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added Prolog)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 112:
 
=={{header|Clojure}}==
See [https://github.com/clojure-cookbook/clojure-cookbook/blob/master/03_general-computing/3-07_parse-command-line-arguments.asciidoc Parsing Command-Line Arguments] from O'Reilly's Clojure Cookbook github.
 
=={{header|D}}==
Line 323:
-> {math, -v, -n, -z, -w, 1, 192.168.1.2, 1-1000}
</lang>
=={{header|PARI/GP}}==
GP exists in a REPL and so it doesn't make sense to parse command-line arguments. But PARI can parse them just like [[#C|C]]:
<lang c>#include <pari/pari.h>
#include <stdio.h>
 
int main(int argc, char **argv){
if(strcmp(argv[1],"-n"))
pari_printf("8 + 1 = %Ps\n", addii(int2u(3), gen_1));
return 0;
}</lang>
 
=={{header|Nim}}==
Line 401 ⟶ 391:
Got arg 3: "1-1000"
</pre>
 
=={{header|PARI/GP}}==
GP exists in a REPL and so it doesn't make sense to parse command-line arguments. But PARI can parse them just like [[#C|C]]:
<lang c>#include <pari/pari.h>
#include <stdio.h>
 
int main(int argc, char **argv){
if(strcmp(argv[1],"-n"))
pari_printf("8 + 1 = %Ps\n", addii(int2u(3), gen_1));
return 0;
}</lang>
 
=={{header|Perl}}==
Line 439 ⟶ 440:
Outputting to 'test.txt' path, with Verbosity and a length of 190.
</pre>
 
=={{header|Perl 6}}==
At the end of running any top-level code (which can preprocess the arguments if it likes), Perl 6 automatically examines any remaining arguments and transforms them into a call to a <tt>MAIN</tt> routine, if one is defined. The arguments are parsed based on the signature of the routine, so that options are mapped to named arguments.
<lang perl6>sub MAIN (Bool :$b, Str :$s = '', Int :$n = 0, *@rest) {
say "Bool: $b";
say "Str: $s";
say "Num: $n";
say "Rest: @rest[]";
}</lang>
{{out}}
<pre>$ ./main -h
Usage:
./main [-b] [-s=<Str>] [-n=<Int>] [<rest> ...]
 
$ ./main -b -n=42 -s=turtles all the way down
Bool: True
Str: turtles
Num: 42
Rest: all the way down</pre>
If there are multiple <tt>MAIN</tt> subs, they are differentiated by multiple dispatch. A help message can automatically be generated for all the variants. The intent of this mechanism is not to cover every possible switch structure, but just to make it drop-dead easy to handle most of the common ones.
 
=={{header|Phix}}==
Line 477 ⟶ 458:
</pre>
 
=={{header|PicoLisp}}==
PicoLisp doesn't have a library to get options. Instead, the command line is parsed at startup and handled in the following way: Each command line argument is executed (interpreted) as a Lisp source file, except that if the first character is a hypen '-', then that arguments is taken as a Lisp function call (without the surrounding parentheses). For example, the command line
<lang shell>$ ./pil abc.l -foo def.l -"bar 3 4" -'mumble "hello"' -bye</lang>
has the effect that
# The file "abc.l" is executed
# (foo) is called
# The file "def.l" is executed
# (bar 3 4) is called
# (mumble "hello") is called
# (bye) is called, resulting in program termination
Command line arguments like "-v", "-n" and "-z" can be implemented simply by defining three functions 'v', 'n' and 'z'.
 
In addition to the above mechanism, the command line can also be handled "manually", by either processing the list of arguments returned by '[http://software-lab.de/doc/refA.html#argv argv]', or by fetching arguments individually with '[http://software-lab.de/doc/refO.html#opt opt]'.
 
=={{header|PowerShell}}==
Powershell functions and filters handle options organically, with advanced .NET support to handle complex and advanced options including aliases, ranges, sets, datatypes, and more. [https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.core/about/about_parsing See] [https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Functions more] [https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.core/about/about_functions_advanced here]. However, to parse options 'classically', you can write a custom parser. A slightly messy version (inspired by ruby optparse) that can only handle switches and relies on RegEx:
<lang Powershell>
$options = @{
opt1 = [bool] 0
opt2 = [bool] 0
opt3 = [bool] 0
}
$help = @"
FUNCTION usage: FUNCTION [-p] [-w] [-h] [-c] <int><float><string>PARAMETERS...
Lorem Ipsum blah blah blah
NOTE something yada yada
Options:
-p,--pxx Name Some option that has significance with the letter 'p'
-w,--wxx Name Some option that has significance with the letter 'w'
-c,--cxx Name Some option that has significance with the letter 'c'
-h,--help Help Prints this message
"@
 
function parseOptions ($argv,$options) {
$opts = @()
if (!$argv) { return $null }
foreach ($arg in $argv) {
# Make sure the argument is something you are expecting
$test = ($arg -is [int]) -or
($arg -is [string]) -or
($arg -is [float])
if (!$test) {
Write-Host "Bad argument: $arg is not an integer, float, nor string." -ForegroundColor Red
throw "Error: Bad Argument"
}
if ($arg -like '-*') { $opts += $arg }
}
$argv = [Collections.ArrayList]$argv
if ($opts) {
foreach ($opt in $opts) {
switch ($opt) {
{'-p' -or '--pxx'} { $options.opt1 = [bool] 1 }
{'-w' -or '--wxx'} { $options.opt2 = [bool] 1 }
{'-c' -or '--cxx'} { $options.opt3 = [bool] 1 }
{'-h' -or '--help'} { Write-Host $help -ForegroundColor Cyan; break 1 }
default {
Write-Host "Bad option: $opt is not a valid option." -ForegroundColor Red
throw "Error: Bad Option"
}
}
$argv.Remove($opt)
}
}
return [array]$argv,$options
}#fn
</lang>
Usage (in some function or script):
<lang Powershell>
 
$argv,$options = parseOptions $args $options
 
if ($options.opt3) {
$foo = $blah - ($yada * $options.opt1) + ($yada * $options.opt2)
$bar = $argv | SomeOtherFilter | Baz
}
</lang>
Usage in shell:
<lang shell>
$> function -c --wxx arg1 arg2
</lang>
Note that this works in Powershell. All of the arguments after the function name will be passed as strings to the function, which then calls them as an array with the automatic variable, $args. The custom parser/function does the work from there, turning the strings into flags and typed arguments. WARNING: This is reinventing the wheel to an extreme degree.
 
=={{header|Prolog}}==
Line 571 ⟶ 636:
<yourscript> --file=outfile -q
</lang>
 
=={{header|PicoLisp}}==
PicoLisp doesn't have a library to get options. Instead, the command line is parsed at startup and handled in the following way: Each command line argument is executed (interpreted) as a Lisp source file, except that if the first character is a hypen '-', then that arguments is taken as a Lisp function call (without the surrounding parentheses). For example, the command line
<lang shell>$ ./pil abc.l -foo def.l -"bar 3 4" -'mumble "hello"' -bye</lang>
has the effect that
# The file "abc.l" is executed
# (foo) is called
# The file "def.l" is executed
# (bar 3 4) is called
# (mumble "hello") is called
# (bye) is called, resulting in program termination
Command line arguments like "-v", "-n" and "-z" can be implemented simply by defining three functions 'v', 'n' and 'z'.
 
In addition to the above mechanism, the command line can also be handled "manually", by either processing the list of arguments returned by '[http://software-lab.de/doc/refA.html#argv argv]', or by fetching arguments individually with '[http://software-lab.de/doc/refO.html#opt opt]'.
 
=={{header|PowerShell}}==
Powershell functions and filters handle options organically, with advanced .NET support to handle complex and advanced options including aliases, ranges, sets, datatypes, and more. [https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.core/about/about_parsing See] [https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Functions more] [https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.core/about/about_functions_advanced here]. However, to parse options 'classically', you can write a custom parser. A slightly messy version (inspired by ruby optparse) that can only handle switches and relies on RegEx:
<lang Powershell>
$options = @{
opt1 = [bool] 0
opt2 = [bool] 0
opt3 = [bool] 0
}
$help = @"
FUNCTION usage: FUNCTION [-p] [-w] [-h] [-c] <int><float><string>PARAMETERS...
Lorem Ipsum blah blah blah
NOTE something yada yada
Options:
-p,--pxx Name Some option that has significance with the letter 'p'
-w,--wxx Name Some option that has significance with the letter 'w'
-c,--cxx Name Some option that has significance with the letter 'c'
-h,--help Help Prints this message
"@
 
function parseOptions ($argv,$options) {
$opts = @()
if (!$argv) { return $null }
foreach ($arg in $argv) {
# Make sure the argument is something you are expecting
$test = ($arg -is [int]) -or
($arg -is [string]) -or
($arg -is [float])
if (!$test) {
Write-Host "Bad argument: $arg is not an integer, float, nor string." -ForegroundColor Red
throw "Error: Bad Argument"
}
if ($arg -like '-*') { $opts += $arg }
}
$argv = [Collections.ArrayList]$argv
if ($opts) {
foreach ($opt in $opts) {
switch ($opt) {
{'-p' -or '--pxx'} { $options.opt1 = [bool] 1 }
{'-w' -or '--wxx'} { $options.opt2 = [bool] 1 }
{'-c' -or '--cxx'} { $options.opt3 = [bool] 1 }
{'-h' -or '--help'} { Write-Host $help -ForegroundColor Cyan; break 1 }
default {
Write-Host "Bad option: $opt is not a valid option." -ForegroundColor Red
throw "Error: Bad Option"
}
}
$argv.Remove($opt)
}
}
return [array]$argv,$options
}#fn
</lang>
Usage (in some function or script):
<lang Powershell>
 
$argv,$options = parseOptions $args $options
 
if ($options.opt3) {
$foo = $blah - ($yada * $options.opt1) + ($yada * $options.opt2)
$bar = $argv | SomeOtherFilter | Baz
}
</lang>
Usage in shell:
<lang shell>
$> function -c --wxx arg1 arg2
</lang>
Note that this works in Powershell. All of the arguments after the function name will be passed as strings to the function, which then calls them as an array with the automatic variable, $args. The custom parser/function does the work from there, turning the strings into flags and typed arguments. WARNING: This is reinventing the wheel to an extreme degree.
 
=={{header|Racket}}==
Line 722 ⟶ 702:
Operations: add, add, edit, delete
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
At the end of running any top-level code (which can preprocess the arguments if it likes), Perl 6 automatically examines any remaining arguments and transforms them into a call to a <tt>MAIN</tt> routine, if one is defined. The arguments are parsed based on the signature of the routine, so that options are mapped to named arguments.
<lang perl6>sub MAIN (Bool :$b, Str :$s = '', Int :$n = 0, *@rest) {
say "Bool: $b";
say "Str: $s";
say "Num: $n";
say "Rest: @rest[]";
}</lang>
{{out}}
<pre>$ ./main -h
Usage:
./main [-b] [-s=<Str>] [-n=<Int>] [<rest> ...]
 
$ ./main -b -n=42 -s=turtles all the way down
Bool: True
Str: turtles
Num: 42
Rest: all the way down</pre>
If there are multiple <tt>MAIN</tt> subs, they are differentiated by multiple dispatch. A help message can automatically be generated for all the variants. The intent of this mechanism is not to cover every possible switch structure, but just to make it drop-dead easy to handle most of the common ones.
 
=={{header|REXX}}==
10,327

edits