I'm working with a python api that has a function that takes an argument for a filter type. The options are as follows:
Metashape.NoFiltering
Metashape.MildFiltering
Metashape.ModerateFiltering
Metashape.AggressiveFiltering
I'm developing a cli that calls this function and have an argument for filtering mode (using argparse) that has str options for "none", "mild", "moderate", "aggressive".
parser.add_argument(
"--filter-mode",
choices=["none", "mild", "moderate", "aggressive"],
default="moderate",
help="filter mode for depth maps",
)
What would be the best way to map the input string argument to the required filtering type? 2 approaches immediately come to mind for me:
- function that converts string to FilterMode
def string_to_filter_mode(string: str) -> Metashape.FilterMode:
if string == "none":
return Metashape.NoFiltering
if string == "mild":
return Metashape.MildFiltering
if string == "moderate":
return Metashape.ModerateFiltering
if string == "aggressive":
return Metashape.AggressiveFiltering
raise ValueError
usage:
chunk.buildDepthMap(filter_mode=string_to_filter_mode(filter_mode))
- Create a dictionary mapping string value to type
METASHAPE_FILTER_MODES = {
"none": Metashape.NoFiltering,
"mild": Metashape.MildFiltering,
"moderate": Metashape.ModerateFiltering,
"aggressive": Metashape.AggressiveFiltering,
}
usage:
chunk.buildDepthMap(filter_mode=METASHAPE_FILTER_MODES[filter_mode])
Both ways seem pretty simple and extendable but which option would be the preferred method for doing this, as the application I'm working on has various similar types of arguments. Or is there a way to handle this directly in argparse?