Skip to content

Multiple Values

arc allows you to collect multiple values from the command line into a single argument for your comamnd. To do this, you use the collection types: list, set and tuple

list

list_argument.py
import arc


@arc.command()
def main(names: list):
    for name in names:
        arc.print(name)


main()
$ python list_argument.py Jonathen Joseph Jotaro
Jonathen
Joseph
Jotaro
Because list can accept any number of values, you won't be able to add additional arguments after names. Any other positional arguments would have to come before names.

set

Similar to list, but will filter out any non-unique elements.

set_argument.py
import arc


@arc.command()
def main(vals: set):
    arc.print("Unique values:")
    arc.print("\n".join(vals))


main()
$ python set_argument.py 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
Unique values:
4
1
2
3

tuple

Similar to list, but with some additional functionality.

According to PEP 484:

  • tuple represents an arbitrarily sized tuple of any type. In arc, this will behave the same as list
  • tuple[int, ...] represents an arbitrarily sized tuple of integers. In arc, this will behave the same as list[int]
  • tuple[int, int] represents a size-two tuple of integers. In arc, this behavior is unique to tuple as the parameter will only select 2 values from input.

Sub Typing

Collections can be sub-typed so that each item will be converted to the proper type:

sum.py
import arc


@arc.command()
def main(nums: list[int]):
    arc.print("The total is: ", sum(nums))


main()
$ python sum.py 1 2 3 4 5 6 7 8 9
The total is:  45

Collections as Options

When used as an option, it allows the option to be used multiple times:

list_option.py
import arc


@arc.command()
def main(*, names: list):
    for name in names:
        arc.print(name)


main()
$ python list_option.py --names Josuke --names Giorno --names Joylene
Josuke
Giorno
Joylene