Command-line argument parsing: Difference between revisions

Content deleted Content added
Jooja (talk | contribs)
Add Kotlin language example
m remove extraneous characters from perl code sample
 
(11 intermediate revisions by 6 users not shown)
Line 8:
 
An example of [[C (programming language)|C]] argument parsing would be:
<syntaxhighlight lang="Cc">
#include <stdio.h>
int main (int argc, char *argv[])
{
int count;
for (count = 0; count < argc; count++)
puts (argv[count]);
}
</syntaxhighlight>
C also has functions called [[getopt]] and getopt_long.
 
===C#===
An example of [[C Sharp (programming language)|C#]] argument parsing would be:
<syntaxhighlight lang="csharp">
class Program
{
static void Main(string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
}
}
</syntaxhighlight>
 
===Java===
Line 30 ⟶ 43:
}
</syntaxhighlight>
 
 
===Kotlin===
Here are some possible ways to print arguments in [[Kotlin (programming language)|Kotlin]]:<ref>{{cite web|url=https://kotlinlang.org/docs/basic-syntax.html#program-entry-point |title=Kotlin: Basic syntax |date= |accessdate=2022-05-13}}</ref>:
 
<syntaxhighlight lang="kotlin">
Line 49 ⟶ 61:
}
</syntaxhighlight>
 
 
===Perl===
Line 55 ⟶ 66:
 
<syntaxhighlight lang="perl">
foreach $arg (@ARGV)GT
{
print $arg;
}
</syntaxhighlight>FT
or
<syntaxhighlight lang="perl">
foreach $argnum (0 .. $#ARGV)ST
{
print $ARGV[$argnum];
Line 85 ⟶ 96:
<syntaxhighlight lang="php">
$args = parseArgs($argv);
echo getArg($args, '"foo'");
 
function parseArgs(array $args)
{
foreach ($args as $arg) {
$tmp = explode('":'", $arg, 2);
if ($arg[0] === '"-'") {
$args[substr($tmp[0], 1)] = $tmp[1];
}
Line 159 ⟶ 170:
</syntaxhighlight>
 
===Node.jsRust===
The args are in <code>env::args()</code>.<ref>{{cite web |title=Accepting Command Line Arguments - The Rust Programming Language |url=https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html |website=doc.rust-lang.org |access-date=22 December 2022}}</ref>
<syntaxhighlight lang="rust">
use std::env;
 
fn main() {
let args: Vec<String> = env::args().collect();
 
let query = &args[1];
let file_path = &args[2];
 
println!("Searching for {}", query);
println!("In file {}", file_path);
}
</syntaxhighlight>
 
===JavaScript===
====Node.js====
[[JavaScript]] programs written for [[Node.js]] use the <code>process.argv</code> global variable.<ref>{{cite web|title=process.argv|work=Node.js v10.16.3 Documentation|url=https://nodejs.org/docs/latest-v10.x/api/process.html#process_process_argv|accessdate=3 October 2019}}</ref>
 
Line 191 ⟶ 219:
'two=three',
'four' ]
</syntaxhighlight>
 
====Bun====
JavaScript written for [[Bun (software)|Bun]] use <code>Bun.argv</code> and the <code>util.parseArgs</code> function.<ref>{{cite web |title=Parse command-line arguments {{!}} Bun Examples |url=https://bun.sh/guides/process/argv |website=Bun |language=en}}</ref>
 
<syntaxhighlight lang="javascript">
console.log(Bun.argv);
</syntaxhighlight>
 
====Deno====
JavaScript written for [[Deno (software)|Deno]] use <code>Deno.args</code><ref>{{cite web |title=Deno.args |url=https://docs.deno.com/api/deno/~/Deno.args |website=docs.deno.com}}</ref> and the <code>parseArgs</code> function.<ref>{{cite web |title=parseArgs from parse-args - @std/cli - JSR |url=https://jsr.io/@std/cli/doc/parse-args/~/parseArgs |website=jsr.io |language=en}}</ref>
 
<syntaxhighlight lang="javascript">
console.log(Deno.args);
</syntaxhighlight>