Joey Armstrong | 44fa7d8 | 2022-11-01 17:46:04 -0400 | [diff] [blame] | 1 | # -*- python -*- |
| 2 | '''A module for parsing script command line arguments. |
| 3 | |
| 4 | ..seealso: https://docs.python.org/3/library/argparse.html## |
| 5 | ''' |
| 6 | |
| 7 | # ----------------------------------------------------------------------- |
| 8 | # Copyright 2022 Open Networking Foundation (ONF) and the ONF Contributors |
| 9 | # |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | # you may not use this file except in compliance with the License. |
| 12 | # You may obtain a copy of the License at |
| 13 | # |
| 14 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | # |
| 16 | # Unless required by applicable law or agreed to in writing, software |
| 17 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | # See the License for the specific language governing permissions and |
| 20 | # limitations under the License. |
| 21 | # ----------------------------------------------------------------------- |
| 22 | |
| 23 | ##-------------------## |
| 24 | ##---] GLOBALS [---## |
| 25 | ##-------------------## |
| 26 | ARGV = None |
| 27 | namespace = None |
| 28 | |
| 29 | ##-------------------## |
| 30 | ##---] IMPORTS [---## |
| 31 | ##-------------------## |
| 32 | import argparse |
| 33 | |
| 34 | from flog.main import utils as main_utils |
| 35 | from flog.main import help as main_help |
| 36 | |
| 37 | ## ----------------------------------------------------------------------- |
| 38 | ## ----------------------------------------------------------------------- |
| 39 | def get_argv(): |
| 40 | """Retrieve parsed command line switches. |
| 41 | |
| 42 | ..pre: getopts() was called earlier. |
| 43 | |
| 44 | :return: Parsed command line argument storage |
| 45 | :rtype : dict |
| 46 | """ |
| 47 | |
| 48 | global ARGV |
| 49 | global namespace |
| 50 | |
| 51 | if ARGV is None: |
| 52 | # Normalize argspace/namespace into a getopt/dictionary |
| 53 | # Program wide syntax edits needed: args['foo'] => args.foo |
| 54 | arg_dict = {} |
| 55 | for arg in vars(namespace): |
| 56 | arg_dict[arg] = getattr(namespace, arg) |
| 57 | ARGV = arg_dict |
| 58 | |
| 59 | return ARGV |
| 60 | |
| 61 | ## ----------------------------------------------------------------------- |
| 62 | ## ----------------------------------------------------------------------- |
| 63 | def getopts(argv, debug=None) -> None: |
| 64 | """Parse command line args, check options and pack into a hashmap |
| 65 | |
| 66 | :param argv: values passed on the command line |
| 67 | :param debug: optional flag to enable debug mode |
| 68 | |
| 69 | :return: Digested command line arguments |
| 70 | :rtype : dict |
| 71 | |
| 72 | :raises ValueError |
| 73 | |
| 74 | .. versionadded:: 1.0 |
| 75 | """ |
| 76 | |
| 77 | global namespace |
| 78 | |
| 79 | iam = main_utils.iam() |
| 80 | |
| 81 | if debug is None: |
| 82 | debug = False |
| 83 | |
| 84 | parser = argparse.ArgumentParser\ |
| 85 | ( |
| 86 | description = '''Report test dependencies based on selection criteria.''' |
| 87 | # epilog = 'extra-help-text' |
| 88 | ) |
| 89 | |
| 90 | ## ----------------------------------------------------------------------- |
| 91 | ## [TEST: categories] |
| 92 | ## ----------------------------------------------------------------------- |
| 93 | parser.add_argument('--attr', |
| 94 | action = 'append', |
| 95 | default = [], |
| 96 | choices=\ |
| 97 | [ |
| 98 | 'olt', # optical line termination |
| 99 | # |
| 100 | 'onu', # optical network unit |
| 101 | # |
| 102 | 'epon', |
| 103 | 'pon', # passive optical network |
| 104 | 'gpon', # gigabit-pon |
| 105 | 'xpon', |
| 106 | ], |
| 107 | help = 'Enable testing by attribute', |
| 108 | ) |
| 109 | |
| 110 | ## ----------------------------------------------------------------------- |
| 111 | ## [TEST:types] |
| 112 | ## ----------------------------------------------------------------------- |
| 113 | parser.add_argument('--type', |
| 114 | action = 'append', |
| 115 | default = [], |
| 116 | choices=\ |
| 117 | [ |
| 118 | 'burnin', # profile, stress testing |
| 119 | 'integration', # trigger inter-dependencies |
| 120 | 'oink', # kitchen sink testing |
| 121 | 'regression', # have we broken the renaissance ? |
| 122 | 'scale', # |
| 123 | 'system', # |
| 124 | 'smoke', # quick: 60s > [n] |
| 125 | 'standalone', # dependency-less tests |
| 126 | 'suite', # |
| 127 | 'unit', # module/api/narrow focus. |
| 128 | ], |
| 129 | help = 'Enable testing by category', |
| 130 | ) |
| 131 | |
| 132 | ## ----------------------------------------------------------------------- |
| 133 | ## [FILTER] |
| 134 | ## ----------------------------------------------------------------------- |
| 135 | parser.add_argument('--excl', |
| 136 | action = 'append', |
| 137 | default = [], |
| 138 | help = 'FILTER: Probe resources to exclude', |
| 139 | ) |
| 140 | parser.add_argument('--incl', |
| 141 | action = 'append', |
| 142 | default = [], |
| 143 | help = 'FILTER: Probe resources to include', |
| 144 | ) |
| 145 | |
| 146 | ## ----------------------------------------------------------------------- |
| 147 | ## [MODES] |
| 148 | ## ----------------------------------------------------------------------- |
| 149 | parser.add_argument('--debug', |
| 150 | action = 'store_true', |
| 151 | default = False, |
| 152 | help = 'Enable debug mode', |
| 153 | ) |
| 154 | |
| 155 | parser.add_argument('--trace', |
| 156 | action = 'append', |
| 157 | default = [], |
| 158 | help = 'Enable python debugging to trace a named resource.', |
| 159 | ) |
| 160 | |
| 161 | parser.add_argument('--usage', |
| 162 | action = 'store_true', |
| 163 | default = False, |
| 164 | help = 'Show usage examples', |
| 165 | ) |
| 166 | |
| 167 | parser.add_argument('--version', action='version', version='%(prog)s 1.0') |
| 168 | |
| 169 | |
| 170 | namespace = parser.parse_args() |
| 171 | |
| 172 | # -------------------------------------------------------------- |
| 173 | # [TODO] update --usage to accept a value, display context help. |
| 174 | # -------------------------------------------------------------- |
| 175 | if namespace.usage: |
| 176 | main_help.usage() |
| 177 | |
| 178 | return |
| 179 | |
| 180 | # [EOF] |
| 181 | |