Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_cli / py / pw_cli / __main__.py
1 # Copyright 2020 The Pigweed Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
5 # the License at
6 #
7 #     https://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
13 # the License.
14 """The Pigweed command line interface."""
15
16 import logging
17 import os
18 import sys
19 from typing import NoReturn
20
21 import pw_cli.log
22 from pw_cli import arguments, plugins
23
24 _LOG = logging.getLogger(__name__)
25
26
27 def main() -> NoReturn:
28     """Entry point for the pw command."""
29
30     args = arguments.parse_args()
31
32     pw_cli.log.install()
33     pw_cli.log.set_level(args.loglevel)
34
35     # Start with the most critical part of the Pigweed command line tool.
36     if not args.no_banner:
37         arguments.print_banner()
38
39     _LOG.debug('Executing the pw command from %s', args.directory)
40     os.chdir(args.directory)
41
42     plugins.register(args.directory)
43
44     if args.help or args.command is None:
45         print(arguments.format_help(), file=sys.stderr)
46         sys.exit(0)
47
48     try:
49         sys.exit(plugins.run(args.command, args.plugin_args))
50     except plugins.Error as err:
51         _LOG.critical('Cannot run command %s.', args.command)
52         _LOG.critical('%s', err)
53         sys.exit(2)
54
55
56 if __name__ == '__main__':
57     main()