From 890ee4d149f776a820b972a015bdc43b4f3f3695 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 27 Jan 2025 16:01:53 +0000 Subject: [PATCH] opts.py (global opts), help.__init__: minor fixes & cleanups --- mmgen/help/__init__.py | 35 +++++++++++++++-------------------- mmgen/opts.py | 24 ++++++++++++++---------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/mmgen/help/__init__.py b/mmgen/help/__init__.py index c0614d80..5b4e138e 100755 --- a/mmgen/help/__init__.py +++ b/mmgen/help/__init__.py @@ -62,7 +62,7 @@ def usage(cfg): class Help: - def make(self, cfg, opts, proto): + def make(self, cfg, opts): def gen_arg_tuple(func, text): @@ -86,27 +86,28 @@ class Help: yield d[arg] if arg in d else text def gen_output(): - yield ' {} {}'.format(gc.prog_name.upper() + ':', text['desc'].strip()) + yield ' {} {}'.format(gc.prog_name.upper() + ':', opts.opts_data['text']['desc'].strip()) yield make_usage_str(cfg, caller='help') - yield help_type.upper().replace('_', ' ') + ':' + yield self.help_type.upper().replace('_', ' ') + ':' # process code for options opts_text = nl.join(self.gen_text(opts)) - if help_type in code: - yield code[help_type](*tuple(gen_arg_tuple(code[help_type], opts_text))) + if 'options' in code: + yield code['options'](*tuple(gen_arg_tuple(code['options'], opts_text))) else: yield opts_text # process code for notes - if help_type == 'options' and 'notes' in text: + if 'notes' in text: if 'notes' in code: yield from code['notes'](*tuple(gen_arg_tuple(code['notes'], text['notes']))).splitlines() else: yield from text['notes'].splitlines() - text = opts.opts_data['text'] - code = opts.opts_data['code'] - help_type = self.help_type + from ..protocol import init_proto_from_cfg + proto = init_proto_from_cfg(cfg, need_amt=True) + text = getattr(opts, self.data_desc)['text'] + code = getattr(opts, self.data_desc)['code'] nl = '\n ' return nl.join(gen_output()) + '\n' @@ -114,6 +115,7 @@ class Help: class CmdHelp(Help): help_type = 'options' + data_desc = 'opts_data' def gen_text(self, opts): opt_filter = opts.opt_filter @@ -137,10 +139,12 @@ class CmdHelp(Help): class GlobalHelp(Help): help_type = 'global_options' + data_desc = 'global_opts_data' def gen_text(self, opts): from ..opts import global_opts_pat - for line in opts.global_opts_data['text'][1:-2].splitlines(): + skipping = False + for line in opts.global_opts_data['text']['options'][1:-3].splitlines(): if m := global_opts_pat.match(line): if m[1] in opts.global_opts_filter.coin and m[2] in opts.global_opts_filter.cmd: yield ' --{} {}'.format(m[3], m[5]) @@ -152,18 +156,9 @@ class GlobalHelp(Help): def print_help(cfg, opts): - from ..protocol import init_proto_from_cfg - proto = init_proto_from_cfg(cfg, need_amt=True) - if not 'code' in opts.opts_data: opts.opts_data['code'] = {} - if cfg.help: - cls = CmdHelp - else: - opts.opts_data['code']['global_options'] = opts.global_opts_data['code'] - cls = GlobalHelp - from ..ui import do_pager - do_pager(cls().make(cfg, opts, proto)) + do_pager((CmdHelp if cfg.help else GlobalHelp)().make(cfg, opts)) sys.exit(0) diff --git a/mmgen/opts.py b/mmgen/opts.py index cd0390be..870ff9fc 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -139,7 +139,7 @@ def parse_opts(opts_data, opt_filter, global_opts_data, global_opts_filter): yield (m[2], ret) def parse_global_opts_text(): - for line in global_opts_data['text'].splitlines(): + for line in global_opts_data['text']['options'].splitlines(): m = global_opts_pat.match(line) if m and m[1] in global_opts_filter.coin and m[2] in global_opts_filter.cmd: yield (m[3], opt_tuple(m[3].replace('-', '_'), m[4] == '=')) @@ -249,7 +249,8 @@ class UserOpts(Opts): global_opts_data = { # coin code : cmd code : opt : opt param : text - 'text': """ + 'text': { + 'options': """ -- --accept-defaults Accept defaults at all prompts hp --cashaddr=0|1 Display addresses in cashaddr format (default: 1) -p --coin=c Choose coin unit. Default: BTC. Current choice: {cu_dfl} @@ -266,10 +267,10 @@ class UserOpts(Opts): rr --ignore-daemon-version Ignore coin daemon version check rr --http-timeout=t Set HTTP timeout in seconds for JSON-RPC connections -- --no-license Suppress the GPL license prompt - rr --rpc-host=HOST Communicate with coin daemon running on host HOST + Rr --rpc-host=HOST Communicate with coin daemon running on host HOST rr --rpc-port=PORT Communicate with coin daemon listening on port PORT - rr --rpc-user=USER Authenticate to coin daemon using username USER - rr --rpc-password=PASS Authenticate to coin daemon using password PASS + br --rpc-user=USER Authenticate to coin daemon using username USER + br --rpc-password=PASS Authenticate to coin daemon using password PASS Rr --rpc-backend=backend Use backend 'backend' for JSON-RPC communications Rr --aiohttp-rpc-queue-len=N Use N simultaneous RPC connections with aiohttp -p --regtest=0|1 Disable or enable regtest mode @@ -282,11 +283,14 @@ class UserOpts(Opts): b- --bob Specify user ‘Bob’ in MMGen regtest mode b- --alice Specify user ‘Alice’ in MMGen regtest mode b- --carol Specify user ‘Carol’ in MMGen regtest mode - """, - 'code': lambda proto, help_notes, s: s.format( - pnm = gc.proj_name, - cu_dfl = proto.coin, - tw_name = help_notes('dfl_twname')) + """, + }, + 'code': { + 'options': lambda proto, help_notes, s: s.format( + pnm = gc.proj_name, + cu_dfl = proto.coin, + tw_name = help_notes('dfl_twname')), + } } @staticmethod