<commit_before># Copyright (c) 2016, Nordic Semiconductor
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of Nordic Semiconductor ASA nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import argparse

from lib import nrf5x

def _add_erase_command(subparsers):
    """
    Adds the erase sub-command and it's command-line arguments to our top-level parser.

    """
    program_parser = subparsers.add_parser('erase', help = 'Erases the device.')

    erase_group = program_parser.add_mutually_exclusive_group()
    erase_group.add_argument('-e', '--eraseall', action = 'store_true', help = 'Erase all user flash, including UICR')
    erase_group.add_argument('-p', '--erasepage', type = int, help = 'Erase page in flash. ERASEPAGE is the start address of the page to be erased.')
    erase_group.add_argument('-u', '--eraseuicr', action = 'store_true', help = 'Erase the UICR page in flash.')

    program_parser.set_defaults(func = nrf5x.erase)

def _add_program_command(subparsers):
    """
    Adds the program sub-command and it's command-line arguments to our top-level parser.

    """
    program_parser = subparsers.add_parser('program', help = 'Programs the device.')

    program_parser.add_argument('-f', '--file', type = file, help = 'The hex file to be programmed to the device.', required = True)
    
    erase_group = program_parser.add_mutually_exclusive_group()
    erase_group.add_argument('-e', '--eraseall', action = 'store_true', help = 'Erase all user flash, including UICR, before programming the device.')
    erase_group.add_argument('-s', '--sectorserase', action = 'store_true', help = 'Erase all sectors that FILE writes before programming.')
    erase_group.add_argument('-u', '--sectorsanduicrerase', action = 'store_true', help = 'Erase all sectors that FILE writes and the UICR before programming.')

    program_parser.add_argument('-v', '--verify', action = 'store_true', help = 'Read back memory after programming and verify that FILE was correctly written.')

    program_parser.set_defaults(func = nrf5x.program)

def _add_recover_command(subparsers):
    """
    Adds the recover sub-command to our top-level parser.

    """
    recover_parser = subparsers.add_parser('recover', help = 'Erases all user FLASH and RAM and disables any readback protection mechanisms that are enabled.')
    recover_parser.set_defaults(func = nrf5x.recover)

def _add_reset_command(subparsers):
    """
    Adds the reset sub-command and it's command-line arguments to our top-level parser.

    """
    reset_parser = subparsers.add_parser('reset', help = 'Resets the device.')

    reset_group = reset_parser.add_mutually_exclusive_group()
    reset_group.add_argument('-d', '--debugreset', action = 'store_true', help = 'Executes a debug reset.')
    reset_group.add_argument('-p', '--pinreset', action = 'store_true', help = 'Executes a pin reset.')
    reset_group.add_argument('-s', '--systemreset', action = 'store_true', help = 'Executes a system reset.')

    reset_parser.set_defaults(func = nrf5x.reset)

parser = argparse.ArgumentParser(description='nrfjprog is a command line tool used for programming nRF5x devices.')
subparsers = parser.add_subparsers()
_add_erase_command(subparsers)
_add_program_command(subparsers)
_add_recover_command(subparsers)
_add_reset_command(subparsers)

if __name__ == '__main__':<commit_msg>Reduce code duplication in parser.<commit_after># Copyright (c) 2016, Nordic Semiconductor
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of Nordic Semiconductor ASA nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import argparse

from lib import nrf5x

def _add_erase_command(subparsers):
    """
    Adds the erase sub-command and it's command-line arguments to our top-level parser.

    """
    erase_parser = subparsers.add_parser('erase', help = 'Erases the device.')
    _add_erase_group(erase_parser)
    erase_parser.set_defaults(func = nrf5x.erase)

def _add_program_command(subparsers):
    """
    Adds the program sub-command and it's command-line arguments to our top-level parser.

    """
    program_parser = subparsers.add_parser('program', help = 'Programs the device.')
    _add_file_option(program_parser)
    _add_erase_group(program_parser)
    _add_verify_option(program_parser)
    _add_reset_group(program_parser)
    program_parser.set_defaults(func = nrf5x.program)

def _add_recover_command(subparsers):
    """
    Adds the recover sub-command to our top-level parser.

    """
    recover_parser = subparsers.add_parser('recover', help = 'Erases all user FLASH and RAM and disables any readback protection mechanisms that are enabled.')
    recover_parser.set_defaults(func = nrf5x.recover)

def _add_reset_command(subparsers):
    """
    Adds the reset sub-command and it's command-line arguments to our top-level parser.

    """
    reset_parser = subparsers.add_parser('reset', help = 'Resets the device.')
    _add_reset_group(reset_parser)
    reset_parser.set_defaults(func = nrf5x.reset)

def _add_erase_group(sub_parser):
    """
    Adds the mutually exclusive group of erase options to our command.

    """
    erase_group = sub_parser.add_mutually_exclusive_group()
    erase_group.add_argument('-e', '--eraseall', action = 'store_true', help = 'Erase all user flash, including UICR, before programming the device.')
    erase_group.add_argument('-s', '--sectorserase', action = 'store_true', help = 'Erase all sectors that FILE writes before programming.')
    erase_group.add_argument('-u', '--sectorsanduicrerase', action = 'store_true', help = 'Erase all sectors that FILE writes and the UICR before programming.')

def _add_reset_group(sub_parser):
    """
    Adds the mutually exclusive group of reset options to our command.

    """
    reset_group = sub_parser.add_mutually_exclusive_group()
    reset_group.add_argument('-d', '--debugreset', action = 'store_true', help = 'Executes a debug reset.')
    reset_group.add_argument('-p', '--pinreset', action = 'store_true', help = 'Executes a pin reset.')
    reset_group.add_argument('-r', '--systemreset', action = 'store_true', help = 'Executes a system reset.')

def _add_file_option(sub_parser):
    """
    Adds the required file option to our command.

    """
    sub_parser.add_argument('-f', '--file', type = file, help = 'The hex file to be programmed to the device.', required = True)

def _add_verify_option(sub_parser):
    """
    Adds the verify option to our command.
    """
    sub_parser.add_argument('-v', '--verify', action = 'store_true', help = 'Read back memory after programming and verify that FILE was correctly written.')


parser = argparse.ArgumentParser(description='nrfjprog is a command line tool used for programming nRF5x devices.')
subparsers = parser.add_subparsers()
_add_erase_command(subparsers)
_add_program_command(subparsers)
_add_recover_command(subparsers)
_add_reset_command(subparsers)

if __name__ == '__main__':
{'fraction_non_alphanumeric': 0.0914344685242518, 'fraction_numerical': 0.0020639834881320948, 'mean_word_length': 4.569540229885058, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278211', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2773, 'n_words': 1004}