1 # Copyright (c) 2018 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Traffic scripts argument parser library."""
19 class TrafficScriptArg(object):
20 """Traffic scripts argument parser.
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. You can add also optional arguments. Default
25 value for optional arguments is empty string.
27 :param more_args: List of additional arguments (optional).
28 :param opt_args: List of optional arguments (optional).
34 >>> from TrafficScriptArg import TrafficScriptArg
35 >>> args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
38 def __init__(self, more_args=None, opt_args=None):
39 parser = argparse.ArgumentParser()
40 parser.add_argument("--tx_if", help="interface that sends traffic")
41 parser.add_argument("--rx_if", help="interface that receives traffic")
43 if more_args is not None:
45 arg_name = '--{0}'.format(arg)
46 parser.add_argument(arg_name)
48 if opt_args is not None:
50 arg_name = '--{0}'.format(arg)
51 parser.add_argument(arg_name, nargs='?', default='')
54 self._args = vars(parser.parse_args())
56 def get_arg(self, arg_name):
57 """Get argument value.
59 :param arg_name: Argument name.
61 :returns: Argument value.
64 arg_val = self._args.get(arg_name)
66 raise Exception("Argument '{0}' not found".format(arg_name))