|
1 | 1 | # SPDX-FileCopyrightText: 2024 Tim Cocks
|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 |
| -from importlib import resources |
5 |
| -import os |
| 4 | +import argparse |
6 | 5 | import sys
|
7 | 6 | import shutil
|
| 7 | +from importlib import resources |
8 | 8 |
|
9 | 9 |
|
10 | 10 | def set_board():
|
11 |
| - if len(sys.argv) != 2: |
12 |
| - print(f"Incorrect args. Please call with: 'circuitpython_setboard chosen_board'") |
13 |
| - return |
| 11 | + parser = argparse.ArgumentParser( |
| 12 | + prog=__name__, |
| 13 | + usage="Install CircuitPython board-specific stubs", |
| 14 | + ) |
| 15 | + parser.add_argument("chosen_board", help="selected board", nargs="?") |
| 16 | + parser.add_argument("-l", "--list", help="show available boards", action="store_true") |
| 17 | + |
| 18 | + args = parser.parse_args() |
| 19 | + |
| 20 | + if args.list: |
| 21 | + sys.stdout.write("Available boards are: \n") |
| 22 | + |
| 23 | + for board in resources.files("board_definitions").iterdir(): |
| 24 | + sys.stdout.write(f"{board.name}\n") |
| 25 | + |
| 26 | + sys.exit(0) |
| 27 | + |
| 28 | + if args.chosen_board is None: |
| 29 | + sys.stderr.write("Must select a board") |
| 30 | + sys.exit(1) |
14 | 31 |
|
15 |
| - chosen_board = sys.argv[1] |
16 |
| - print(f"setting board: {chosen_board}") |
| 32 | + print(f"setting board: {args.chosen_board}") |
17 | 33 |
|
18 | 34 | board_stubs_file = resources.files("board-stubs").joinpath("__init__.pyi")
|
19 |
| - board_definitions_path = resources.files("board_definitions").joinpath(chosen_board) |
| 35 | + board_definitions_path = resources.files("board_definitions").joinpath(args.chosen_board) |
20 | 36 | board_definitions_file = board_definitions_path.joinpath("__init__.pyi")
|
21 | 37 |
|
22 | 38 | if not board_definitions_file.is_file():
|
23 |
| - print(f"Board: '{chosen_board}' was not found") |
24 |
| - return |
| 39 | + print(f"Board: '{args.chosen_board}' was not found") |
| 40 | + sys.exit(1) |
25 | 41 |
|
26 | 42 | shutil.copyfile(board_definitions_file, board_stubs_file)
|
0 commit comments