New version of RF tests.
[csit.git] / resources / libraries / python / TrafficScriptArg.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Traffic scripts argument parser library."""
15
16 import argparse
17
18
19 class TrafficScriptArg(object):
20     """Traffic scripts argument parser.
21
22     Parse arguments for traffic script. Default has two arguments '--tx_if'
23     and '--rx_if'. You can provide more arguments. All arguments have string
24     representation of the value.
25
26     :param more_args: List of aditional arguments (optional).
27     :type more_args: list
28
29     :Example:
30
31     >>> from TrafficScriptArg import TrafficScriptArg
32     >>> args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
33     """
34
35     def __init__(self, more_args=None):
36         parser = argparse.ArgumentParser()
37         parser.add_argument("--tx_if", help="interface that sends traffic")
38         parser.add_argument("--rx_if", help="interface that receives traffic")
39
40         if more_args is not None:
41             for arg in more_args:
42                 arg_name = '--{0}'.format(arg)
43                 parser.add_argument(arg_name)
44
45         self._parser = parser
46         self._args = vars(parser.parse_args())
47
48     def get_arg(self, arg_name):
49         """Get argument value.
50
51         :param arg_name: Argument name.
52         :type arg_name: str
53         :return: Argument value.
54         :rtype: str
55         """
56         arg_val = self._args.get(arg_name)
57         if arg_val is None:
58             raise Exception("Argument '{0}' not found".format(arg_name))
59
60         return arg_val