lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
399 stars 49 forks source link

Utility function to shorten the parsing code isn't shown in README #163

Closed szymonk211 closed 1 year ago

szymonk211 commented 1 year ago

I thing that the code to parse simple, most basic use case is a little bit too complicated:

from dataclasses import dataclass
from simple_parsing import ArgumentParser

@dataclass
class HParams:
    """Set of options for the training of a Model."""

    num_layers: int = 4
    num_units: int = 64
    optimizer: str = "ADAM"
    learning_rate: float = 0.001

parser = ArgumentParser()
parser.add_arguments(HParams, dest="hparams")
args = parser.parse_args()
print(args.hparams)

Instead i would like something like this:

from dataclasses import dataclass
from simple_parsing import parse_dataclass

@dataclass
class HParams:
    """Set of options for the training of a Model."""

    num_layers: int = 4
    num_units: int = 64
    optimizer: str = "ADAM"
    learning_rate: float = 0.001

args: HParams = simple_parsing.parse_dataclass(HParams)

The only thing I (and probably the most of the users) need is the simple utility function, that takes the class name, and returns the parsed object. This is what almost every user really wants. Other, more advanced features are important too, but they shouldn't obscure the most frequent use cases.

If you decide to add an utility function I would advice to add the simplified example to the front page, so that every new user would see how simple it is. You could than put more complicated examples. My personal experience, when I first saw the library was that the library is great, exactly something I was looking for, but for some reason it is unnecessary complicated.

lebrice commented 1 year ago

Hello there @szymonk211! Thanks for your comment.

That utility function exists, it's simple_parsing.parse, but it's brand new, and I havent added it to the README or examples just yet.

I'd agree that this probably simplifies things for a lot of new users, but one thing to keep in mind is that one of the biggest selling points for simple_parsing is its backward-compatibility with argparse. My main "target user" is trying to clean up some ugly argparse code, by transitioning one block at a time into a dataclass, and starts to type their codebase.

For that reason, the "complicated" api is still preferred. I don't want people to see this as a complete replacement of argparse per-say. I'll still include an example with the utility function in the readme.

Hope this helps! :)

szymonk211 commented 1 year ago

If they want to clean up some ugly code, than the use of more simple function will only clean up the code more ;) I will close the problem for now anyway if the function is already implemented.

lebrice commented 1 year ago

I'll keep this open until I properly show that function in the README.