1# Copyright 2015-2021 Espressif Systems (Shanghai) CO LTD
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import argparse
16import os
17
18from .constants import DEFAULT_PRINT_FILTER, DEFAULT_TOOLCHAIN_PREFIX, PANIC_DECODE_BACKTRACE, PANIC_DECODE_DISABLE
19from .coredump import COREDUMP_DECODE_DISABLE, COREDUMP_DECODE_INFO
20
21
22def get_parser():  # type: () -> argparse.ArgumentParser
23    parser = argparse.ArgumentParser('idf_monitor - a serial output monitor for esp-idf')
24
25    parser.add_argument(
26        '--port', '-p',
27        help='Serial port device',
28        default=os.environ.get('ESPTOOL_PORT', '/dev/ttyUSB0')
29    )
30
31    parser.add_argument(
32        '--disable-address-decoding', '-d',
33        help="Don't print lines about decoded addresses from the application ELF file",
34        action='store_true',
35        default=os.environ.get('ESP_MONITOR_DECODE') == 0
36    )
37
38    parser.add_argument(
39        '--baud', '-b',
40        help='Serial port baud rate',
41        type=int,
42        default=os.getenv('IDF_MONITOR_BAUD', os.getenv('MONITORBAUD', 115200)))
43
44    parser.add_argument(
45        '--make', '-m',
46        help='Command to run make',
47        type=str, default='make')
48
49    parser.add_argument(
50        '--encrypted',
51        help='Use encrypted targets while running make',
52        action='store_true')
53
54    parser.add_argument(
55        '--toolchain-prefix',
56        help='Triplet prefix to add before cross-toolchain names',
57        default=DEFAULT_TOOLCHAIN_PREFIX)
58
59    parser.add_argument(
60        '--eol',
61        choices=['CR', 'LF', 'CRLF'],
62        type=lambda c: c.upper(),
63        help='End of line to use when sending to the serial port',
64        default='CR')
65
66    parser.add_argument(
67        'elf_file', help='ELF file of application',
68        type=argparse.FileType('rb'))
69
70    parser.add_argument(
71        '--print_filter',
72        help='Filtering string',
73        default=DEFAULT_PRINT_FILTER)
74
75    parser.add_argument(
76        '--decode-coredumps',
77        choices=[COREDUMP_DECODE_INFO, COREDUMP_DECODE_DISABLE],
78        default=COREDUMP_DECODE_INFO,
79        help='Handling of core dumps found in serial output'
80    )
81
82    parser.add_argument(
83        '--decode-panic',
84        choices=[PANIC_DECODE_BACKTRACE, PANIC_DECODE_DISABLE],
85        default=PANIC_DECODE_DISABLE,
86        help='Handling of panic handler info found in serial output'
87    )
88
89    parser.add_argument(
90        '--target',
91        help='Target name (used when stack dump decoding is enabled)',
92        default=os.environ.get('IDF_TARGET', 'esp32')
93    )
94
95    parser.add_argument(
96        '--revision',
97        help='Revision of the target',
98        type=int,
99        default=0
100    )
101
102    parser.add_argument(
103        '--ws',
104        default=os.environ.get('ESP_IDF_MONITOR_WS', None),
105        help='WebSocket URL for communicating with IDE tools for debugging purposes'
106    )
107
108    parser.add_argument(
109        '--timestamps',
110        help='Add timestamp for each line',
111        default=False,
112        action='store_true')
113
114    parser.add_argument(
115        '--timestamp-format',
116        default=os.environ.get('ESP_IDF_MONITOR_TIMESTAMP_FORMAT', '%Y-%m-%d %H:%M:%S'),
117        help='Set a strftime()-compatible timestamp format'
118    )
119
120    return parser
121