1 # Copyright (c) 2020 Cisco and/or its affiliates.
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5 # Licensed under the Apache License 2.0 or
6 # GNU General Public License v2.0 or later; you may not use this file
7 # except in compliance with one of these Licenses. You
8 # may obtain a copy of the Licenses at:
10 # http://www.apache.org/licenses/LICENSE-2.0
11 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
14 # must be under GPLv2+. If at any point in the future it is no longer linked
15 # with Scapy (or other GPLv2+ licensed software), you are free to choose Apache 2.
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
23 """Traffic scripts argument parser library."""
28 class TrafficScriptArg:
29 """Traffic scripts argument parser.
31 Parse arguments for traffic script. Default has two arguments '--tx_if'
32 and '--rx_if'. You can provide more arguments. All arguments have string
33 representation of the value. You can add also optional arguments. Default
34 value for optional arguments is empty string.
36 :param more_args: List of additional arguments (optional).
37 :param opt_args: List of optional arguments (optional).
43 >>> from TrafficScriptArg import TrafficScriptArg
44 >>> args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
47 def __init__(self, more_args=None, opt_args=None):
48 parser = argparse.ArgumentParser()
49 parser.add_argument(u"--tx_if", help=u"interface that sends traffic")
50 parser.add_argument(u"--rx_if", help=u"interface that receives traffic")
52 if more_args is not None:
55 parser.add_argument(arg_name)
57 if opt_args is not None:
60 parser.add_argument(arg_name, nargs=u"?", default=u"")
63 self._args = vars(parser.parse_args())
65 def get_arg(self, arg_name):
66 """Get argument value.
68 :param arg_name: Argument name.
70 :returns: Argument value.
73 arg_val = self._args.get(arg_name)
75 raise Exception(f"Argument '{arg_name}' not found")