Parse command-line arguments: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Ada solution)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 378:
Other concepts are also possible...
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
The jq and gojq programs parse some command-line options and arguments for their own purposes
but also provide two mechanisms allowing for arbitrarily many command-line arguments to be provided to the program:
 
* the --args option can be used to provide a sequence of shell strings that are converted to JSON strings;
* the --jsonargs option can similarly be used to specify a sequence of JSON values.
 
For example, assuming a bash or bash-like shell, the invocation
<pre>
jq -n '$ARGS' --args 1 two '[3, "four"]'
</pre>
results in:
<pre>
{
"positional": [
"1",
"two",
"[3, \"four\"]"
],
"named": {}
}
</pre>
whereas:
<pre>
jq -n '$ARGS' --jsonargs 1 '"two"' '[3, "four"]'
</pre>
results in:
<pre>
{
"positional": [
1,
"two",
[
3,
"four"
]
],
"named": {}
}
</pre>
 
Notice that in the first case, the token `two` has not been quoted, whereas in the second case, it must be presented as `'"two"'`
if it is to be understood as a JSON string.
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 498 ⟶ 544:
Unknown option: x
Got arg 3: "1-1000"
</pre>
 
=={{header|Nu}}==
Parsing can be done through the <code>main</code> function's signature.
<syntaxhighlight lang="nu">
def main [
input: string # File to operate on
output?: string # File to write to
--verbose (-v): # Be verbose
] {
{
Input: $input
Output: $output
Verbose: $verbose
}
}
</syntaxhighlight>
{{out}}
<pre>
~> nu cli.nu input.txt --verbose
╭─────────┬───────────╮
│ Input │ input.txt │
│ Output │ │
│ Verbose │ true │
╰─────────┴───────────╯
~> nu cli.nu input.txt output.txt
╭─────────┬────────────╮
│ Input │ input.txt │
│ Output │ output.txt │
│ Verbose │ false │
╰─────────┴────────────╯
~> nu cli.nu --invalid
Error: nu::parser::unknown_flag
 
× The `main` command doesn't have flag `invalid`.
╭─[<commandline>:1:1]
1 │ main --invalid
· ────┬────
· ╰── unknown flag
╰────
help: Available flags: --verbose(-v), --help(-h). Use `--help` for more information.
 
~> nu cli.nu --help
Usage:
> cli.nu {flags} <input> (output)
 
Flags:
-v, --verbose - Be verbose
-h, --help - Display the help message for this command
 
Parameters:
input <string>: File to operate on
output <string>: File to write to (optional)
 
Input/output types:
╭───┬───────┬────────╮
│ # │ input │ output │
├───┼───────┼────────┤
│ 0 │ any │ any │
╰───┴───────┴────────╯
</pre>
 
Line 1,305 ⟶ 1,411:
=={{header|Wren}}==
Any command line arguments passed to a Wren CLI script are collected in the same order into a list of strings. If individual arguments require any further parsing, this can then be done using normal Wren code.
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
 
var args = Process.arguments
Line 1,318 ⟶ 1,424:
{{out}}
<pre>
$ wren_cli parse_commandParse_command-line_arguments.wren -v -n -z -w 1 192.168.1.2 1-1000
 
The arguments passed are: [-v, -n, -z, -w, 1, 192.168.1.2, 1-1000]
The final argument expressed as a Range object is 1..1000
</pre>
 
=={{header|XPL0}}==
<pre>parseargs nc -v -n -z -w 1 192.168.1.2 1-1000</pre>
<syntaxhighlight lang "XPL0">int N, C;
[N:= 0;
loop [C:= ChIn(8);
if C = $0D \CR\ then quit;
N:= N+1;
CrLf(0); IntOut(0, N); Text(0, ": ");
repeat ChOut(0, C);
C:= ChIn(8);
until C = $20 \space\;
];
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
 
1: nc
2: -v
3: -n
4: -z
5: -w
6: 1
7: 192.168.1.2
8: 1-1000
</pre>
 
9,476

edits