You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With argparse.add_argument it is possible to convert the argument passed on the command line to a different type. It would be useful to have this functionality using datargs, but does not seem possible (e.g., type is not a supported keyword for the arg utility function).
For example, I have a command line option for setting the log level. I would like the end user to be able to specify the level using its name, a string, but stored in the dataclass as an int so that it can be directly used to setup the logging configuration.
It's certainly possible to do the conversion afterwards, but it would be more convenient to have the transformation done during parsing. Currently, I might reuse the same arguments dataclass in multiple programs, which requires me to apply the type transformation in each program, versus having it done transparently by the parser.
[Edit]
One other thing that doing the conversion client side makes less convenient are choices. Using the type conversion with argparse it is easy to make string choices case insensitive, but doing it client side requires the command line argument to match the case exactly.
The text was updated successfully, but these errors were encountered:
datargs is meant to express a simple equivalence between dataclasses and argument parsers so it is not meant for this use. If you can reuse the dataclass, you can also reuse the same utility functions - setting the log level, just converting between the parser dataclass and a slightly different one, or whatever else you choose.
Note that you can create a custom class to mimic argparse's behavior, although I would generally advise against it:
With
argparse.add_argument
it is possible to convert the argument passed on the command line to a different type. It would be useful to have this functionality usingdatargs
, but does not seem possible (e.g.,type
is not a supported keyword for thearg
utility function).For example, I have a command line option for setting the log level. I would like the end user to be able to specify the level using its name, a string, but stored in the
dataclass
as an int so that it can be directly used to setup the logging configuration.It's certainly possible to do the conversion afterwards, but it would be more convenient to have the transformation done during parsing. Currently, I might reuse the same arguments
dataclass
in multiple programs, which requires me to apply the type transformation in each program, versus having it done transparently by the parser.[Edit]
One other thing that doing the conversion client side makes less convenient are
choices
. Using the type conversion withargparse
it is easy to make string choices case insensitive, but doing it client side requires the command line argument to match the case exactly.The text was updated successfully, but these errors were encountered: