For the current release:
pip install auto-argparseFor the latest version from GitHub:
pip install git+https://github.com/neighthan/auto-argparseReplace something like
fromargparseimportArgumentParserfromtypingimportListdeffunc(x: int, things: List[int], y: str="test"):
""" A very useful function. It does many things. :param x: the first param :param things: variable length! :param y: the last param """defparse_args():
description="A very useful function.\n\nIt does many things."parser=ArgumentParser(description=description)
# have to replicate all of `func`s arguments here, including their types, defaults,# and help strings. And make sure to update this if you ever change `func`!parser.add_argument("-x", "--x", type=int, required=True, help="the first param")
parser.add_argument(
"-t", "--things", nargs="+", type=int, required=True, help="variable length!"
)
parser.add_argument("-y", "--y", type=str, help="the last param")
returnparser.parse_args()
if__name__=="__main__":
func(**vars(parse_args()))with
fromtypingimportListfromauto_argparseimportparse_args_and_rundeffunc(x: int, things: List[int], y: str="test"):
""" A very useful function. It does many things. :param x: the first param :param things: variable length! :param y: the last param """if__name__=="__main__":
parse_args_and_run(func)See the docstring for auto_argparse.make_parser for more details.
The following types should be fully supported, but any annotation T should work if T(cli_string) gives the desired value, where cli_string is the string entered at the command line.
intfloatstrboolList[T],Sequence[T]whereTis any of (int,float,str) or as described in the paragraph aboveOptional[T]whereTcould additionally beListorSequence. Note that there's no way to explicitly enter aNonevalue from the command-line though it can be the default value.
defoptis a more mature library which has the same aims asauto-argparsebut with a slightly different implementation (e.g.auto-argparseadds short names, makes all arguments keyword-only, and puts the part of the doc string for each argument into its help string)