def test_vanilla_argparse_issue64(): """This test shows that the ArgumentDefaultsHelpFormatter of argparse doesn't add the "(default: xyz)" if the 'help' argument isn't already passed! This begs the question: Should simple-parsing add a 'help' argument always, so that the formatter can then add the default string after? """ import argparse parser = ArgumentParser( "issue64", formatter_class=argparse.ArgumentDefaultsHelpFormatter) group = parser.add_argument_group("Options ['options']", description="These are the options") group.add_argument("--foo", type=str, metavar="str", default="aaa", help="Description") group.add_argument("--bar", type=str, metavar="str", default="bbb") from io import StringIO s = StringIO() parser.print_help(file=s) s.seek(0) assert s.read() == textwrap.dedent("""\ usage: issue64 [-h] [--foo str] [--bar str] optional arguments: -h, --help show this help message and exit Options ['options']: These are the options --foo str Description (default: aaa) --bar str """)
def test_solved_issue64(): """test that shows that Issue 64 is solved now, by adding a single space as the 'help' argument, the help formatter can then add the "(default: bbb)" after the argument. """ parser = ArgumentParser("issue64") parser.add_arguments(Options, dest="options") s = StringIO() parser.print_help(file=s) s.seek(0) assert s.read() == textwrap.dedent("""\ usage: issue64 [-h] [--foo str] [--bar str] optional arguments: -h, --help show this help message and exit Options ['options']: These are the options --foo str Description (default: aaa) --bar str (default: bbb) """)
def test_reproduce_issue64(): parser = ArgumentParser("issue64") parser.add_arguments(Options, dest="options") # args = parser.parse_args(["--help"]) s = StringIO() parser.print_help(file=s) s.seek(0) assert s.read() == textwrap.dedent("""\ usage: issue64 [-h] [--foo str] [--bar str] optional arguments: -h, --help show this help message and exit Options ['options']: These are the options --foo str Description (default: aaa) --bar str """)
def test_issue_48(): parser = ArgumentParser("Prepare input data for training") parser.add_arguments(InputArgs, dest="args") s = StringIO() parser.print_help(file=s) s.seek(0) assert s.read().replace(" ", "") == textwrap.dedent("""\ usage: Prepare input data for training [-h] -s str -e str optional arguments: -h, --help show this help message and exit InputArgs ['args']: InputArgs(start_date:str, end_date:str) -s str, --start_date str Start date from which to collect data about base users. Input in iso format (YYYY-MM-DD). The date is included in the data (default: None) -e str, --end_date str End date for collecting base users. Input in iso format (YYYY-MM-DD). The date is included in the data. Should not be before `start_date` (default: None) """).replace(" ", "")
from simple_parsing import ArgumentParser from simple_parsing.helpers import choice @dataclass class HParams: """ Hyper-Parameters of my model.""" # Learning rate. learning_rate: float = 3e-4 # L2 regularization coefficient. weight_decay: float = 1e-6 # Choice of optimizer optimizer: str = choice("adam", "sgd", "rmsprop", default="sgd") parser = ArgumentParser() parser.add_arguments(HParams, "hparams") parser.print_help() expected += """\ usage: dataclasses_example.py [-h] [--learning_rate float] [--weight_decay float] [--optimizer {adam,sgd,rmsprop}] optional arguments: -h, --help show this help message and exit HParams ['hparams']: Hyper-Parameters of my model. --learning_rate float, --hparams.learning_rate float Learning rate. (default: 0.0003) --weight_decay float, --hparams.weight_decay float L2 regularization coefficient. (default: 1e-06)