Skip to content

Commit 9d1ffe0

Browse files
committed
support for arguments to plugin commands:
- add a simple parser that only figures out where the config is, and does not complain if args are unknown
1 parent d2fa404 commit 9d1ffe0

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

electrum/commands.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,28 @@ def add_wallet_option(parser):
18031803
parser.add_argument("--forgetconfig", action="store_true", dest=SimpleConfig.CONFIG_FORGET_CHANGES.key(), default=False, help="Forget config on exit")
18041804

18051805

1806+
def get_simple_parser():
1807+
""" simple parser that figures out the path of the config file and ignore unknown args """
1808+
from optparse import OptionParser, BadOptionError, AmbiguousOptionError
1809+
class PassThroughOptionParser(OptionParser):
1810+
# see https://stackoverflow.com/questions/1885161/how-can-i-get-optparses-optionparser-to-ignore-invalid-options
1811+
def _process_args(self, largs, rargs, values):
1812+
while rargs:
1813+
try:
1814+
OptionParser._process_args(self,largs,rargs,values)
1815+
except (BadOptionError,AmbiguousOptionError) as e:
1816+
largs.append(e.opt_str)
1817+
parser = PassThroughOptionParser()
1818+
parser.add_option("-D", "--dir", dest="electrum_path", help="electrum directory")
1819+
parser.add_option("-P", "--portable", action="store_true", dest="portable", default=False, help="Use local 'electrum_data' directory")
1820+
parser.add_option("--testnet", action="store_true", dest="testnet", default=False, help="Use Testnet")
1821+
parser.add_option("--testnet4", action="store_true", dest="testnet4", default=False, help="Use Testnet4")
1822+
parser.add_option("--regtest", action="store_true", dest="regtest", default=False, help="Use Regtest")
1823+
parser.add_option("--simnet", action="store_true", dest="simnet", default=False, help="Use Simnet")
1824+
parser.add_option("--signet", action="store_true", dest="signet", default=False, help="Use Signet")
1825+
return parser
1826+
1827+
18061828
def get_parser():
18071829
# create main parser
18081830
parser = argparse.ArgumentParser(
@@ -1843,7 +1865,11 @@ def get_parser():
18431865
continue
18441866
if optname in ['plugin']:
18451867
continue
1846-
a, help = command_options[optname]
1868+
if optname in command_options:
1869+
a, help = command_options[optname]
1870+
else:
1871+
a = None
1872+
help = None
18471873
b = '--' + optname
18481874
action = "store_true" if default is False else 'store'
18491875
args = (a, b) if a else (b,)

electrum/plugins/labels/commands.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ async def push(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None) ->
1515

1616

1717
@plugin_command('w', plugin_name)
18-
async def pull(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None) -> int:
18+
async def pull(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None, force=False) -> int:
1919
""" pull labels from server """
20-
return await plugin.pull_thread(wallet, force=False)
20+
return await plugin.pull_thread(wallet, force=force)

run_electrum

+19-12
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ from electrum.storage import WalletStorage
103103
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
104104
from electrum.util import InvalidPassword
105105
from electrum.plugin import Plugins
106-
from electrum.commands import get_parser, known_commands, Commands, config_variables
106+
from electrum.commands import get_parser, get_simple_parser, known_commands, Commands, config_variables
107107
from electrum import daemon
108108
from electrum import keystore
109109
from electrum.util import create_and_start_event_loop, UserFacingException, JsonRPCError
@@ -271,15 +271,21 @@ def sys_exit(i):
271271
loop_thread.join(timeout=1)
272272
sys.exit(i)
273273

274-
def parse_command_line() -> Dict:
274+
def parse_command_line(simple_parser=False) -> Dict:
275275
# parse command line from sys.argv
276-
parser = get_parser()
277-
args = parser.parse_args()
278-
config_options = args.__dict__
279-
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys()
280-
config_options = {key: config_options[key] for key in filter(f, config_options.keys())}
281-
if config_options.get(SimpleConfig.NETWORK_SERVER.key()):
282-
config_options[SimpleConfig.NETWORK_AUTO_CONNECT.key()] = False
276+
if simple_parser:
277+
parser = get_simple_parser()
278+
options, args = parser.parse_args()
279+
config_options = options.__dict__
280+
config_options['cmd'] = 'gui'
281+
else:
282+
parser = get_parser()
283+
args = parser.parse_args()
284+
config_options = args.__dict__
285+
f = lambda key: config_options[key] is not None and key not in config_variables.get(args.cmd, {}).keys()
286+
config_options = {key: config_options[key] for key in filter(f, config_options.keys())}
287+
if config_options.get(SimpleConfig.NETWORK_SERVER.key()):
288+
config_options[SimpleConfig.NETWORK_AUTO_CONNECT.key()] = False
283289

284290
config_options['cwd'] = cwd = os.getcwd()
285291

@@ -361,10 +367,11 @@ def main():
361367
# save sys args for next parser
362368
saved_sys_argv = sys.argv[:]
363369
# disable help, the next parser will display it
364-
if '-h' in sys.argv:
365-
sys.argv.remove('-h')
370+
for x in sys.argv:
371+
if x in ['-h', '--help']:
372+
sys.argv.remove(x)
366373
# parse first without plugins
367-
config_options = parse_command_line()
374+
config_options = parse_command_line(simple_parser=True)
368375
tmp_config = SimpleConfig(config_options)
369376
# load (only) the commands modules of plugins so their commands are registered
370377
plugin_commands = Plugins(tmp_config, cmd_only=True)

0 commit comments

Comments
 (0)