1*89229afdSVladimir Kotal#!/usr/bin/env python3 2*89229afdSVladimir Kotal# 3*89229afdSVladimir Kotal# CDDL HEADER START 4*89229afdSVladimir Kotal# 5*89229afdSVladimir Kotal# The contents of this file are subject to the terms of the 6*89229afdSVladimir Kotal# Common Development and Distribution License (the "License"). 7*89229afdSVladimir Kotal# You may not use this file except in compliance with the License. 8*89229afdSVladimir Kotal# 9*89229afdSVladimir Kotal# See LICENSE.txt included in this distribution for the specific 10*89229afdSVladimir Kotal# language governing permissions and limitations under the License. 11*89229afdSVladimir Kotal# 12*89229afdSVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each 13*89229afdSVladimir Kotal# file and include the License file at LICENSE.txt. 14*89229afdSVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the 15*89229afdSVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying 16*89229afdSVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner] 17*89229afdSVladimir Kotal# 18*89229afdSVladimir Kotal# CDDL HEADER END 19*89229afdSVladimir Kotal# 20*89229afdSVladimir Kotal 21*89229afdSVladimir Kotal# 22*89229afdSVladimir Kotal# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. 23*89229afdSVladimir Kotal# 24*89229afdSVladimir Kotal 252d57dc69SVladimir Kotalimport argparse 262d57dc69SVladimir Kotal 272d57dc69SVladimir Kotalfrom .log import add_log_level_argument 282d57dc69SVladimir Kotalfrom ..version import __version__ as tools_version 292d57dc69SVladimir Kotal 302d57dc69SVladimir Kotal 312d57dc69SVladimir Kotaldef str2bool(v): 322d57dc69SVladimir Kotal if isinstance(v, bool): 332d57dc69SVladimir Kotal return v 342d57dc69SVladimir Kotal 352d57dc69SVladimir Kotal if isinstance(v, str): 362d57dc69SVladimir Kotal v_lower = v.lower() 372d57dc69SVladimir Kotal if v_lower in ('yes', 'true', 'y', '1'): 382d57dc69SVladimir Kotal return True 392d57dc69SVladimir Kotal elif v_lower in ('no', 'false', 'n', '0'): 402d57dc69SVladimir Kotal return False 412d57dc69SVladimir Kotal 422d57dc69SVladimir Kotal raise argparse.ArgumentTypeError('Boolean value or its string ' 432d57dc69SVladimir Kotal 'representation expected.') 442d57dc69SVladimir Kotal 452d57dc69SVladimir Kotal 46*89229afdSVladimir Kotaldef add_http_headers(parser): 47*89229afdSVladimir Kotal parser.add_argument('-H', '--header', nargs='+', 48*89229afdSVladimir Kotal help='add HTTP header(s) to API requests. ' 49*89229afdSVladimir Kotal 'In the form of \'name: value\' or @file_path ' 50*89229afdSVladimir Kotal 'to read headers from input file') 51*89229afdSVladimir Kotal 52*89229afdSVladimir Kotal 53*89229afdSVladimir Kotaldef get_headers(headers_list): 54*89229afdSVladimir Kotal """ 55*89229afdSVladimir Kotal Complement to add_http_headers() for parsing the data 56*89229afdSVladimir Kotal acquired from argument parsing. 57*89229afdSVladimir Kotal 58*89229afdSVladimir Kotal :param headers_list: list of 'name: value' strings 59*89229afdSVladimir Kotal :return: dictionary indexed with names 60*89229afdSVladimir Kotal """ 61*89229afdSVladimir Kotal headers = {} 62*89229afdSVladimir Kotal if headers_list: 63*89229afdSVladimir Kotal for arg in headers_list: 64*89229afdSVladimir Kotal if arg.startswith("@"): 65*89229afdSVladimir Kotal file_path = arg[1:] 66*89229afdSVladimir Kotal with open(file_path) as file: 67*89229afdSVladimir Kotal for line in file: 68*89229afdSVladimir Kotal line = line.rstrip() 69*89229afdSVladimir Kotal if ': ' in line: 70*89229afdSVladimir Kotal name, value = line.split(': ') 71*89229afdSVladimir Kotal headers[name] = value 72*89229afdSVladimir Kotal else: 73*89229afdSVladimir Kotal name, value = arg.split(': ') 74*89229afdSVladimir Kotal headers[name] = value 75*89229afdSVladimir Kotal 76*89229afdSVladimir Kotal return headers 77*89229afdSVladimir Kotal 78*89229afdSVladimir Kotal 792d57dc69SVladimir Kotaldef get_base_parser(tool_version=None): 802d57dc69SVladimir Kotal """ 812d57dc69SVladimir Kotal Get the base parser which supports --version option reporting 822d57dc69SVladimir Kotal the overall version of the tools and the specific version of the 832d57dc69SVladimir Kotal invoked tool. 842d57dc69SVladimir Kotal :param tool_version: the specific version tool if applicable 852d57dc69SVladimir Kotal :return: the parser 862d57dc69SVladimir Kotal """ 872d57dc69SVladimir Kotal parser = argparse.ArgumentParser(add_help=False) 882d57dc69SVladimir Kotal add_log_level_argument(parser) 892d57dc69SVladimir Kotal version = tools_version 902d57dc69SVladimir Kotal if tool_version: 912d57dc69SVladimir Kotal version += ' (v{})'.format(tool_version) 922d57dc69SVladimir Kotal parser.add_argument('-v', '--version', action='version', version=version, 932d57dc69SVladimir Kotal help='Version of the tool') 942d57dc69SVladimir Kotal return parser 952d57dc69SVladimir Kotal 962d57dc69SVladimir Kotal 972d57dc69SVladimir Kotaldef get_java_parser(): 982d57dc69SVladimir Kotal parser = argparse.ArgumentParser(add_help=False, 992d57dc69SVladimir Kotal parents=[get_base_parser()]) 1002d57dc69SVladimir Kotal parser.add_argument('-j', '--java', 1012d57dc69SVladimir Kotal help='path to java binary') 1022d57dc69SVladimir Kotal parser.add_argument('-J', '--java_opts', 1032d57dc69SVladimir Kotal help='java options. Use one for every java option, ' 1042d57dc69SVladimir Kotal 'e.g. -J=-server -J=-Xmx16g', 1052d57dc69SVladimir Kotal action='append') 1062d57dc69SVladimir Kotal parser.add_argument('-e', '--environment', action='append', 1072d57dc69SVladimir Kotal help='Environment variables in the form of name=value') 1082d57dc69SVladimir Kotal parser.add_argument('--doprint', type=str2bool, nargs=1, default=None, 1092d57dc69SVladimir Kotal metavar='boolean', 1102d57dc69SVladimir Kotal help='Enable/disable printing of messages ' 1112d57dc69SVladimir Kotal 'from the application as they are produced.') 1122d57dc69SVladimir Kotal 1132d57dc69SVladimir Kotal group = parser.add_mutually_exclusive_group(required=True) 1142d57dc69SVladimir Kotal group.add_argument('-a', '--jar', 1152d57dc69SVladimir Kotal help='Path to jar archive to run') 1162d57dc69SVladimir Kotal group.add_argument('-c', '--classpath', 1172d57dc69SVladimir Kotal help='Class path') 1182d57dc69SVladimir Kotal 1192d57dc69SVladimir Kotal parser.add_argument('options', nargs='+', help='options') 1202d57dc69SVladimir Kotal 1212d57dc69SVladimir Kotal return parser 122