Source code for launch_tools.argument_parsing

  1"""
  2Argument parsers for arguments used by CARLA examples.
  3
  4.. deprecated:: _
  5    In favor of Hydra_.
  6
  7:meta private:
  8"""
  9
 10import argparse
 11from functools import wraps
 12from typing import Any, Callable, Optional, cast
 13from typing_extensions import Concatenate, ParamSpec, Protocol
 14
 15_P = ParamSpec("_P")
 16
 17
 18# maybe later: add some more flexible way to construct a parser.
 19# i.e. combine certain subparsers, e.g. one for port& host another one for settings
 20
 21
 22class _SubparserMixIn(Protocol[_P]):
 23    @staticmethod
 24    def parse_args(*args: _P.args, **kwargs: _P.kwargs) -> argparse.Namespace: ...
 25
 26    @staticmethod
 27    def add(parser: "argparse.ArgumentParser", *args: _P.args, **kwargs: _P.kwargs) -> argparse.ArgumentParser: ...
 28
 29    @staticmethod
 30    def __call__(
 31        parser: Optional[argparse.ArgumentParser] = None, *args: _P.args, **kwargs: _P.kwargs
 32    ) -> argparse.ArgumentParser: ...
 33
 34
[docs] 35def subparser(func: Callable[Concatenate[argparse.ArgumentParser, _P], Any]) -> _SubparserMixIn[_P]: 36 """This decorator allows to join multiple subparsers in a flexible way.""" 37 38 @wraps(func) 39 def _wrapper( 40 parser: Optional[argparse.ArgumentParser] = None, *args: _P.args, **kwargs: _P.kwargs 41 ) -> argparse.ArgumentParser: 42 if parser is None: # create a parser if none is given 43 parser = argparse.ArgumentParser() 44 # else: TODO: are subparsers useful? 45 func(parser, *args, **kwargs) 46 return parser # return the parser object again 47 48 wrapper = cast("_SubparserMixIn[_P]", _wrapper) 49 50 # allows to circumvent calling the function. 51 # i.e. parser_function.parse_args() instead of parser_function().parse_args() 52 wrapper.parse_args = lambda *args, **kwargs: wrapper(None, *args, **kwargs).parse_args() 53 # allows to adjust parsers by adding another parser or by adding a parser function 54 wrapper.add = ( 55 lambda parser, *args, **kwargs: wrapper(parser, *args, **kwargs) 56 if isinstance(parser, argparse.ArgumentParser) 57 else wrapper(parser(), *args, **kwargs) 58 ) 59 return wrapper
60 61
[docs] 62@subparser 63def client_settings(parser: argparse.ArgumentParser): 64 parser.add_argument("--host", metavar="H", default="127.0.0.1", help="IP of the host server (default: 127.0.0.1)") 65 parser.add_argument( 66 "-p", "--port", metavar="P", default=2000, type=int, help="TCP port to listen to (default: 2000)" 67 ) 68 parser.add_argument("-m", "--map", help="Map", default="Town04_Opt", type=str) 69 parser.add_argument("--fps", help="Frames per second.", default=20, type=int)
70 71
[docs] 72@subparser 73def interactive_mode(parser: argparse.ArgumentParser): 74 parser.add_argument("-I", "--interactive", action="store_true", help="Interactive mode", default=False)
75 76
[docs] 77@subparser 78def interactive_control_example(parser: argparse.ArgumentParser): 79 parser.add_argument("--rolename", metavar="NAME", default="hero", help='actor role name (default: "hero")') 80 parser.add_argument("--gamma", default=2.2, type=float, help="Gamma correction of the camera (default: 2.2)") 81 parser.add_argument( 82 "--externalActor", action="store_true", help="attaches to externally created actor by role name" 83 )
84 85
[docs] 86@subparser 87def automatic_control_example(argparser: argparse.ArgumentParser): 88 argparser.description = "CARLA Lunatic Agent Example" 89 argparser.add_argument("-v", "--verbose", action="store_true", dest="debug", help="Print debug information") 90 argparser.add_argument( 91 "--res", metavar="WIDTHxHEIGHT", default="1280x720", help="Window resolution (default: 1280x720)" 92 ) 93 argparser.add_argument("--sync", action="store_true", help="Synchronous mode execution") 94 argparser.add_argument( 95 "--filter", metavar="PATTERN", default="vehicle.*", help='Actor filter (default: "vehicle.*")' 96 ) 97 argparser.add_argument( 98 "--generation", 99 metavar="G", 100 default="2", 101 help='restrict to certain actor generation (values: "1","2","All" - default: "2")', 102 ) 103 argparser.add_argument( 104 "-l", 105 "--loop", 106 action="store_true", 107 dest="loop", 108 help="Sets a new random destination upon reaching the previous one (default: False)", 109 ) 110 111 # Could: separate into subparsers 112 argparser.add_argument( 113 "-a", 114 "--agent", 115 type=str, 116 choices=["Behavior", "Basic", "Constant"], 117 help="select which agent to run", 118 default="Behavior", 119 ) 120 argparser.add_argument( 121 "-b", 122 "--behavior", 123 type=str, 124 # choices=["cautious", "normal", "aggressive"], 125 help="Choose one of the possible agent behaviors (default: normal) ", 126 default="normal", 127 ) 128 129 argparser.add_argument( 130 "-s", "--seed", help="Set seed for repeating executions (default: None)", default=None, type=int 131 ) 132 argparser.add_argument( 133 "-I", "--interactive", help="Enter interactive mode after initialization", action="store_true" 134 ) 135 argparser.add_argument("-ap", "--autopilot", action="store_true", help="enable autopilot")
136 137
[docs] 138def main_parser() -> argparse.ArgumentParser: 139 parser = argparse.ArgumentParser() 140 client_settings.add(parser) 141 automatic_control_example.add(parser) 142 interactive_control_example.add(parser) 143 return parser