def _update_argparse_argdict(argdict): """converts to the pyNastranGUI argument format""" argdict['debug'] = not argdict['quiet'] del argdict['quiet'] if not GROUPS_DEFAULT: swap_key(argdict, 'nogroups', 'is_groups') else: argdict['is_groups'] = argdict['groups'] swap_key(argdict, 'points_fname', 'user_points') input_filenames = _add_inputs_outputs(argdict['INPUT'], argdict['input'], word='input') del argdict['INPUT'] argdict['input'] = input_filenames output_filenames = _add_inputs_outputs(argdict['OUTPUT'], argdict['output'], word='output') del argdict['OUTPUT'] argdict['output'] = output_filenames for output_filename in output_filenames: check_path(output_filename, name='output_filename') for input_filename in input_filenames: check_path(input_filename, name='input file') plugin = False if 'plugin' in argdict: plugin = True formats = argdict['format'] if input_filenames and formats is None: input_formats = [] for input_filenamei in input_filenames: if isinstance(input_filenamei, str): formati = determine_format(input_filenamei) else: # pragma: no cover raise TypeError('input_filenamei=%s type=%s' % (input_filenamei, type(input_filenamei))) input_formats.append(formati) #input_formats = [determine_format(input_filenamei) for input_filenamei in input_filenames] argdict['format'] = input_formats del input_formats elif formats: input_formats = [] for formati in formats: if isinstance(formati, str): input_formats.append(formati) else: input_formats.extend(formati) argdict['format'] = input_formats del input_formats del formats if not plugin: # None is for custom geometry allowed_formats = [ 'nastran', 'stl', 'cart3d', 'tecplot', 'ugrid', 'ugrid3d', 'panair', #'plot3d', 'surf', 'lawgs', 'degen_geom', 'shabp', 'avus', 'fast', 'abaqus', 'usm3d', 'bedge', 'su2', 'tetgen', 'openfoam_hex', 'openfoam_shell', 'openfoam_faces', 'obj', 'avl', None, ] for input_format in input_formats: assert input_format in allowed_formats, 'format=%r is not supported' % input_format if argdict['geomscript']: geom_script = argdict['geomscript'] check_path(geom_script, name='geomscript') if argdict['postscript']: post_script = argdict['postscript'] check_path(post_script, name='postscript') if argdict['qt'] is not None: qt = argdict['qt'].lower() assert qt in ['pyside', 'pyqt4', 'pyqt5', 'pyside2'], 'qt=%r' % qt os.environ.setdefault('QT_API', qt) #if argdict['input'] is None: #argdict['input'] = [] #inputs = { #'format' : input_format, #'input' : input_filename, #'output' : output_filename, #'debug' : debug, #'geomscript' : geom_script, #'postscript' : post_script, #'user_points' : user_points, #'user_geom' : user_geom, #'is_groups' : is_groups, #'log' : log, #'test' : test, #} formats = argdict['format'] ninput_files = len(input_filenames) if formats: if isinstance(formats, str): formats = [formats] nformats = len(formats) if nformats == 1 and ninput_files > 1: formats = formats * ninput_files argdict['format'] = formats if nformats != ninput_files: msg = ('nformats=%s formats=%s\n' 'ninput_files=%s input_filenames=%s' % (nformats, formats, ninput_files, input_filenames)) raise RuntimeError(msg) return argdict
def test_bdfv_argparse(argv=None): """test_bdf argument parser""" if argv is None: argv = sys.argv[1:] # same as argparse #print('get_inputs; argv was None -> %s' % argv) else: # drop the pyNastranGUI; same as argparse argv = argv[1:] encoding = sys.getdefaultencoding() import argparse parent_parser = argparse.ArgumentParser() parent_parser.add_argument('BDF_FILENAME', help='path to BDF/DAT/NAS file', type=str) parent_parser.add_argument('-v', '--version', action='version', version=pyNastran.__version__) #nargs : str/int # * : 0 or more # + : one or more # ? : optional # int : int values # -------------------------------------------------------------------------- # Options xref_safe_group = parent_parser.add_mutually_exclusive_group() xref_safe_group.add_argument( '-x', '--xref', action='store_false', help= 'disables cross-referencing and checks of the BDF (default=True -> on)' ) xref_safe_group.add_argument( '--safe', action='store_true', help='Use safe cross-reference (default=False)') parent_parser.add_argument( '-p', '--punch', action='store_true', help='disables reading the executive and case control decks in the BDF\n' '(default=False -> reads entire deck)') stop_check_group = parent_parser.add_mutually_exclusive_group() stop_check_group.add_argument( '-c', '--check', action='store_true', help='disables BDF checks. Checks run the methods on \n' ' every element/property to test them. May fails if a \n' ' card is fully not supported (default=False)') stop_check_group.add_argument( '--stop', action='store_true', # dev help='Stop after first read/write (default=False)\n') width_group = parent_parser.add_mutually_exclusive_group() width_group.add_argument( '-l', '--large', action='store_true', help= 'writes the BDF in large field, single precision format (default=False)' ) width_group.add_argument( '-d', '--double', action='store_true', help= 'writes the BDF in large field, double precision format (default=False)' ) parent_parser.add_argument( '-L', '--loads', action='store_false', help= 'Disables forces/moments summation for the different subcases (default=True)' ) parent_parser.add_argument( '-e', '--nerrors', nargs=1, default=100, help='Allow for cross-reference errors (default=100)') parent_parser.add_argument('--encoding', nargs=1, default=encoding, help='the encoding method (default=%r)\n' % encoding) parent_parser.add_argument('-q', '--quiet', action='store_true', help='prints debug messages (default=False)') # -------------------------------------------------------------------------- #'Developer:\n' parent_parser.add_argument( '--crash', nargs=1, type=str, help='Crash on specific cards (e.g. CGEN,EGRID)') parent_parser.add_argument( '--dumplines', action='store_true', help='Writes the BDF exactly as read with the INCLUDEs processed\n' '(pyNastran_dump.bdf)') parent_parser.add_argument( '--dictsort', action='store_true', help='Writes the BDF exactly as read with the INCLUDEs processed\n' '(pyNastran_dict.bdf)') parent_parser.add_argument('--profile', action='store_true', help='Profiles the code (default=False)\n') parent_parser.add_argument( '--pickle', action='store_true', help='Pickles the data objects (default=False)\n') parent_parser.add_argument('--hdf5', action='store_true', help='Save/load the BDF in HDF5 format') usage, args, examples = get_test_bdf_usage_args_examples(encoding) # -------------------------------------------------------------------------- #argv #print(argv) usage, args, examples = get_test_bdf_usage_args_examples(encoding) usage = usage.replace('test_bdf', 'test_bdfv') examples = examples.replace('test_bdf', 'test_bdfv') msg = usage + args + examples update_message(parent_parser, usage, args, examples) #try: #args = parent_parser.parse_args(args=argv) #except SystemExit: #fobj = StringIO() ##args = parent_parser.format_usage() #parent_parser.print_usage(file=fobj) #args = fobj.getvalue() #raise args = parent_parser.parse_args(args=argv) args2 = argparse_to_dict(args) optional_args = [ 'double', 'large', 'crash', 'quiet', 'profile', 'xref', 'safe', 'check', 'punch', 'loads', 'stop', 'encoding', 'dumplines', 'dictsort', 'nerrors', 'pickle', 'hdf5', ] for arg in optional_args: swap_key(args2, arg, '--' + arg) return args2