Parameter Sources
While generally, input is parsed from the command line, there are a few other sources that parameters can recieve values from
The precedence of sources is:
- Command Argument
- Command Line
- Environment Variables
- Input Prompt
- Getter Function
- Default Value
Type Conversion
All input sources, except for default values and getter functions, will still pass through the type
conversion systems that arc provides. So you're free to use int
, float
, bool
, or any other type
that you've defined. arc will handle the conversion from enviroment variables and input prompts for you.
Command Argument¶
When an arc command is executed it will check sys.argv
for input. However, you can actually provide explcit input as the first argument to call:
import arc
@arc.command
def hello(name: str):
arc.print(f"Hello {name}!")
hello("Sean")
Note that the command string is treated as if it was the command line input, so if the command string is provided, sys.argv
will be ignored.
Command Line¶
This is the default you're probably used to. If you provide an argument on the command line, it will be parsed as the value for that parameter.
import arc
@arc.command
def hello(name: str):
"""Greets someone by name"""
arc.print(f"Hello {name}!")
hello()
Environment Variables¶
import arc
import os
os.environ["VAL"] = "2"
@arc.command
def command(val: int = arc.Argument(envvar="VAL")):
arc.print(f"VAL: {val}")
command()
VAL
Input Prompt¶
If there is no input provided on the command line for name
(and there was no enviroment variable), arc will prompt the user for input.
import arc
@arc.command
def command(name: str = arc.Argument(prompt="What is your first name? ")):
arc.print("Hello, " + name)
command()
$ python from_prompt.py Jolyne
Hello, Jolyne
$ python from_prompt.py
What is your name? Jolyne
Hello, Jolyne
You can customize the prompt via a configuration parameter.
Getter Function¶
Getter functions are a way to provide a default for an argument, based on the result of a function call.
import arc
def get_default_name():
return "Josuke"
@arc.command
def command(name: str = arc.Argument(get=get_default_name)):
arc.print(f"Good morning {name}")
command()
Different Syntax¶
Getter functions may also be defined using this decorator syntax
import arc
@arc.command
def command(name: str = arc.Argument()):
arc.print(f"Good morning {name}")
@command.get("name")
def get_default_name():
return "Josuke"
command()
Default Value¶
If none of the above are satisfied first, arc will check for a default value of your parameter.
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()
$ 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!
If there is no default (like with firstname
above), arc will emit an error
$ python parameter_default.py
USAGE
parameter_default.py [-h] firstname [lastname]
The following arguments are required: firstname
Checking origin of parameter value¶
You can check what the origin of a value is like this: