For the current release:
pip install auto-argparseFor the latest version from GitHub:
pip install git+https://github.com/neighthan/auto-argparseReplace something like
from argparse import ArgumentParser
from typing import List
def func(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
"""
def parse_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")
return parser.parse_args()
if __name__ == "__main__":
func(**vars(parse_args()))with
from typing import List
from auto_argparse import parse_args_and_run
def func(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)