Jump to content

Parse command-line arguments: Difference between revisions

Add Nu
imported>Brie
(Add Nu)
Line 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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.