MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / setup_parser

Method setup_parser

tools/Polygraphy/polygraphy/tools/base/tool.py:38–93  ·  view source on GitHub ↗

Set up a command-line argument parser. Args: subparsers (argparse.SubParsers): A subparser group from argparse, like that returned by ``ArgumentParser.add_subparsers()``. If this is omitted, this function will generate a new ``Arg

(self, subparsers=None)

Source from the content-addressed store, hash-verified

36 self.arg_groups = ArgGroups() # Populated by setup_parser based on get_subscriptions()
37
38 def setup_parser(self, subparsers=None):
39 """
40 Set up a command-line argument parser.
41
42 Args:
43 subparsers (argparse.SubParsers):
44 A subparser group from argparse, like that returned by ``ArgumentParser.add_subparsers()``.
45 If this is omitted, this function will generate a new ``ArgumentParser`` instance.
46 Defaults to None.
47
48 Returns:
49 argparse.ArgumentParser:
50 The newly created parser if ``subparsers`` is not provided, or the newly created subparser otherwise.
51 """
52 assert self.__doc__, "No help output was provided for this tool!"
53
54 subscriptions = self.get_subscriptions()
55 # Always subscribe to the logger arguments first.
56 for arg_group in [LoggerArgs()] + subscriptions:
57 m_type = type(arg_group)
58 self.arg_groups[m_type] = arg_group
59
60 allow_abbrev = all(arg_group.allows_abbreviation() for arg_group in self.arg_groups.values())
61
62 description = dedent(self.__doc__)
63 if subparsers is not None:
64 summary = []
65 for line in description.strip().splitlines():
66 if line.isspace() or not line:
67 break
68 summary.append(line)
69 summary = "\n".join(summary)
70
71 parser = subparsers.add_parser(
72 self.name,
73 help=summary,
74 add_help=True,
75 description=description,
76 allow_abbrev=allow_abbrev,
77 formatter_class=argparse.RawDescriptionHelpFormatter,
78 )
79 parser.set_defaults(subcommand=self)
80 else:
81 parser = argparse.ArgumentParser(add_help=True, description=description, allow_abbrev=allow_abbrev)
82
83 for arg_group in self.arg_groups.values():
84 arg_group.register(self.arg_groups)
85 # This must be done after registration, since some argument groups
86 # may conditionally define arguments based on what other groups are present.
87 arg_group.add_parser_args(parser)
88
89 try:
90 self.add_parser_args(parser)
91 except Exception as err:
92 G_LOGGER.internal_error(f"Could not register tool argument parser for: {self.name}\nNote: Error was: {err}")
93 return parser
94
95 # Implementation for `get_subscriptions`. This should be implemented by child classes instead of `get_subscriptions`

Callers 3

mainMethod · 0.95
mainFunction · 0.80
add_parser_argsMethod · 0.80

Calls 9

get_subscriptionsMethod · 0.95
add_parser_argsMethod · 0.95
LoggerArgsClass · 0.90
typeFunction · 0.85
allows_abbreviationMethod · 0.80
internal_errorMethod · 0.80
valuesMethod · 0.45
appendMethod · 0.45
registerMethod · 0.45

Tested by

no test coverage detected