File: argument.py

package info (click to toggle)
ros2-colcon-package-selection 0.2.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 176 kB
  • sloc: python: 652; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright 2020 Dirk Thomas
# Licensed under the Apache License, Version 2.0

import argparse
import re


def argument_package_name(value):
    """
    Check if an argument is a valid package name.

    Used as a ``type`` callback in ``add_argument()`` calls.
    Package names starting with a dash must be prefixed with a space to avoid
    collisions with command line arguments.

    :param str value: The command line argument
    :returns: The package name
    :raises argparse.ArgumentTypeError: if the value starts with a dash
    """
    if value.startswith('-'):
        raise argparse.ArgumentTypeError('unrecognized argument: ' + value)
    return value.lstrip()


def argument_valid_regex(value):
    """
    Check if an argument is a valid regular expression.

    Used as a ``type`` callback in ``add_argument()`` calls.

    :param str value: The command line argument
    :returns: The regular expression object
    :raises argparse.ArgumentTypeError: if the value is not a valid regex
    """
    try:
        return re.compile(value)
    except re.error as e:  # noqa: F841
        raise argparse.ArgumentTypeError(
            'must be a valid regex: {e}'.format_map(locals()))