Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import argparse |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 2 | import os |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import sys |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 4 | |
| 5 | from cfg_checker.common.exception import ConfigException |
| 6 | |
| 7 | |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 8 | class MyParser(argparse.ArgumentParser): |
| 9 | def error(self, message): |
| 10 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 11 | self.print_help() |
| 12 | |
| 13 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 14 | def get_arg(args, str_arg): |
| 15 | _attr = getattr(args, str_arg) |
| 16 | if _attr: |
| 17 | return _attr |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 18 | else: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 19 | _c = args.command if hasattr(args, 'command') else '' |
| 20 | _t = args.type if hasattr(args, 'type') else '' |
| 21 | raise ConfigException( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 22 | "Argument '{}' not found executing: mcp-check {} {}".format( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 23 | str_arg, |
| 24 | _c, |
| 25 | _t |
| 26 | ) |
| 27 | ) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def get_path_arg(path): |
| 31 | if os.path.exists(path): |
| 32 | return path |
| 33 | else: |
| 34 | raise ConfigException("'{}' not exists".format(path)) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 35 | |
| 36 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 37 | def get_network_map_type_and_filename(args): |
| 38 | if args.html or args.text: |
| 39 | if args.html and args.text: |
| 40 | raise ConfigException("Multuple report types not supported") |
| 41 | if args.html is not None: |
| 42 | return 'html', args.html |
| 43 | if args.text is not None: |
| 44 | return 'text', args.text |
| 45 | else: |
| 46 | return 'console', None |
| 47 | |
| 48 | |
| 49 | def get_package_report_type_and_filename(args): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 50 | if args.html or args.csv: |
| 51 | if args.html and args.csv: |
| 52 | raise ConfigException("Multuple report types not supported") |
| 53 | if args.html is not None: |
| 54 | return 'html', args.html |
| 55 | if args.csv is not None: |
| 56 | return 'csv', args.csv |
| 57 | else: |
| 58 | raise ConfigException("Report type and filename not set") |