|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # |
3 | | -# SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD |
| 3 | +# SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD |
4 | 4 | # |
5 | 5 | # SPDX-License-Identifier: Apache-2.0 |
6 | 6 | # |
7 | | - |
8 | 7 | import argparse |
9 | 8 | import os |
10 | 9 | import subprocess |
11 | 10 | import sys |
12 | 11 |
|
13 | 12 | if __name__ == '__main__': |
14 | | - parser = argparse.ArgumentParser() |
| 13 | + # Here the argparse is used only to "peek" into arguments if |
| 14 | + # legacy version is requested or if old json format is specified. |
| 15 | + # In these two cases the esp_idf_size legacy version is spawned. |
| 16 | + parser = argparse.ArgumentParser(exit_on_error=False, add_help=False) |
15 | 17 | parser.add_argument('--format') |
16 | 18 | parser.add_argument('-l', '--legacy', action='store_true', default=os.environ.get('ESP_IDF_SIZE_LEGACY', '0') == '1') |
| 19 | + |
| 20 | + # The sys.argv is parsed with "exit_on_error", but the argparse.ArgumentError |
| 21 | + # exception should never occur, because unknown args should be put into |
| 22 | + # the rest variable, since the parse_known_args() method is used. |
17 | 23 | args, rest = parser.parse_known_args() |
18 | 24 |
|
19 | 25 | if not args.legacy and args.format != 'json': |
| 26 | + # By default start the refactored version, unless legacy version is explicitly requested with |
| 27 | + # -l/--legacy option or if old json format is specified. |
20 | 28 | try: |
21 | 29 | import esp_idf_size.ng # noqa: F401 |
22 | 30 | except ImportError: |
23 | 31 | print('warning: refactored esp-idf-size not installed, using legacy mode', file=sys.stderr) |
24 | 32 | args.legacy = True |
25 | 33 | else: |
26 | 34 | os.environ['ESP_IDF_SIZE_NG'] = '1' |
27 | | - |
28 | | - if args.legacy and args.format in ['json2', 'raw', 'tree']: |
29 | | - sys.exit(f'Legacy esp-idf-size does not support {args.format} format') |
| 35 | + if not rest or '-h' in rest or '--help' in rest: |
| 36 | + print(('Note: legacy esp_idf_size version can be invoked by specifying the -l/--legacy ' |
| 37 | + 'option or by setting the ESP_IDF_SIZE_LEGACY environment variable.')) |
30 | 38 |
|
31 | 39 | if args.format is not None: |
32 | 40 | rest = ['--format', args.format] + rest |
|
0 commit comments