1
0

run_electrum: add parse_command_line method

This commit is contained in:
ThomasV
2025-03-15 11:44:24 +01:00
parent ae64583ebc
commit 4f79e516e4

View File

@@ -37,7 +37,7 @@ if sys.version_info[:3] < _min_python_version_tuple:
import warnings
import asyncio
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Dict
script_dir = os.path.dirname(os.path.realpath(__file__))
@@ -271,6 +271,39 @@ def sys_exit(i):
loop_thread.join(timeout=1)
sys.exit(i)
def parse_command_line() -> Dict:
# parse command line from sys.argv
parser = get_parser()
args = parser.parse_args()
config_options = args.__dict__
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys()
config_options = {key: config_options[key] for key in filter(f, config_options.keys())}
if config_options.get(SimpleConfig.NETWORK_SERVER.key()):
config_options[SimpleConfig.NETWORK_AUTO_CONNECT.key()] = False
config_options['cwd'] = cwd = os.getcwd()
# fixme: this can probably be achieved with a runtime hook (pyinstaller)
if is_pyinstaller and os.path.exists(os.path.join(sys._MEIPASS, 'is_portable')):
config_options['portable'] = True
if config_options.get('portable'):
if is_local:
# running from git clone or local source: put datadir next to main script
datadir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'electrum_data')
else:
# Running a binary or installed source. The most generic but still reasonable thing
# is to use the current working directory. (see #7732)
# note: The main script is often unpacked to a temporary directory from a bundled executable,
# and we don't want to put the datadir inside a temp dir.
# note: Re the portable .exe on Windows, when the user double-clicks it, CWD gets set
# to the parent dir, i.e. we will put the datadir next to the exe.
datadir = os.path.join(os.path.realpath(cwd), 'electrum_data')
config_options['electrum_path'] = datadir
if not config_options.get('verbosity'):
warnings.simplefilter('ignore', DeprecationWarning)
return config_options
def main():
global loop, stop_loop, loop_thread
@@ -309,9 +342,6 @@ def main():
sys.argv[i] = prompt_password('Enter argument (will not echo):', confirm=False)
# load (only) the commands modules of plugins so their commands are registered
plugin_commands = Plugins(cmd_only=True)
# parse command line
parser = get_parser()
args = parser.parse_args()
# config is an object passed to the various constructors (wallet, interface, gui)
if is_android:
@@ -330,34 +360,8 @@ def main():
# ~hack for easier regtest builds. pkgname subject to change.
config_options['regtest'] = True
else:
config_options = args.__dict__
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys()
config_options = {key: config_options[key] for key in filter(f, config_options.keys())}
if config_options.get(SimpleConfig.NETWORK_SERVER.key()):
config_options[SimpleConfig.NETWORK_AUTO_CONNECT.key()] = False
config_options = parse_command_line()
config_options['cwd'] = cwd = os.getcwd()
# fixme: this can probably be achieved with a runtime hook (pyinstaller)
if is_pyinstaller and os.path.exists(os.path.join(sys._MEIPASS, 'is_portable')):
config_options['portable'] = True
if config_options.get('portable'):
if is_local:
# running from git clone or local source: put datadir next to main script
datadir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'electrum_data')
else:
# Running a binary or installed source. The most generic but still reasonable thing
# is to use the current working directory. (see #7732)
# note: The main script is often unpacked to a temporary directory from a bundled executable,
# and we don't want to put the datadir inside a temp dir.
# note: Re the portable .exe on Windows, when the user double-clicks it, CWD gets set
# to the parent dir, i.e. we will put the datadir next to the exe.
datadir = os.path.join(os.path.realpath(cwd), 'electrum_data')
config_options['electrum_path'] = datadir
if not config_options.get('verbosity'):
warnings.simplefilter('ignore', DeprecationWarning)
config = SimpleConfig(config_options)
cmdname = config.get('cmd')