|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2025 Nordic Semiconductor ASA |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: BSD-3-Clause |
| 6 | +import argparse |
| 7 | +import sys |
| 8 | + |
| 9 | +from nrfcloud_utils import ( |
| 10 | + claim_and_provision_device, |
| 11 | + claim_devices, |
| 12 | + create_ca_cert, |
| 13 | + create_device_credentials, |
| 14 | + create_proxy_jwt, |
| 15 | + device_credentials_installer, |
| 16 | + gather_attestation_tokens, |
| 17 | + modem_credentials_parser, |
| 18 | + nrf_cloud_device_mgmt, |
| 19 | + nrf_cloud_onboard, |
| 20 | + nrf93_onboard, |
| 21 | +) |
| 22 | + |
| 23 | +_MODULES = { |
| 24 | + "claim_and_provision_device": claim_and_provision_device, |
| 25 | + "claim_devices": claim_devices, |
| 26 | + "create_ca_cert": create_ca_cert, |
| 27 | + "create_device_credentials": create_device_credentials, |
| 28 | + "create_proxy_jwt": create_proxy_jwt, |
| 29 | + "device_credentials_installer": device_credentials_installer, |
| 30 | + "gather_attestation_tokens": gather_attestation_tokens, |
| 31 | + "modem_credentials_parser": modem_credentials_parser, |
| 32 | + "nrf_cloud_device_mgmt": nrf_cloud_device_mgmt, |
| 33 | + "nrf_cloud_onboard": nrf_cloud_onboard, |
| 34 | + "nrf93_onboard": nrf93_onboard, |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +def _build_parser(): |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + prog="nrfcloud-utils", |
| 41 | + description="Scripts and utilities for working with nRF Cloud", |
| 42 | + ) |
| 43 | + subparsers = parser.add_subparsers(dest="command", metavar="command", required=False) |
| 44 | + for name, mod in _MODULES.items(): |
| 45 | + _p = mod.get_parser() |
| 46 | + subparsers.add_parser( |
| 47 | + name, |
| 48 | + parents=[_p], |
| 49 | + description=_p.description, |
| 50 | + formatter_class=_p.formatter_class, |
| 51 | + help=_p.description, |
| 52 | + ) |
| 53 | + return parser |
| 54 | + |
| 55 | + |
| 56 | +def run(): |
| 57 | + parser = _build_parser() |
| 58 | + # parse_args() handles --help at both levels and validates the subcommand name |
| 59 | + # and its args (using each module's get_parser() for the subparser definition). |
| 60 | + # We then re-dispatch to the module's own main() so setup_logging and other |
| 61 | + # module init runs exactly as it does when the command is called standalone. |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + if not args.command: |
| 65 | + parser.print_help() |
| 66 | + sys.exit(0) |
| 67 | + |
| 68 | + cmd = args.command |
| 69 | + sys.argv = [cmd] + sys.argv[2:] |
| 70 | + _MODULES[cmd].main(sys.argv[1:]) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + run() |
0 commit comments