Arguments
An arc argument is a parameter which get passed to the command positionally. They are defined using regular Python function arguments.
import arc
@arc.command
def hello(firstname: str, lastname: str | None):
name = firstname
if lastname:
name += f" {lastname}"
arc.print(f"Hello, {name}! Hope you have a wonderful day!")
hello()
str
, but the second argument as str | None
? We do this because we want firstname
to be required and lastname
to be optional. By giving lastname
a union type that includes None
, we're letting arc know that if no value is passed in from the command line, it's ok for it to be None
.
Using None
In arc, defining parameters as allowing None
, can be done in several ways, all of which are equivelant. The following are all equivelant:
None
, you don't actually have to annotate as such if not desired
We can see it in action here
And adding the optional lastname
Documentation¶
$ python parameter_argument.py --help
USAGE
parameter_argument.py [-h] firstname [lastname]
ARGUMENTS
firstname
lastname
OPTIONS
--help (-h) Displays this help message
Note
The brackets around lastname
in the USAGE indicate that it is optional
Default Values¶
Often, we don't just want a None
when a value isn't provided, but we want some sort of default. This can be accomplished by simply giving the argument a default value.
import arc
@arc.command
def hello(firstname: str, lastname: str = "Joestar"):
name = f"{firstname} {lastname}"
arc.print(f"Hello, {name}! Hope you have a wonderful day!")
hello()
None
in the type signature. Now that we have a default, the value will always be a string, and we don't need to tell arc that the value is optional.
Check it:
$ python parameter_default.py Sean
Hello, Sean Joestar! Hope you have a wonderful day!
$ python parameter_default.py Sean Collings
Hello, Sean Collings! Hope you have a wonderful day!
Alternative Syntax¶
arc has an alternative syntax fo defining arguments
Take this argument from the last example
Could also be defined as:
Now, arc.Argument
is unnecessary here, but comes with some additional bells and whistles that make it more useful. Some of these features will be explored in future guides, or you check the reference for full details on what it provides.