Closed donaldcampbelljr closed 1 month ago
This PR is a bit of a hack to get short arguments working again.
Another suggested workaround was given by the creator of pydantic-argparse: https://github.com/SupImDos/pydantic-argparse/issues/44#issuecomment-2102307590
such that:
# Create parser with model
parser = pydantic_argparse.ArgumentParser(model=...)
# Add `-s` short argument for existing `--string` argument
argument = parser._option_string_actions["--string"]
argument.option_strings = ("-s", "--string")
parser._option_string_actions["-s"] = argument
My approach modifies the input string before sending it to the parser.
After yesterday's discussion, I implemented the short form arguments using the suggested approach where I add them directly to the parser object immediately after it is created:
parser = pydantic_argparse.ArgumentParser(model=...)
# Add `-s` short argument for existing `--string` argument
argument = parser._option_string_actions["--string"]
argument.option_strings = ("-s", "--string")
parser._option_string_actions["-s"] = argument
I created a new function to do this and have it residing in the same file as the parser generation. Within that function are all of the key value pairs for the short and long arguments.
I modified this so that the add_short_arguments
function uses the alias on the Argument object as the short argument. This allows for both the long and short argument to be maintainable in the same place.
Example:
DRY_RUN = Argument(
name="dry_run",
alias="-d",
default=(bool, False),
description="Don't actually submit jobs",
)
Name is used to create the long argument. I use the alias later to add the short argument after parser creation.
Addresses the last part of: https://github.com/pepkit/looper/issues/438
An attempt to create a workaround so that we can use shortform arguments with the pydantic-argparse package.