Course of Raku / Essentials / Positional data types / Exercises / The number of command-line arguments

Solution: The number of command-line arguments

The number of arguments still comes from @*ARGS.elems, but this time we need to do something with it rather than just print it. Save the count in a variable, then pick the right word with a ternary operator:

Code

my $n = @*ARGS.elems;
my $word = $n == 1 ?? 'argument' !! 'arguments';
say "You passed $n $word.";

🦋 Find the program in the file number-of-command-line-arguments.raku.

Output

It is worth testing the three interesting cases: no arguments, exactly one, and many. Only the second one uses the singular form.

$ raku exercises/positionals/number-of-command-line-arguments.raku
You passed 0 arguments.

$ raku exercises/positionals/number-of-command-line-arguments.raku solo
You passed 1 argument.

$ raku exercises/positionals/number-of-command-line-arguments.raku one 2 three 4 five 6 seven
You passed 7 arguments.

Course navigation

The number of command-line arguments   |   Print command-line arguments

Translations of this page: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська