Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for shortform arguments #489

Merged
merged 9 commits into from
May 15, 2024
Merged

Conversation

donaldcampbelljr
Copy link
Contributor

@donaldcampbelljr donaldcampbelljr commented May 13, 2024

An attempt to create a workaround so that we can use shortform arguments with the pydantic-argparse package.

@donaldcampbelljr donaldcampbelljr changed the title test workaround for shortform arguments Workaround for shortform arguments May 13, 2024
@donaldcampbelljr
Copy link
Contributor Author

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:
SupImDos/pydantic-argparse#44 (comment)

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.

@donaldcampbelljr
Copy link
Contributor Author

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.

@donaldcampbelljr
Copy link
Contributor Author

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.

@donaldcampbelljr donaldcampbelljr merged commit 36bc061 into dev May 15, 2024
3 checks passed
@donaldcampbelljr donaldcampbelljr deleted the dev_pydantic_arguments_fix branch May 15, 2024 19:35
@donaldcampbelljr
Copy link
Contributor Author

Addresses the last part of: #438

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant