def cmd_line_free_faces(argv=None, quiet=False): """command line interface to bdf free_faces""" if argv is None: argv = sys.argv encoding = sys.getdefaultencoding() import pyNastran usage = ( 'Usage:\n' ' bdf free_faces BDF_FILENAME SKIN_FILENAME [-d] [-l] [-f] [--encoding ENCODE]\n' ' bdf free_faces -h | --help\n' ' bdf free_faces -v | --version\n' '\n') arg_msg = ( "Positional Arguments:\n" " BDF_FILENAME path to input BDF/DAT/NAS file\n" " SKIN_FILENAME path to output BDF/DAT/NAS file\n" '\n' 'Options:\n' ' -l, --large writes the BDF in large field, single precision format (default=False)\n' ' -d, --double writes the BDF in large field, double precision format (default=False)\n' ' --encoding ENCODE the encoding method (default=None -> %r)\n' '\n' 'Developer:\n' ' -f, --profile Profiles the code (default=False)\n' '\n' "Info:\n" ' -h, --help show this help message and exit\n' " -v, --version show program's version number and exit\n" % encoding) if len(argv) == 1: sys.exit(arg_msg) arg_msg += '\n' examples = ('Examples\n' '--------\n' ' bdf free_faces solid.bdf skin.bdf\n' ' bdf free_faces solid.bdf skin.bdf --large\n') import argparse parent_parser = argparse.ArgumentParser() # positional arguments parent_parser.add_argument('BDF_FILENAME', help='path to input BDF/DAT/NAS file', type=str) parent_parser.add_argument('SKIN_FILENAME', help='path to output BDF/DAT/NAS file', type=str) size_group = parent_parser.add_mutually_exclusive_group() size_group.add_argument( '-d', '--double', help='writes the BDF in large field, single precision format', action='store_true') size_group.add_argument( '-l', '--large', help='writes the BDF in large field, double precision format', action='store_true') size_group.add_argument( '--encoding', help='the encoding method (default=None -> {repr(encoding)})', type=str) parent_parser.add_argument('--profile', help='Profiles the code', action='store_true') parent_parser.add_argument('-v', '--version', action='version', version=pyNastran.__version__) from pyNastran.utils.arg_handling import argparse_to_dict, update_message update_message(parent_parser, usage, arg_msg, examples) print(argv) args = parent_parser.parse_args(args=argv[2:]) data = argparse_to_dict(args) if not quiet: # pragma: no cover for key, value in sorted(data.items()): print("%-12s = %r" % (key.strip('--'), value)) import time time0 = time.time() is_double = False if data['double']: size = 16 is_double = True elif data['large']: size = 16 else: size = 8 bdf_filename = data['BDF_FILENAME'] skin_filename = data['SKIN_FILENAME'] from pyNastran.bdf.mesh_utils.bdf_equivalence import bdf_equivalence_nodes tol = 1e-005 bdf_filename_merged = 'merged.bdf' level = 'debug' if not quiet else 'warning' log = SimpleLogger(level=level, encoding='utf-8', log_func=None) bdf_equivalence_nodes(bdf_filename, bdf_filename_merged, tol, renumber_nodes=False, neq_max=10, xref=True, node_set=None, size=8, is_double=is_double, remove_collapsed_elements=False, avoid_collapsed_elements=False, crash_on_collapse=False, log=log, debug=True) if not quiet: # pragma: no cover print('done with equivalencing') write_skin_solid_faces( bdf_filename_merged, skin_filename, write_solids=False, write_shells=True, size=size, is_double=is_double, encoding=None, log=log, ) if not quiet: # pragma: no cover print("total time: %.2f sec" % (time.time() - time0))
def run_argparse(argv): """Gets the inputs for pyNastranGUI using argparse.""" import argparse #msg = "Usage:\n" # INPUT format may be explicitly or implicitly defined with or # without an output file #test = ' [--test]' #qt = ' [--qt QT] [--plugin]' #msg += " pyNastranGUI INPUT [-f FORMAT] [-o OUTPUT]\n" #msg += ' [-q] [--groups] [--noupdate] [--log LOG]%s%s\n' % (test, qt) # You don't need to throw a -o flag #msg += " pyNastranGUI INPUT OUTPUT [-f FORMAT] [-o OUTPUT]\n" #msg += ' [-q] [--groups] [--noupdate] [--log LOG]%s%s\n' % (test, qt) dev = '' dev_list = [] if not pyNastran.is_pynastrangui_exe: #dev = ' [--noupdate] [--test] [--qt Qt] [--plugin]' dev_list = ['--noupdate', '--test', '--qt', '--plugin'] dev = ''.join([' [%s]' % devi for devi in dev_list]) # no input/output files # can you ever have an OUTPUT, but no INPUT? usage = 'Usage:\n' usage += ' pyNastranGUI INPUT [-f FORMAT] [-o OUTPUT] [options]\n' # You don't need to throw a -o flag usage += ' pyNastranGUI INPUT OUTPUT [-f FORMAT] [-o OUTPUT] [options]\n' # no input/output files # can you ever have an OUTPUT, but no INPUT? usage += ' pyNastranGUI [-f FORMAT] [-i INPUT] [-o OUTPUT...] [options]\n' #usage += ' pyNastranGUI -h | --help\n' usage += ' pyNastranGUI -v | --version\n' usage += ( ' [options] = [-g GSCRIPT] [-p PSCRIPT]\n' ' [-u POINTS_FNAME...] [--user_geom GEOM_FNAME...]\n') if GROUPS_DEFAULT: usage += ' [-q] [--groups] [--log LOG]%s\n' % (dev) else: usage += ' [-q] [--nogroups] [--log LOG]%s\n' % (dev) arg_msg = '' arg_msg += '\n' arg_msg += 'Primary Options:\n' # plot3d, arg_msg += ' -f FORMAT, --format FORMAT format type (avus, bedge, cart3d, lawgs, nastran,\n' arg_msg += ' openfoam_hex, openfoam_shell, openfoam_faces,\n' arg_msg += ' panair, stl, surf, tetgen, usm3d, ugrid, ugrid3d)\n' arg_msg += ' -i INPUT, --input INPUT path to input file\n' arg_msg += ' -o OUTPUT, --output OUTPUT path to output file\n' #arg_msg += " -r XYZ, --rotation XYZ [x, y, z, -x, -y, -z] default is ???\n" arg_msg += '\n' arg_msg += 'Secondary Options:\n' if GROUPS_DEFAULT: arg_msg += ' --groups enables groups\n' else: arg_msg += ' --nogroups disables groups\n' arg_msg += ' -g GSCRIPT, --geomscript path to geometry script file (runs before load geometry)\n' arg_msg += ' -p PSCRIPT, --postscript path to post script file (runs after load geometry)\n' arg_msg += ' --user_geom GEOM_FNAME add user specified geometry (repeatable)\n' arg_msg += ' -u POINTS_FNAME, --user_points add user specified points (repeatable)\n' arg_msg += '\n' arg_msg += "Debug:\n" if not pyNastran.is_pynastrangui_exe: arg_msg += ' --noupdate disables the update check\n' arg_msg += ' --test temporary dev mode (default=False)\n' arg_msg += ' --qt QT sets the qt version (default=QT_API)\n' arg_msg += ' --plugin disables the format check\n' arg_msg += ' --log LOG disables HTML logging; prints to the screen\n' arg_msg += '\n' arg_msg += 'Info:\n' arg_msg += ' -q, --quiet prints debug messages (default=True)\n' arg_msg += ' -h, --help show this help message and exits\n' arg_msg += " -v, --version show program's version number and exit\n" arg_msg += '\n' #msg += "\n" #parser = argparse.ArgumentParser( #prog=None, usage=None, description=None, epilog=None, #version=None, parents=[], formatter_class=HelpFormatter, #prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, #conflict_handler='error', add_help=True) #usage = '[options]' examples = ( 'Examples\n' '--------\n' ' pyNastranGUI\n' ' pyNastranGUI fem.bdf\n' ' pyNastranGUI fem.bdf fem.op2\n' ' pyNastranGUI fem.dat fem.op2 --format nastran -o fem2.op2\n\n') #import textwrap parent_parser = argparse.ArgumentParser( #prog = 'pyNastranGUI', #usage = usage, #description='A foo that bars', #epilog="And that's how you'd foo a bar", #formatter_class=argparse.RawDescriptionHelpFormatter, #description=textwrap.dedent(text), #version=pyNastran.__version__, #add_help=False, ) # pyNastranGUI INPUT OUTPUT [-f FORMAT] [-o OUTPUT] # positional arguments parent_parser.add_argument('INPUT', nargs='?', help='path to input file', type=str) parent_parser.add_argument('OUTPUT', nargs='?', help='path to output file', type=str) #nargs : str/int # * : 0 or more # + : one or more # ? : optional # int : int values #SUPPORT_MULTIMODEL = False #append_nargs = 1 if SUPPORT_MULTIMODEL else 1 append_action = 'append' if SUPPORT_MULTIMODEL else None parent_parser.add_argument('-i', '--input', help='path to input file', nargs=1, action=append_action) parent_parser.add_argument('-o', '--output', help='path to output file', nargs=1, action=append_action) #parent_parser.add_argument('--user_geom', type=str, help='log msg') parent_parser.add_argument( '-f', '--format', type=str, nargs=1, action=append_action, help='format type (avus, bedge, cart3d, lawgs, nastran, ' 'openfoam_hex, openfoam_shell, openfoam_faces, panair, ' 'stl, surf, tetgen, usm3d, ugrid, ugrid3d, #plot3d)') # double args #parent_parser.add_argument('-f', '--format', type=str, #help='format type (avus, bedge, cart3d, lawgs, nastran, ' #'openfoam_hex, openfoam_shell, openfoam_faces, panair, ' #'stl, surf, tetgen, usm3d, ugrid, ugrid3d, #plot3d)', #action='append') parent_parser.add_argument( '-g', '--geomscript', type=str, help='path to geometry script file (runs before load geometry)') parent_parser.add_argument( '-p', '--postscript', type=str, help='path to post script file (runs after load geometry)') parent_parser.add_argument( '-u', '--points_fname', type=str, action='append', help= 'an (nrows, 3) comma/tab/space separated list of points (repeatable)') parent_parser.add_argument('--user_geom', type=str, action='append', help='add user specified geometry (repeatable)') parent_parser.add_argument('--log', type=str, help='{debug, info, warning, error} msg') # no arguments if dev: parent_parser.add_argument('--qt', type=str, help='{pyqt4, pyqt5, pyside, pyside2} msg') parent_parser.add_argument('--test', help='test msg', action='store_true') parent_parser.add_argument('--noupdate', help='noupdate msg', action='store_true') parent_parser.add_argument('--plugin', help='disables the format check', action='store_true') if GROUPS_DEFAULT: parent_parser.add_argument('--groups', help='enables groups', action='store_true') else: parent_parser.add_argument('--nogroups', help='disables groups', action='store_false') parent_parser.add_argument('-q', '--quiet', help='prints debug messages (default=True)', action='store_true') #parent_parser.add_argument('-h', '--help', help='show this help message and exits', #action='store_true') parent_parser.add_argument('-v', '--version', action='version', version=pyNastran.__version__) #foo_parser = argparse.ArgumentParser(parents=[parent_parser]) #foo_parser.parse_args(['INPUT', '--format', '--output', #'--geomscript', '--postscript', '--points_fname', '--user_geom', #'--quiet', '--groups', '--log' '--help'] + dev_list) #msg += " pyNastranGUI INPUT [-f FORMAT] [-o OUTPUT]\n" #parser_no_output = p #parser = argparse.ArgumentParser( #description='A foo that bars', #epilog="And that's how you'd foo a bar", #version=pyNastran.__version__, #) #parser.add_argument("square", help="display a square of a given number", #type=int) #parser.add_argument('-v', '--version', action='version', #version='%%(prog)s %s' % pyNastran.__version__) #parser.add_argument("-w", "--verbosity", type=int, choices=[0, 1, 2], #help="increase output verbosity") #mymsg = 'replacing argparse message' #def print_help(self, file=None): #if file is None: #file = _sys.stdout #self._print_message(self.format_help(), file) update_message(parent_parser, usage, arg_msg, examples) args = parent_parser.parse_args(args=argv) #args.plugin = True argdict = argparse_to_dict(args) _update_argparse_argdict(argdict) 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