1#!/usr/bin/env python3 2# 3# CDDL HEADER START 4# 5# The contents of this file are subject to the terms of the 6# Common Development and Distribution License (the "License"). 7# You may not use this file except in compliance with the License. 8# 9# See LICENSE.txt included in this distribution for the specific 10# language governing permissions and limitations under the License. 11# 12# When distributing Covered Code, include this CDDL HEADER in each 13# file and include the License file at LICENSE.txt. 14# If applicable, add the following below this CDDL HEADER, with the 15# fields enclosed by brackets "[]" replaced with your own identifying 16# information: Portions Copyright [yyyy] [name of copyright owner] 17# 18# CDDL HEADER END 19# 20 21# 22# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. 23# 24 25import argparse 26 27from .log import add_log_level_argument 28from ..version import __version__ as tools_version 29 30 31def str2bool(v): 32 if isinstance(v, bool): 33 return v 34 35 if isinstance(v, str): 36 v_lower = v.lower() 37 if v_lower in ('yes', 'true', 'y', '1'): 38 return True 39 elif v_lower in ('no', 'false', 'n', '0'): 40 return False 41 42 raise argparse.ArgumentTypeError('Boolean value or its string ' 43 'representation expected.') 44 45 46def add_http_headers(parser): 47 parser.add_argument('-H', '--header', nargs='+', 48 help='add HTTP header(s) to API requests. ' 49 'In the form of \'name: value\' or @file_path ' 50 'to read headers from input file') 51 52 53def get_headers(headers_list): 54 """ 55 Complement to add_http_headers() for parsing the data 56 acquired from argument parsing. 57 58 :param headers_list: list of 'name: value' strings 59 :return: dictionary indexed with names 60 """ 61 headers = {} 62 if headers_list: 63 for arg in headers_list: 64 if arg.startswith("@"): 65 file_path = arg[1:] 66 with open(file_path) as file: 67 for line in file: 68 line = line.rstrip() 69 if ': ' in line: 70 name, value = line.split(': ') 71 headers[name] = value 72 else: 73 name, value = arg.split(': ') 74 headers[name] = value 75 76 return headers 77 78 79def get_base_parser(tool_version=None): 80 """ 81 Get the base parser which supports --version option reporting 82 the overall version of the tools and the specific version of the 83 invoked tool. 84 :param tool_version: the specific version tool if applicable 85 :return: the parser 86 """ 87 parser = argparse.ArgumentParser(add_help=False) 88 add_log_level_argument(parser) 89 version = tools_version 90 if tool_version: 91 version += ' (v{})'.format(tool_version) 92 parser.add_argument('-v', '--version', action='version', version=version, 93 help='Version of the tool') 94 return parser 95 96 97def get_java_parser(): 98 parser = argparse.ArgumentParser(add_help=False, 99 parents=[get_base_parser()]) 100 parser.add_argument('-j', '--java', 101 help='path to java binary') 102 parser.add_argument('-J', '--java_opts', 103 help='java options. Use one for every java option, ' 104 'e.g. -J=-server -J=-Xmx16g', 105 action='append') 106 parser.add_argument('-e', '--environment', action='append', 107 help='Environment variables in the form of name=value') 108 parser.add_argument('--doprint', type=str2bool, nargs=1, default=None, 109 metavar='boolean', 110 help='Enable/disable printing of messages ' 111 'from the application as they are produced.') 112 113 group = parser.add_mutually_exclusive_group(required=True) 114 group.add_argument('-a', '--jar', 115 help='Path to jar archive to run') 116 group.add_argument('-c', '--classpath', 117 help='Class path') 118 119 parser.add_argument('options', nargs='+', help='options') 120 121 return parser 122