From e93aec5a194f6df02895b967b6019a2981f5d0ce Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 19 Oct 2026 09:06:13 +0000 Subject: [PATCH] opts_data 'requires': support non-boolean a-value --- mmgen/cfg.py | 13 +++++++++---- test/cmdtest_d/opts.py | 6 +++++- test/misc/opts_main.py | 3 +++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mmgen/cfg.py b/mmgen/cfg.py index b046c2a6..ac298e9b 100644 --- a/mmgen/cfg.py +++ b/mmgen/cfg.py @@ -30,11 +30,13 @@ def die2(exit_val, s): sys.stderr.write(s+'\n') sys.exit(exit_val) -def fmt_opt_val(opt_in, val): +def fmt_opt_val(opt_in, val, requires=False): opt = opt_in.replace('_', '-') match val: case True: return f'--{opt}' + case False if requires: + return f'--{opt} to be omitted' case False: return f'--no-{opt}' case _: @@ -731,10 +733,13 @@ class Config(Lockable): def _check_opts_data_requires_opts(self, opts_data): for a_opt, a_val, b_opt, b_val in opts_data['requires']: - if getattr(self, a_opt, False) == a_val and getattr(self, b_opt, False) != b_val: + usr_a_val = getattr(self, a_opt, False) + if ( + (usr_a_val == a_val or (usr_a_val and a_val is bool)) + and getattr(self, b_opt, False) != b_val): die('UserOptError', 'Option {} requires option {}\n'.format( - fmt_opt_val(a_opt, a_val), - fmt_opt_val(b_opt, b_val))) + fmt_opt_val(a_opt, usr_a_val), + fmt_opt_val(b_opt, b_val, requires=True))) def _die_on_incompatible_opts(self): for group in self._incompatible_opts: diff --git a/test/cmdtest_d/opts.py b/test/cmdtest_d/opts.py index 84242f29..926c94b7 100644 --- a/test/cmdtest_d/opts.py +++ b/test/cmdtest_d/opts.py @@ -63,6 +63,7 @@ class CmdTestOpts(CmdTestBase): ('opt_bad_sets2', (41, "invalid value for opts_data['sets']", [])), ('opt_bad_requires1', (41, "invalid value for opts_data['requires']", [])), ('opt_bad_requires2', (41, "invalid value for opts_data['requires']", [])), + ('opt_bad_requires3', (41, "invalid value for opts_data['requires']", [])), ('opt_invalid_1', (41, 'invalid cmdline opt ‘--x’', [])), ('opt_invalid_2', (41, 'invalid cmdline opt ‘---’', [])), ('opt_invalid_5', (41, 'invalid cmdline opt (missing parameter)', [])), @@ -353,7 +354,10 @@ class CmdTestOpts(CmdTestBase): return self.do_run(['--coin=DOGE'], 'requires option', 1) def opt_bad_requires2(self): - return self.do_run(['--no-foobleize', '--silent'], 'requires option --no-', 1) + return self.do_run(['--no-foobleize', '--silent'], 'to be omitted', 1) + + def opt_bad_requires3(self): + return self.do_run(['--data-size=123', '--data-ratio=3%'], 'to be omitted', 1) def opt_invalid(self, args, expect, opts=[], need_proto=False, exit_val=1): t = self.spawn_prog(args, opts=opts, exit_val=exit_val, need_proto=need_proto) diff --git a/test/misc/opts_main.py b/test/misc/opts_main.py index aa2742de..b70f9fa1 100755 --- a/test/misc/opts_main.py +++ b/test/misc/opts_main.py @@ -14,6 +14,7 @@ opts_data = { 'requires': [ ('coin', 'DOGE', 'no_foobleize', True), ('no_foobleize', True, 'silent', False), + ('data_size', bool, 'data_ratio', False), ], 'text': { 'desc': 'Opts test', @@ -23,6 +24,8 @@ opts_data = { --, --longhelp Print help message for long (global) options -i, --in-fmt= f Input is from wallet format 'f' -d, --outdir= d Use outdir 'd' +--, --data-size=N Specify data size of N +--, --data-ratio=N Specify data ratio of N percent -C, --print-checksum Print a checksum -E, --fee-estimate-mode=M Specify the network fee estimate mode. -F, --no-foobleize Do not foobleize the output, even on user request