Repository: google/OpenSK
File: deploy.py (Python)

Code before:
            "support for CTAP2.1 protocol."),
  )
  main_parser.add_argument(
      "--regen-keys",
      action="store_true",
      default=False,
      dest="regenerate_keys",
      help=("Forces the generation of files (certificates and private keys) "
            "under the crypto_data/ directory. "
            "This is useful to allow flashing multiple OpenSK authenticators "
            "in a row without them being considered clones."),
  )
  main_parser.add_argument(
      "--no-persistent-storage",
      action="append_const",
      const="ram_storage",
      dest="features",
      help=("Compiles and installs the OpenSK application without persistent "
            "storage (i.e. unplugging the key will reset the key)."),
  )

  main_parser.add_argument(
      "--elf2tab-output",
      metavar="FILE",
      dest="elf2tab_output",
      default=None,
      help=("When set, the output of elf2tab is appended to this file."),
  )

  apps_group = main_parser.add_mutually_exclusive_group(required=True)
  apps_group.add_argument(
      "--no-app",
      dest="application",
      action="store_const",
      const=None,
      help=("Doesn't compile nor install any application. Useful when you only "
            "want to update Tock OS kernel."))
  apps_group.add_argument(
      "--opensk",
      dest="application",
      action="store_const",
      const="ctap2",
      help="Compiles and installs the OpenSK application.")
  apps_group.add_argument(
      "--crypto_bench",
      dest="application",
      action="store_const",
      const="crypto_bench",
      help=("Compiles and installs the crypto_bench example that tests "
            "the performance of the cryptographic algorithms on the board."))


Review comment (suggestion):
I would specify this argument with a type:
```python
main_parser.add_argument(
  "--elf2tab-output",
  metavar="FILE",
  type=argparse.FileType("a"),
  dest="elf2tab_output",
  default=None,
```

This way, opening/closing the file is left to the argparse module and you don't have to handle errors.
The code above that logs the error will then be:
```python
if self.args.elf2tab_output:
  self.args.elf2tab_output.write(self.checked_command_output(elf2tab_args))
else:
  self.checked_command(elf2tab_args)
```


Code after:
  )
  main_parser.add_argument(
      "--regen-keys",
      action="store_true",
      default=False,
      dest="regenerate_keys",
      help=("Forces the generation of files (certificates and private keys) "
            "under the crypto_data/ directory. "
            "This is useful to allow flashing multiple OpenSK authenticators "
            "in a row without them being considered clones."),
  )
  main_parser.add_argument(
      "--no-persistent-storage",
      action="append_const",
      const="ram_storage",
      dest="features",
      help=("Compiles and installs the OpenSK application without persistent "
            "storage (i.e. unplugging the key will reset the key)."),
  )

  main_parser.add_argument(
      "--elf2tab-output",
      metavar="FILE",
      type=argparse.FileType("a"),
      dest="elf2tab_output",
      default=None,
      help=("When set, the output of elf2tab is appended to this file."),
  )

  apps_group = main_parser.add_mutually_exclusive_group(required=True)
  apps_group.add_argument(
      "--no-app",
      dest="application",
      action="store_const",
      const=None,
      help=("Doesn't compile nor install any application. Useful when you only "
            "want to update Tock OS kernel."))
  apps_group.add_argument(
      "--opensk",
      dest="application",
      action="store_const",
      const="ctap2",
      help="Compiles and installs the OpenSK application.")
  apps_group.add_argument(
      "--crypto_bench",
      dest="application",
      action="store_const",
      const="crypto_bench",
      help=("Compiles and installs the crypto_bench example that tests "
            "the performance of the cryptographic algorithms on the board."))