| 1 |
# Copyright 2014 Open Source Robotics Foundation, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
"""Commonly used, CLI related functions.""" |
| 16 |
|
| 17 |
import re |
| 18 |
|
| 19 |
|
| 20 |
def extract_jobs_flags(arguments): |
| 21 |
"""Extracts make job flags from a list of other make flags, i.e. -j8 -l8 |
| 22 |
|
| 23 |
The input arguments are given as a string separated by whitespace. |
| 24 |
Make job flags are matched and removed from the arguments, and the Make |
| 25 |
job flags and what is left over from the input arguments are returned. |
| 26 |
|
| 27 |
If no job flags are encountered, then an empty string is returned as the |
| 28 |
first element of the returned tuple. |
| 29 |
|
| 30 |
Examples: |
| 31 |
|
| 32 |
.. code-block:: python |
| 33 |
|
| 34 |
>> extract_jobs_flags('-j8 -l8') |
| 35 |
('-j8 -l8', '') |
| 36 |
>> extract_jobs_flags('-j8 ') |
| 37 |
('-j8', ' ') |
| 38 |
>> extract_jobs_flags('target -j8 -l8 --some-option') |
| 39 |
('-j8 -l8', 'target --some-option') |
| 40 |
>> extract_jobs_flags('target --some-option') |
| 41 |
('', 'target --some-option') |
| 42 |
|
| 43 |
:param str arguments: string of space separated arguments which may or |
| 44 |
may not contain make job flags |
| 45 |
:returns: tuple of make jobs flags as a space separated string and |
| 46 |
leftover arguments as a space separated string |
| 47 |
:rtype: tuple |
| 48 |
""" |
| 49 |
regex = ( |
| 50 |
r'(?:^|\s)(-?(?:j|l)(?:\s*[0-9]+|\s|$))' |
| 51 |
r'|' |
| 52 |
r'(?:^|\s)((?:--)?(?:jobs|load-average)(?:(?:=|\s+)[0-9]+|(?:\s|$)))' |
| 53 |
) |
| 54 |
matches = [] |
| 55 |
leftover = '' |
| 56 |
last_match_end = 0 |
| 57 |
for match in re.finditer(regex, arguments) or []: |
| 58 |
matches.append(match.groups()[0] or match.groups()[1]) |
| 59 |
leftover += arguments[last_match_end:match.start()] |
| 60 |
last_match_end = match.end() |
| 61 |
leftover += arguments[last_match_end:] |
| 62 |
return ' '.join([m.strip() for m in matches]), leftover |
| 63 |
|
| 64 |
|
| 65 |
def extract_argument_group(args, delimiting_option): |
| 66 |
"""Extract a group of arguments from a list of arguments using a delimiter. |
| 67 |
|
| 68 |
Here is an example: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
>>> extract_argument_group(['foo', '--args', 'bar', '--baz'], '--args') |
| 73 |
(['foo'], ['bar', '--baz']) |
| 74 |
|
| 75 |
The group can always be ended using the double hyphen ``--``. |
| 76 |
In order to pass a double hyphen as arguments, use three hyphens ``---``. |
| 77 |
Any set of hyphens encountered after the delimiter, and up to ``--``, which |
| 78 |
have three or more hyphens and are isolated, will be captured and reduced |
| 79 |
by one hyphen. |
| 80 |
|
| 81 |
For example: |
| 82 |
|
| 83 |
.. code-block:: python |
| 84 |
|
| 85 |
>> extract_argument_group(['foo', |
| 86 |
'--args', 'bar', '--baz', '---', '--', |
| 87 |
'--foo-option'], '--args') |
| 88 |
(['foo', '--foo-option'], ['bar', '--baz', '--']) |
| 89 |
|
| 90 |
In the result the ``--`` comes from the ``---`` in the input. |
| 91 |
The ``--args`` and the corresponding ``--`` are removed entirely. |
| 92 |
|
| 93 |
The delimiter and ``--`` terminator combination can also happen multiple |
| 94 |
times, in which case the bodies of arguments are combined and returned in |
| 95 |
the order they appeared. |
| 96 |
|
| 97 |
For example: |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
>> extract_argument_group(['foo', |
| 102 |
'--args', 'ping', '--', |
| 103 |
'bar', |
| 104 |
'--args', 'pong', '--', |
| 105 |
'baz', |
| 106 |
'--args', '--'], '--args') |
| 107 |
(['foo', 'bar', 'baz'], ['ping', 'pong']) |
| 108 |
|
| 109 |
Note: ``--`` cannot be used as the ``delimiting_option``. |
| 110 |
|
| 111 |
:param list args: list of strings which are ordered arguments. |
| 112 |
:param str delimiting_option: option which denotes where to split the args. |
| 113 |
:returns: tuple of arguments before and after the delimiter. |
| 114 |
:rtype: tuple |
| 115 |
:raises: ValueError if the delimiting_option is ``--``. |
| 116 |
""" |
| 117 |
if delimiting_option == '--': |
| 118 |
raise ValueError("Cannot use '--' as the delimiter") |
| 119 |
if delimiting_option not in args: |
| 120 |
return args, [] |
| 121 |
trimmed_args = args |
| 122 |
extracted_args = [] |
| 123 |
# Loop through all arguments extracting groups of arguments |
| 124 |
while True: |
| 125 |
try: |
| 126 |
next_delimiter = trimmed_args.index(delimiting_option) |
| 127 |
except ValueError: |
| 128 |
# No delimiter's left in the arguments, stop looking |
| 129 |
break |
| 130 |
# Capture and remove args after the delimiter |
| 131 |
tail = trimmed_args[next_delimiter + 1:] |
| 132 |
trimmed_args = trimmed_args[:next_delimiter] |
| 133 |
# Look for a terminator, '--' |
| 134 |
next_terminator = None |
| 135 |
try: |
| 136 |
next_terminator = tail.index('--') |
| 137 |
except ValueError: |
| 138 |
pass |
| 139 |
if next_terminator is None: |
| 140 |
# No terminator, put all args in extracted_args and stop looking |
| 141 |
extracted_args.extend(tail) |
| 142 |
break |
| 143 |
else: |
| 144 |
# Terminator found, put args up, but not including terminator |
| 145 |
# in extracted_args |
| 146 |
extracted_args.extend(tail[:next_terminator]) |
| 147 |
# And put arguments after the terminator back in trimmed_args |
| 148 |
# then continue looking for additional delimiters |
| 149 |
trimmed_args.extend(tail[next_terminator + 1:]) |
| 150 |
# Iterate through extracted args and shorted tokens with 3+ -'s only |
| 151 |
for i, token in enumerate(extracted_args): |
| 152 |
# '--' should have been removed from extracted_args in the above loop |
| 153 |
assert token != '--', "this shouldn't happen" |
| 154 |
# Skip single hyphens |
| 155 |
if token == '-': |
| 156 |
continue |
| 157 |
# Check for non-hyphen characters |
| 158 |
if [c for c in token if c != '-']: |
| 159 |
# contains something other than -, continue |
| 160 |
continue |
| 161 |
# Must be only hyphens with more than two, Shorted by one - |
| 162 |
extracted_args[i] = token[1:] |
| 163 |
return trimmed_args, extracted_args |