← Code Compare

Read Command-Line Arguments

Read the arguments a program was invoked with, skipping the program name itself. Most languages expose them as a list or array on a standard global, then the program counts and iterates over them.

Show: GujiGoOCamlHaskellPerlRakuRustPython
Guji
sub main() {
    @args = args()
    print("count: { @args.count() }")
    for $arg in @args {
        print("arg: $arg")
    }
}

Guji's args() intrinsic returns the real command-line arguments as an immutable List[Str], already starting after the source path - so there is no program name to slice off the way os.Args[1:] or sys.argv[1:] do. .count() reports the length and a for loop iterates, binding each element. Sigils stay invariant: the list is @args and each element is $arg. This compiles natively (guji build) as well as running under the interpreter.

Go
package main

import (
	"fmt"
	"os"
)

func main() {
	args := os.Args[1:]
	fmt.Printf("count: %d\n", len(args))
	for _, arg := range args {
		fmt.Printf("arg: %s\n", arg)
	}
}

os.Args is a []string whose first element is the program path, so os.Args[1:] drops it to leave the user arguments. len gives the count and range iterates, discarding the index with _. For flag parsing the standard flag package is idiomatic, but raw access goes through os.Args.

OCaml
let () =
  let args = Array.to_list (Array.sub Sys.argv 1 (Array.length Sys.argv - 1)) in
  Printf.printf "count: %d\n" (List.length args);
  List.iter (fun arg -> Printf.printf "arg: %s\n" arg) args

Sys.argv is a string array with the program name at index 0, so Array.sub takes the tail before converting to a list. List.length reports the count and List.iter runs a side-effecting closure over each element. The standard Arg module handles structured option parsing.

Haskell
import System.Environment (getArgs)
import Text.Printf (printf)

main :: IO ()
main = do
  args <- getArgs
  printf "count: %d\n" (length args)
  mapM_ (printf "arg: %s\n") args

getArgs :: IO [String] returns just the user arguments (the program name comes from getProgName), bound here with <- in the IO monad. length counts them and mapM_ sequences a printing action over each, discarding the unit results. The optparse-applicative library is the idiomatic choice for real flag parsing.

Perl
use strict;
use warnings;

my @args = @ARGV;
print "count: ", scalar(@args), "\n";
print "arg: $_\n" for @args;

@ARGV already holds only the user arguments (the program name lives in $0), so no slicing is needed. Evaluating the array in scalar context with scalar yields its length, and the statement-modifier for loop binds each element to $_. Getopt::Long is the standard module for parsing named options.

Raku
sub MAIN(*@args) {
    say "count: { @args.elems }";
    for @args -> $arg {
        say "arg: $arg";
    }
}

Raku binds command-line arguments straight into MAIN's signature; the slurpy *@args captures all positionals, while @*ARGS is also available globally. .elems gives the count and the for loop binds each element to $arg. Declaring typed or named parameters on MAIN makes Raku generate usage and parsing automatically.

Rust
use std::env;

fn main() {
    let args: Vec<String> = env::args().skip(1).collect();
    println!("count: {}", args.len());
    for arg in &args {
        println!("arg: {}", arg);
    }
}

env::args() is an iterator whose first item is the program name, so .skip(1) drops it before collect builds a Vec<String>. .len() reports the count and iterating over &args borrows each element rather than moving it. For nontrivial CLIs the clap crate is the community standard.

Python
import sys


def main() -> int:
    args = sys.argv[1:]
    print(f"count: {len(args)}")
    for arg in args:
        print(f"arg: {arg}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

sys.argv is a list of strings whose element 0 is the script name, so the [1:] slice keeps only the user arguments. len gives the count and a plain for loop iterates. The standard argparse module is preferred when you need typed options, help text, and validation.