+booleans = {'1': True , 'yes': True , 'y': True , 'on' : True , 'true' : True , 'ok' : True,
+ '0': False, 'no' : False, 'n': False, 'off': False, 'false': False, 'cancel' : False}
+
+boolean_choices = list(booleans.keys())
+
+
+class CheckEmbeddedAction(argparse.Action):
+ def __call__(self, parser, namespace, value, option_string=None):
+ assert value is not None
+ if namespace.embedded:
+ embedded = [a for a in re.split("[:;,]", namespace.embedded) if a.strip()]
+ else:
+ embedded = []
+ if namespace.standalone:
+ standalone = [a for a in re.split("[:;,]", namespace.standalone) if a.strip()]
+ else:
+ standalone = []
+ vals = [a for a in re.split("[:;,]", value) if a.strip()]
+ for v in vals:
+ if v not in embedded_choices:
+ raise argparse.ArgumentError("option %s: invalid choice: %r (choose from %s)"
+ % (self.dest, v, ", ".join(map(repr, embedded_choices))))
+ if v not in embedded:
+ embedded.append(v)
+ if v in standalone:
+ del standalone[standalone.index(v)]
+ pass
+ namespace.embedded = ",".join(embedded)
+ namespace.standalone = ",".join(standalone)
+ pass
+
+
+class CheckStandaloneAction(argparse.Action):
+ def __call__(self, parser, namespace, value, option_string=None):
+ assert value is not None
+ if namespace.embedded:
+ embedded = [a for a in re.split("[:;,]", namespace.embedded) if a.strip()]
+ else:
+ embedded = []
+ if namespace.standalone:
+ standalone = [a for a in re.split("[:;,]", namespace.standalone) if a.strip()]
+ else:
+ standalone = []
+ vals = [a for a in re.split("[:;,]", value) if a.strip()]
+ for v in vals:
+ if v not in standalone_choices:
+ raise argparse.ArgumentError("option %s: invalid choice: %r (choose from %s)"
+ % (self.dest, v, ", ".join(map(repr, standalone_choices))))
+ if v not in standalone:
+ standalone.append(v)
+ if v in embedded:
+ del embedded[embedded.index(v)]
+ pass
+ namespace.embedded = ",".join(embedded)
+ namespace.standalone = ",".join(standalone)
+
+
+class StoreBooleanAction(argparse.Action):
+ def __call__(self, parser, namespace, value, option_string=None):
+ if isinstance(value, bytes):
+ value = value.decode()
+ if isinstance(value, str):
+ try:
+ value_conv = booleans[value.strip().lower()]
+ setattr(namespace, self.dest, value_conv)
+ except KeyError:
+ raise argparse.ArgumentError(
+ "option %s: invalid boolean value: %s (choose from %s)"
+ % (self.dest, value, boolean_choices))
+ else:
+ setattr(namespace, self.dest, value)
+
+
+def CreateOptionParser(exeName=None):
+
+ if not exeName:
+ exeName = "%(prog)s"
+
+ a_usage = """%s [options] [STUDY_FILE] [PYTHON_FILE [args] [PYTHON_FILE [args]...]]
+Python file arguments, if any, must be comma-separated (without blank characters) and prefixed by "args:" (without quotes), e.g. myscript.py args:arg1,arg2=val,...
+""" % exeName
+ version_str = "Salome %s" % version()
+ pars = argparse.ArgumentParser(usage=a_usage)
+
+ # Version
+ pars.add_argument('-v', '--version', action='version', version=version_str)
+
+ # GUI/Terminal. Default: GUI
+ help_str = "Launch without GUI (in the terminal mode)."
+ pars.add_argument("-t",
+ "--terminal",
+ action="store_false",
+ dest="gui",
+ help=help_str)
+
+ help_str = "Launch in Batch Mode. (Without GUI on batch machine)"
+ pars.add_argument("-b",
+ "--batch",
+ action="store_true",
+ dest="batch",
+ help=help_str)
+
+ help_str = "Launch in GUI mode [default]."
+ pars.add_argument("-g",
+ "--gui",
+ action="store_true",
+ dest="gui",
+ help=help_str)
+
+ # Show Desktop (only in GUI mode). Default: True
+ help_str = "1 to activate GUI desktop [default], "
+ help_str += "0 to not activate GUI desktop (Session_Server starts, but GUI is not shown). "
+ help_str += "Ignored in the terminal mode."
+ pars.add_argument("-d",
+ "--show-desktop",
+ metavar="<1/0>",
+ action=StoreBooleanAction,
+ dest="desktop",
+ help=help_str)
+ help_str = "Do not activate GUI desktop (Session_Server starts, but GUI is not shown). "
+ help_str += "The same as --show-desktop=0."
+ pars.add_argument("-o",
+ "--hide-desktop",
+ action="store_false",
+ dest="desktop",
+ help=help_str)
+
+ # Use logger or log-file. Default: nothing.
+ help_str = "Redirect messages to the CORBA collector."
+ pars.add_argument("-l",
+ "--logger",
+ action="store_const", const="CORBA",
+ dest="log_file",
+ help=help_str)
+ help_str = "Redirect messages to the <log-file>"
+ pars.add_argument("-f",
+ "--log-file",
+ metavar="<log-file>",
+ dest="log_file",
+ help=help_str)
+
+ # Configuration XML file. Default: see defaultUserFile() function
+ help_str = "Parse application settings from the <file> "
+ help_str += "instead of default %s" % defaultUserFile()
+ pars.add_argument("-r",
+ "--resources",
+ metavar="<file>",
+ dest="resources",
+ help=help_str)
+
+ # Use own xterm for each server. Default: False.
+ help_str = "Launch each SALOME server in own xterm console"
+ pars.add_argument("-x",
+ "--xterm",
+ action="store_true",
+ dest="xterm",
+ help=help_str)
+
+ # Modules. Default: Like in configuration files.
+ help_str = "SALOME modules list (where <module1>, <module2> are the names "
+ help_str += "of SALOME modules which should be available in the SALOME session)"
+ pars.add_argument("-m",
+ "--modules",
+ metavar="<module1,module2,...>",
+ action="append",
+ dest="modules",
+ help=help_str)
+
+ # Embedded servers. Default: Like in configuration files.
+ help_str = "CORBA servers to be launched in the Session embedded mode. "
+ help_str += "Valid values for <serverN>: %s " % ", ".join( embedded_choices )
+ help_str += "[by default the value from the configuration files is used]"
+ pars.add_argument("-e",
+ "--embedded",
+ metavar="<server1,server2,...>",
+ action=CheckEmbeddedAction,
+ dest="embedded",
+ help=help_str)
+
+ # Standalone servers. Default: Like in configuration files.
+ help_str = "CORBA servers to be launched in the standalone mode (as separate processes). "
+ help_str += "Valid values for <serverN>: %s " % ", ".join( standalone_choices )
+ help_str += "[by default the value from the configuration files is used]"
+ pars.add_argument("-s",
+ "--standalone",
+ metavar="<server1,server2,...>",
+ action=CheckStandaloneAction,
+ dest="standalone",
+ help=help_str)
+
+ # Kill with port. Default: False.
+ help_str = "Kill SALOME with the current port"
+ pars.add_argument("-p",
+ "--portkill",
+ action="store_true",
+ dest="portkill",
+ help=help_str)
+
+ # Kill all. Default: False.
+ help_str = "Kill all running SALOME sessions"
+ pars.add_argument("-k",
+ "--killall",
+ action="store_true",
+ dest="killall",
+ help=help_str)
+
+ # Additional python interpreters. Default: 0.
+ help_str = "The number of additional external python interpreters to run. "
+ help_str += "Each additional python interpreter is run in separate "
+ help_str += "xterm session with properly set SALOME environment"
+ pars.add_argument("-i",
+ "--interp",
+ metavar="<N>",
+ type=int,
+ dest="interp",
+ help=help_str)
+
+ # Splash. Default: True.
+ help_str = "1 to display splash screen [default], "
+ help_str += "0 to disable splash screen. "
+ help_str += "This option is ignored in the terminal mode. "
+ help_str += "It is also ignored if --show-desktop=0 option is used."
+ pars.add_argument("-z",
+ "--splash",
+ metavar="<1/0>",
+ action=StoreBooleanAction,
+ dest="splash",
+ help=help_str)
+
+ # Catch exceptions. Default: True.
+ help_str = "1 (yes,true,on,ok) to enable centralized exception handling [default], "
+ help_str += "0 (no,false,off,cancel) to disable centralized exception handling."
+ pars.add_argument("-c",
+ "--catch-exceptions",
+ metavar="<1/0>",
+ action=StoreBooleanAction,
+ dest="catch_exceptions",
+ help=help_str)
+
+ # Print free port and exit
+ help_str = "Print free port and exit"
+ pars.add_argument("--print-port",
+ action="store_true",
+ dest="print_port",
+ help=help_str)
+
+ # launch only omniNames and Launcher server
+ help_str = "launch only omniNames and Launcher server"
+ pars.add_argument("--launcher_only",
+ action="store_true",
+ dest="launcher_only",
+ help=help_str)
+
+ # machine and port where is the Launcher
+ help_str = "machine and port where is the Launcher. Usage: "
+ help_str += "--launcher=machine:port"
+ pars.add_argument("--launcher",
+ metavar="<=machine:port>",
+ type=str,
+ dest="launcher",
+ help=help_str)
+
+ # Do not relink ${HOME}/.omniORB_last.cfg
+ help_str = "Do not save current configuration ${HOME}/.omniORB_last.cfg"
+ pars.add_argument("--nosave-config",
+ action="store_false",
+ dest="save_config",
+ help=help_str)
+
+ # Launch with interactive python console. Default: False.
+ help_str = "Launch with interactive python console."
+ pars.add_argument("--pinter",
+ action="store_true",
+ dest="pinter",
+ help=help_str)
+
+ # Print Naming service port into a user file. Default: False.
+ help_str = "Print Naming Service Port into a user file."
+ pars.add_argument("--ns-port-log",
+ metavar="<ns_port_log_file>",
+ dest="ns_port_log_file",
+ help=help_str)
+
+ # Write/read test script file with help of TestRecorder. Default: False.
+ help_str = "Write/read test script file with help of TestRecorder."
+ pars.add_argument("--test",
+ metavar="<test_script_file>",
+ dest="test_script_file",
+ help=help_str)
+
+ # Reproducing test script with help of TestRecorder. Default: False.
+ help_str = "Reproducing test script with help of TestRecorder."
+ pars.add_argument("--play",
+ metavar="<play_script_file>",
+ dest="play_script_file",
+ help=help_str)
+
+ # gdb session
+ help_str = "Launch session with gdb"
+ pars.add_argument("--gdb-session",
+ action="store_true",
+ dest="gdb_session",
+ help=help_str)