def main(args=None): # # Top-level command that executes everything # # # Import plugins # import pyomo.environ # # Parse command-line options. # options = PySPConfigBlock() convertddsip_register_options(options) # # Prevent the compile_scenario_instances option from # appearing on the command line. This script relies on # the original constraints being present on the model # argparse_val = options.get('compile_scenario_instances')._argparse options.get('compile_scenario_instances')._argparse = None try: ap = argparse.ArgumentParser(prog='pyomo.pysp.convert.ddsip') options.initialize_argparse(ap) # restore the option so the class validation does not # raise an exception options.get('compile_scenario_instances')._argparse = argparse_val options.import_argparse(ap.parse_args(args=args)) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified # - catch it to exit gracefully. return _exc.code return launch_command(run_convertddsip, options, error_label="pyomo.pysp.convert.ddsip: ", disable_gc=options.disable_gc, profile_count=options.profile, traceback=options.traceback)
def main(args=None): # # Top-level command that executes everything # # # Import plugins # import pyomo.environ # # Parse command-line options. # options = PySPConfigBlock() pysp2smps_register_options(options) # # Prevent the compile_scenario_instances option from # appearing on the command line. This script relies on # the original constraints being present on the model # argparse_val = options.get('compile_scenario_instances')._argparse options.get('compile_scenario_instances')._argparse = None try: ap = argparse.ArgumentParser(prog='pysp2smps') options.initialize_argparse(ap) # restore the option so the class validation does not # raise an exception options.get('compile_scenario_instances')._argparse = argparse_val options.import_argparse(ap.parse_args(args=args)) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified # - catch it to exit gracefully. return _exc.code return launch_command(run_pysp2smps, options, error_label="pysp2smps: ", disable_gc=options.disable_gc, profile_count=options.profile, traceback=options.traceback)
def parse_command_line(args, register_options_callback, with_extensions=None, **kwds): import pyomo.pysp.plugins pyomo.pysp.plugins.load() from pyomo.pysp.util.config import _domain_tuple_of_str registered_extensions = {} if with_extensions is not None: for name in with_extensions: plugins = ExtensionPoint(with_extensions[name]) for plugin in plugins(all=True): registered_extensions.setdefault(name,[]).\ append(plugin.__class__.__module__) def _get_argument_parser(options): # if we modify this and don't copy it, # the this output will appear twice the second # time this function gets called _kwds = dict(kwds) if len(registered_extensions) > 0: assert with_extensions is not None epilog = _kwds.pop('epilog',"") if epilog != "": epilog += "\n\n" epilog += "Registered Extensions:\n" for name in registered_extensions: epilog += " - "+str(with_extensions[name].__name__)+": " epilog += str(registered_extensions[name])+"\n" _kwds['epilog'] = epilog ap = argparse.ArgumentParser( add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter, **_kwds) options.initialize_argparse(ap) ap.add_argument("-h", "--help", dest="show_help", action="store_true", default=False, help="show this help message and exit") return ap # # Register options # options = PySPConfigBlock() register_options_callback(options) if with_extensions is not None: for name in with_extensions: configval = options.get(name, None) assert configval is not None assert configval._domain is _domain_tuple_of_str ap = _get_argument_parser(options) # First parse known args, then import any extension plugins # specified by the user, regenerate the options block and # reparse to pick up plugin specific registered options opts, _ = ap.parse_known_args(args=args) options.import_argparse(opts) extensions = {} if with_extensions is None: if opts.show_help: pass else: if all(len(options.get(name).value()) == 0 for name in with_extensions) and \ opts.show_help: ap.print_help() sys.exit(0) for name in with_extensions: extensions[name] = load_extensions( options.get(name).value(), with_extensions[name]) # regenerate the options options = PySPConfigBlock() register_options_callback(options) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.register_options(options) # do a dummy access to option to prevent # a warning about it not being used options.get(name).value() ap = _get_argument_parser(options) opts = ap.parse_args(args=args) options.import_argparse(opts) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.set_options(options) if opts.show_help: ap.print_help() sys.exit(0) if with_extensions: for name in extensions: extensions[name] = sort_extensions_by_precedence(extensions[name]) return options, extensions else: return options