pepkit / looper

A job submitter for Portable Encapsulated Projects
http://looper.databio.org
BSD 2-Clause "Simplified" License
20 stars 7 forks source link

Workaround for shortform arguments #489

Closed donaldcampbelljr closed 1 month ago

donaldcampbelljr commented 1 month ago

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

donaldcampbelljr commented 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.

donaldcampbelljr commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

Addresses the last part of: https://github.com/pepkit/looper/issues/438