Parse command-line arguments: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m →{{header|Raku}}: Undo bizarre single space indent that somebody found necessary to do |
m →{{header|Wren}}: Changed to Wren S/H |
||
(5 intermediate revisions by 5 users not shown) | |||
Line 8:
Many languages provide a library (getopt or GetOpt) to parse the raw command line options in an intelligent way.
<br><br>
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Show command-line arguments
-- J. Carter 2023 Apr
-- The task is called "Parse command-line arguments", but as parsing requires attaching meaning to arguments, and the task
-- specification does not do so, showing them is all we can reasonably do
with Ada.Command_Line;
with Ada.Text_IO;
procedure Show_Args is
-- Empty
begin -- Show_Args
All_Args : for Arg in 1 .. Ada.Command_Line.Argument_Count loop
Ada.Text_IO.Put_Line (Item => Arg'Image & ": " & Ada.Command_Line.Argument (Arg) );
end loop All_Args;
end Show_Args;
</syntaxhighlight>
{{out}}
<pre>
$ ./show_args nc -v -n -z -w 1 192.168.1.2 1-1000
1: nc
2: -v
3: -n
4: -z
5: -w
6: 1
7: 192.168.1.2
8: 1-1000
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">loop.with:'i arg 'a ->
print ["argument" (to :string i)++":" a]
loop args [k,v]->
if k <> "values" ->
print ["found option:" k "with value:" v "(of type:" (to :string type v)++")"]</syntaxhighlight>
'''Sample input:'''
<pre>arturo parse\ command-line\ arguments.art one two --file:four --verbose --loop:3 three</pre>
'''Sample output:'''
<pre>argument 0: one
argument 1: two
argument 2: --file:four
argument 3: --verbose
argument 4: --loop:3
argument 5: three
found option: file with value: four (of type: string)
found option: verbose with value: true (of type: logical)
found option: loop with value: 3 (of type: integer)</pre>
=={{header|AutoHotkey}}==
Line 321 ⟶ 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 441 ⟶ 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,248 ⟶ 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="
var args = Process.arguments
Line 1,261 ⟶ 1,424:
{{out}}
<pre>
$ wren_cli
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>
|